标签:spring his target main 对象 java ted extends tin
java实现了自己的观察者模式 >>> java.util.Observable;
1、public synchronized void addObserver(Observer o);添加一个观察者
2、public synchronized void deleteObserver(Observer o);删除一个观察者
3、protected synchronized void setChanged();修改通知标记,使得观察者观察的对象发生变化时能记得得知
4、public void notifyObservers(Object arg) ;通知观察者,被观察这已发生变化
1 public class ObserverDemo { 2 3 public static void main(String[] args) { 4 // 被观察的对象 5 MyObservable obs = new MyObservable(); 6 7 // 添加观察者 8 obs.addObserver((o, arg) -> System.out.println("服务列表发生了变化1:" + arg)); 9 10 Observer o = (o1, arg) -> System.out.println("服务列表发生了变化2:" + arg); 11 obs.addObserver(o); 12 13 obs.setServerList("192.168.110.1"); 14 15 obs.deleteObserver(o); 16 obs.setServerList("192.168.110.2"); 17 } 18 } 19 20 class MyObservable extends Observable { 21 22 public void setServerList(String serverList) { 23 this.setChanged(); 24 this.notifyObservers(serverList); 25 } 26 }
1、静态代理
代理就是将除了自己本身需要做的核心业务外的任务交给代理人来做,如明星将接广告等事情交给经纪人。
1 public class Client { 2 public static void main(String[] args) { 3 Teacher teacher = new ProxyDemo(new MathTeacher()); 4 teacher.teach(); 5 } 6 } 7 8 class ProxyDemo implements Teacher { 9 private Teacher teacher; 10 11 public ProxyDemo(Teacher teacher) { 12 this.teacher = teacher; 13 } 14 15 @Override 16 public void teach() { 17 before(); 18 teacher.teach(); 19 after(); 20 } 21 22 public void before() { 23 System.err.println("准备资料。。。"); 24 } 25 26 public void after() { 27 System.err.println("布置作业。。。"); 28 } 29 } 30 31 interface Teacher { 32 void teach(); 33 } 34 35 class MathTeacher implements Teacher { 36 @Override 37 public void teach() { 38 System.err.println("上数学课"); 39 } 40 }
静态代理的缺点是如果需要重新代理一个对象的话,上述代码都需要重弄一遍,这样非常繁琐且重复,所以便有了动态代理。
2、动态代理
1 public class Client { 2 public static void main(String[] args) { 3 Dynamic dynamic = new Dynamic(new MathTeacher()); 4 Teacher mathTeacher = (Teacher) dynamic.getProxyInstance(); 5 mathTeacher.teach(); 6 7 System.err.println("-----------------------"); 8 9 Dynamic dynamic2 = new Dynamic(new EnglishTeacher()); 10 Teacher mathTeacher2 = (Teacher) dynamic2.getProxyInstance(); 11 mathTeacher2.teach(); 12 } 13 } 14 15 class Dynamic implements InvocationHandler { 16 private Object target; 17 18 public Dynamic(Object target) { 19 this.target = target; 20 } 21 22 public void before() { 23 System.out.println("准备资料。。。"); 24 } 25 26 public void after() { 27 System.out.println("布置作业。。。"); 28 } 29 30 @Override 31 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 32 before(); 33 Object result = method.invoke(target, args); 34 after(); 35 return result; 36 } 37 38 public Object getProxyInstance() { 39 return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); 40 } 41 } 42 43 interface Teacher { 44 void teach(); 45 } 46 47 class MathTeacher implements Teacher { 48 @Override 49 public void teach() { 50 System.out.println("上数学课"); 51 } 52 } 53 54 class EnglishTeacher implements Teacher { 55 @Override 56 public void teach() { 57 System.out.println("上英语课"); 58 } 59 }
标签:spring his target main 对象 java ted extends tin
原文地址:https://www.cnblogs.com/bzfsdr/p/11587815.html