恋愛中のデザインモードのエージェントモード(エージェントモード近似AOP機能実現)


エージェント・モードは、静的エージェントと動的エージェントに分けられます.
次に,静的エージェントと動的エージェントの例を示し,エージェントモードを用いてAOPの機能を実現する方法を示す.
シミュレーションによるAOP機能の実現

package com.fruitking.designparten.proxy.aopsimulate;

public interface Appointment {
	
	/**
	 *     
	 * @param address
	 * @return
	 */
	public String eat(String address);
	
	/**
	 *    
	 * @param movie
	 * @return
	 */
	public String seeMovie(String movie);
	
	/**
	 *   
	 */
	public void walk();

}

package com.fruitking.designparten.proxy.aopsimulate;

public class HeroImpl implements Appointment {

	public String eat(String address) {
		System.out.println("        ...");
		String result = "";
		if("   ".equals(address)){
			result = "  ,  ";
		}else if("   ".equals(address)){
			result = "  ,   ";
		}else if("   ".equals(address)){
			result = "  ,  ";
		}else if("   ".equals(address)){
			result = "  ";
		}else{
			result = "   ";
		}
		return result;
	}

	public String seeMovie(String movie) {
		System.out.println("         ...");
		String result = "";
		if("     ".equals(movie)){
			result = "    ,    ";
		}else if("    ".equals(movie)){
			result = "    ";
		}else{
			result = "  ";
		}
		return result;
	}

	public void walk() {
		System.out.println("        ...");
	}

}

package com.fruitking.designparten.proxy.aopsimulate;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class HeroProxy implements InvocationHandler {
	
	private Appointment appointmentObj;
	private Appendix appendixObj;
	
	public HeroProxy(){
	}
	
	public Appointment bind(Appointment appointmentObj,Appendix appendixObj){
           this.appointmentObj = appointmentObj;
           this.appendixObj = appendixObj;
           return (Appointment)Proxy.newProxyInstance(appointmentObj.getClass().getClassLoader(),appointmentObj.getClass().getInterfaces(),this);
    }
	
	public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
		//          
		Class<? extends Appendix> clazz = this.appendixObj.getClass();
		//        prepare  
		Method prepare = clazz.getDeclaredMethod("prepare",new Class[] {});
		//    prepare  
		prepare.invoke(this.appendixObj,new Object[]{});
		Object obj = method.invoke(this.appointmentObj, args);
		//        end  
		Method endOfAppointment = clazz.getDeclaredMethod("endOfAppointment",new Class[] {});
		//    endOfAppointment  
		endOfAppointment.invoke(this.appendixObj,new Object[]{});
		return (String)obj;
	}
}

package com.fruitking.designparten.proxy.aopsimulate;

public interface Appendix {
	
	/**
	 *           
	 */
	public void prepare();
	
	/**
	 *           
	 */
	public void endOfAppointment();

}

package com.fruitking.designparten.proxy.aopsimulate;

public class AppendixImpl implements Appendix {

	public void prepare() {
		System.out.println("     ,    ,    ");
	}
	
	public void endOfAppointment(){
		System.out.println("    ,      ,     ");
	}

}

package com.fruitking.designparten.proxy.aopsimulate;


public class Test {

	public static void main(String[] args) {
		try{
			Appointment appointmentImpl = new HeroImpl();
			Appendix appendixImpl = new AppendixImpl();
			HeroProxy heroProxy = new HeroProxy();
			Appointment appointment = (Appointment)heroProxy.bind(appointmentImpl,appendixImpl);
			String result = appointment.eat("   ");
			System.out.println(result);
			System.out.println("-------------------------");
			result = appointment.seeMovie("    ");
			System.out.println(result);
			System.out.println("-------------------------");
			appointment.walk();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}