代理模式和装饰器模式

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


public class DecorateAndProxyTest {

	public static void main(String[] args) throws Exception {
		Ball ball = (Ball)ProxyFactory.getProxyInstance(new Football());
		ball.play();
		String name = ball.getName();
		
		FootballDecorate decorate1 = new FootballDecorateSub1(new Football());
		decorate1.play();
		decorate1.getName();
		
		FootballDecorate decorate2 = new FootballDecorateSub2(new Football());
		decorate2.play();
		decorate2.getName();
		
		
	}
	
}


/* ================================ proxy ====================================== */

class ProxyFactory {
	
	public static Object getProxyInstance(Object proxyInstance) throws Exception {
		return Proxy.newProxyInstance(proxyInstance.getClass().getClassLoader(),
					proxyInstance.getClass().getInterfaces(),
					new MyInvocationHandler(proxyInstance));
	}
	
}

class MyInvocationHandler implements InvocationHandler {
	
	private Object obj;
	
	public MyInvocationHandler() {
		
	}
	
	public MyInvocationHandler(Object obj) {
		this.bind(obj);
	}
	
	
	public void bind(Object obj) {
		this.obj = obj;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		
		OperationUtils.beforeCall();
		
		Object methodReturn = method.invoke(obj, args);
		
		OperationUtils.afterCall();
		
		return methodReturn;
	}

	
}

interface Ball {
	
	void play();
	
	String getName();
	
}


class Football implements Ball {

	@Override
	public void play() {
		System.out.println("play football");
	}

	@Override
	public String getName() {
		System.out.println("call getName()");
		return "football";
	}
	
}

class OperationUtils {
	
	public static void beforeCall() {
		System.out.println("调用前。。。");
	}
	
	public static void afterCall() {
		System.out.println("调用后。。。");
	}
	
}



/* ================================ decorate ================================= */


abstract class FootballDecorate implements Ball {
	
	private Ball ball;
	
	public FootballDecorate(Ball ball) {
		this.ball = ball;
	}
	
	@Override
	public void play() {
		ball.play();
	}
	
	@Override
	public String getName() {
		return ball.getName();
	}
	
}

class FootballDecorateSub1 extends FootballDecorate {
	
	public FootballDecorateSub1(Ball ball) {
		super(ball);
	}
	
	@Override
	public void play() {
		
		System.out.println("before play 1 ");
		
		super.play();
		
		System.out.println("after play 1 ");
	}

	@Override
	public String getName() {
		
		System.out.println("before getName() 1 ");
		
		String name = super.getName();
		
		System.out.println("after getName() 1 ");
		
		return name;
	}
	
}


class FootballDecorateSub2 extends FootballDecorate {
	
	public FootballDecorateSub2(Ball ball) {
		super(ball);
	}
	
	@Override
	public void play() {
		
		System.out.println("before play 2 ");
		
		super.play();
		
		System.out.println("after play 2 ");
	}

	@Override
	public String getName() {
		
		System.out.println("before getName() 2 " );
		
		String name = super.getName();
		
		System.out.println("after getName() 2 ");
		
		return name;
	}
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值