java 代理模式,观察者模式

代理模式1

[java] view plain copy
 
  1. import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.lang.reflect.InvocationHandler;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.Proxy;  
  4.   
  5. public class ProxyTest {  
  6.   
  7.     public static void main(String[] args) {  
  8.         SaleComputer saleComputer = new ComputerMaker();  
  9.         InvocationHandler handler = new SaleProxy(saleComputer);  
  10.           
  11.         SaleComputer proxy = (SaleComputer)Proxy.newProxyInstance(saleComputer.getClass().getClassLoader(), saleComputer.getClass().getInterfaces(), handler);  
  12.         proxy.saleComputer("花花");  
  13.     }  
  14.   
  15. }  
  16.   
  17. interface SaleComputer{ //卖电脑  
  18.     public void saleComputer(String type);  
  19. }  
  20.   
  21. class ComputerMaker implements SaleComputer{  
  22.   
  23.     public void saleComputer(String type) {  
  24.         System.out.println("卖出一台 " + type +" 牌电脑!");  
  25.     }  
  26.       
  27. }  
  28.   
  29. class SaleProxy implements InvocationHandler{  
  30.   
  31.     Object delegate;  
  32.       
  33.     public SaleProxy(Object delegate) {  
  34.         this.delegate = delegate;  
  35.     }  
  36.   
  37.     public Object invoke(Object proxy, Method method, Object[] args)  
  38.             throws Throwable {  
  39.         giveMouse();  
  40.         return method.invoke(delegate,args);  
  41.     }  
  42.       
  43.     private void giveMouse(){  
  44.         System.out.println("送鼠标...............");  
  45.     }  
  46.       
  47. }  

代理模式2

[java] view plain copy
 
  1. public class ProxyTest1 {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         SaleComputer1 saleComputer = new ComputerMaker1();  
  6.         SaleComputer1 proxy = new SaleProxy1(saleComputer);  
  7.         proxy.saleComputer("鸿润");  
  8.     }  
  9.       
  10. }  
  11.   
  12. interface SaleComputer1{ //接口  
  13.     public void saleComputer(String type);  
  14. }  
  15.   
  16. class ComputerMaker1 implements SaleComputer1{//实现类  
  17.   
  18.     public void saleComputer(String type) {  
  19.         System.out.println("卖出一台 " + type +" 牌电脑!");  
  20.     }  
  21. }  
  22.   
  23. class SaleProxy1 implements SaleComputer1{   //代理类  
  24.       
  25.     SaleComputer1 delegate;  
  26.       
  27.     public SaleProxy1(Object delegate){  
  28.         this.delegate = (SaleComputer1)delegate;  
  29.     }  
  30.       
  31.     public void saleComputer(String type) {  
  32.         this.giveMouse();  
  33.         delegate.saleComputer(type);  
  34.     }  
  35.       
  36.     private void giveMouse(){  
  37.         System.out.println("送鼠标...............");  
  38.     }  
  39. }  

 观察者模式:

[java] view plain copy
 
  1. package com.model;  
  2.   
  3. import java.util.Observable;  
  4. import java.util.Observer;  
  5.   
  6. /** 
  7.     Java的API为我们提供了Observer接口和Observable类来实现所谓观察者模式。 
  8.     Observable(可观察者)类允许在自身发生改变时,通知其它对象(实现接口Observer,观察者)。 
  9.  */  
  10. public class TestObserver{  
  11.       
  12.     public static void main(String[] args){  
  13.           
  14.         Produce produce = new Produce();  
  15.         NameObserver nameObserver = new NameObserver();  
  16.         PriceObserver priceObserver = new PriceObserver();  
  17.           
  18.         produce.addObserver(nameObserver);  
  19.         produce.addObserver(priceObserver);  
  20.           
  21.         produce.setName("Apple");  
  22.         produce.setPrice(100);  
  23.           
  24.     }  
  25. }  
  26.   
  27. //一个可观察者  
  28. class Produce extends Observable{  
  29.   
  30.     private String name;  
  31.     private Integer price;  
  32.       
  33.     public String getName() {  
  34.         return name;  
  35.     }  
  36.     public void setName(String name) {  
  37.         this.name = name;  
  38.         setChanged();          //设置变化点   
  39.         notifyObservers(name); //通知观察者  
  40.     }  
  41.     public Integer getPrice() {  
  42.         return price;  
  43.     }  
  44.     public void setPrice(Integer price) {  
  45.         this.price = price;  
  46.         setChanged();  
  47.         notifyObservers(price);  
  48.     }  
  49.       
  50. }  
  51.   
  52. //两个观察者  
  53. class NameObserver implements Observer{  
  54.   
  55.     public void update(Observable o, Object arg) {  
  56.         if(arg instanceof String){  
  57.             System.out.println("观察者观察到:产品名字已经改为: " +  arg);  
  58.         }  
  59.     }  
  60.       
  61. }  
  62.   
  63. class PriceObserver implements Observer{  
  64.   
  65.     public void update(Observable o, Object arg) {  
  66.         if(arg instanceof Integer){  
  67.             System.out.println("观察者观察到:价格已经改为: " +  arg);  
  68.         }  
  69.     }  
  70.       
  71. }  
原文地址:https://www.cnblogs.com/to-creat/p/5547946.html