客户端:
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.lang.reflect.Constructor; import java.lang.reflect.Method; public class Client { static public void main(String[] args) throws Throwable { RealSubject rs = new RealSubject(); //在这里指定被代理类 InvocationHandler ds = new DynamicSubject(rs); //初始化代理类 Subject subject = (Subject) Proxy.newProxyInstance(rs.getClass().getClassLoader(),rs.getClass ().getInterfaces(),ds ); subject.request(); } |
5)实例二:
package dynamicProxy; public interface Work { public void startWork(); } package dynamicProxy; public class JasonWork implements Work { public void startWork() { System.out.println("jason start to work..."); } } public interface Play { public void startPlay(); } public class JasonPlay implements Play { public void startPlay() { System.out.println("jason start to play..."); } } public class Test { public static void main(String[] args) { JasonWork work=new JasonWork(); InvocationHandler dynamicProxy=new DynamicProxy(work); Work jasonproxy=(Work)Proxy.newProxyInstance(work.getClass().getClassLoader(), work.getClass().getInterfaces(), dynamicProxy); jasonproxy.startWork(); JasonPlay play=new JasonPlay(); InvocationHandler dynamicProxy=new DynamicProxy(play); Play jasonproxy=(Play)Proxy.newProxyInstance(play.getClass().getClassLoader(), play.getClass().getInterfaces(), dynamicProxy); jasonproxy.startPlay(); } } |
===>动态代理类,可以与任何类型的真实类(work/play),进行结合,进行动态的代理.
------------------------------ 状态模式(state) -------------------------------
(1)State模式定义:不同的状态,不同的行为; 或者说,每个状态有着相应的行为.
适用场合:State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif else 进行状态切换, 如果针对状态的这样判断切换反复出现,我们就要联想到是否可以采取State模式了.
-->适合于内部状态,不断循环变化的.
(2)一个state,包括两部分: 对象 + 对象内部的属性(属性接口+具体属性)
一个对象,要有其属性,以及其setter,getter.且设置好其初始状态+一个调用显示状态的方法(里面就是状态调用自身的显示方法).
一个属性接口,应该有一个执行的方法.
一个具体属性,须包含对象进去,实现方法中,须设置对象下一个要显示的属性-->从而在对象下次调用方法时,其属性值会变化.
状态模式与观察者模式的区别:
状态模式,也跟观察者模式一样,是一对多的模式。但观察者模式是“一”变了,所有的“多”也会更新。
状态模式,强调的是:“多”是“一”的各个状态,“一”的各个状态,进行不断的循环。
如何建立一与多的关系:
“多”,都是实现一个接口的。所以,在“一”的类中,声明的是“多”的接口;若“多”中要建立与“一”的关系,只须直接在类中声明“一”即可。
(3)代码:
public interface Color { public void show(); } package state; class Light { Color color; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public Light() { color=new RedColor(this); } public void showColor() { color.show(); } } class RedColor implements Color { Light light; public RedColor(Light light) { this.light=light; } public void show() { System.out.println("the color is red,the car must stop !"); System.out.println("write down all logic shoud do this in this state....."); light.setColor(new GreenColor(light)); } } class GreenColor implements Color { Light light; public GreenColor(Light light) { this.light=light; } public void show() { System.out.println("the color is green,the car can run !"); System.out.println("write down all logic shoud do this in this state....."); light.setColor(new YellowColor(light)); } } class YellowColor implements Color { Light light; public YellowColor(Light light) { this.light=light; } public void show() { System.out.println("the color is yellow,the car shoud stop !"); System.out.println("write down all logic shoud do this in this state....."); light.setColor(new RedColor(light)); } } public class CarLight { public static void main(String[] args) { Light light=new Light(); //初始调用为红灯 light.showColor(); //再调用为绿灯 light.showColor(); //再调用为黄灯 light.showColor(); //不断调用,不断循环. } } |