设计模式程序参考2

来源:互联网 发布:人人聚财网络贷款 编辑:程序博客网 时间:2024/05/07 17:21
参观者模式
参观者模式主要对一组固定结构的对象进行访问,一般和组合模式一起使用。 
  1. abstract class Hardware{  
  2.    protected double price;  
  3.    protected double getPrice();  
  4.    void accept(IComputerVisitor visitor);  
  5.  
  6.   
  7. class Mainboard extends Hardware{  
  8.    void accept(IComputerVisitor visitor){  
  9.       visitor.visitMainboard(this);  
  10.     
  11.  
  12.   
  13. class Memory extends Hardware{  
  14.    void accept(IComputerVisitor visitor){  
  15.       visitor.visitMemory(this);  
  16.     
  17.  
  18.   
  19. class Display extends Hardware(  
  20.    void accept(IComputerVisitor visitor){  
  21.       visitor.visitDisplay(this);  
  22.     
  23.  
  24.   
  25. class NetworkAdapter extends Hardware{  
  26.    void accept(IComputerVisitor visitor){  
  27.       visitor.visitNetworkAdapter(this);  
  28.     
  29.  
  30.   
  31. class Computer extends Hardware[  
  32.    private List<Hardware> parts new Arraylist();  
  33.    public List<Hardware> add(Hardware hardware){  
  34.       parts.add(hardware);  
  35.       return parts;  
  36.     
  37.    public void accept(IComputerVisitor visitor){  
  38.       for(Hardware h: parts){  
  39.          h.accept(visitor);  
  40.        
  41.     
  42.  
  43.   
  44. interface IComputerVisitor{  
  45.    void visitMainboard(Mainboard mainboard);  
  46.    void visitNetworkAdapter(NetworkAdapter adapter);  
  47.    void visitDisplay(Display display);  
  48.    void visitMemory(Memory memory);  
  49.  
  50.   
  51. //遍历 computer 的每个部件,汇总价格  
  52. class PriceVisitor implements IComputerVisitor{  
  53.    //总价格  
  54.    private double amountPrice;  
  55.    void visitMainboard(Mainboard mainboard){  
  56.       amountPrice += mainboard.getPrice();  
  57.     
  58.    void visitNetworkAdapter(NetworkAdapter adapter)  
  59.       amountPrice += adapter.getPrice();  
  60.     
  61.    void visitDisplay(Display display)  
  62.       amountPrice += display.getPrice();  
  63.     
  64.    void visitMemory(Memory memory)  
  65.       amountPrice += memory.getPrice();  
  66.     
  67.  
abstract class Hardware{   protected double price;   protected double getPrice();   void accept(IComputerVisitor visitor);}class Mainboard extends Hardware{   void accept(IComputerVisitor visitor){      visitor.visitMainboard(this);   }}class Memory extends Hardware{   void accept(IComputerVisitor visitor){      visitor.visitMemory(this);   }}class Display extends Hardware(   void accept(IComputerVisitor visitor){      visitor.visitDisplay(this);   }}class NetworkAdapter extends Hardware{   void accept(IComputerVisitor visitor){      visitor.visitNetworkAdapter(this);   }}class Computer extends Hardware[   private List<Hardware> parts = new Arraylist();   public List<Hardware> add(Hardware hardware){      parts.add(hardware);      return parts;   }   public void accept(IComputerVisitor visitor){      for(Hardware h: parts){         h.accept(visitor);      }   }}interface IComputerVisitor{   void visitMainboard(Mainboard mainboard);   void visitNetworkAdapter(NetworkAdapter adapter);   void visitDisplay(Display display);   void visitMemory(Memory memory);}//遍历 computer 的每个部件,汇总价格class PriceVisitor implements IComputerVisitor{   //总价格   private double amountPrice;   void visitMainboard(Mainboard mainboard){      amountPrice += mainboard.getPrice();   }   void visitNetworkAdapter(NetworkAdapter adapter) {      amountPrice += adapter.getPrice();   }   void visitDisplay(Display display) {      amountPrice += display.getPrice();   }   void visitMemory(Memory memory) {      amountPrice += memory.getPrice();   }}



Client:
 
  1. Computer computer new Computer();  
  2. Mainboard mainboard new Mainboard();  
  3. NetworkAdapter networkAdapter new NetworkAdapter();  
  4. Display display new Display();  
  5. Memory memory new Memory();  
  6. computer .add(mainboad)  
  7.          .add(networkAdapter)  
  8.          .add(display)  
  9.          .add(memory);  
  10. IComputerVisitor visitor new PriceVisitor();  
  11. visitor.accept(computer);  
观察者模式

  1. //主题,这里是快餐店  
  2. class SnackShop{  
  3.    private List<Customer> customers new Arraylist();  
  4.    public void add(Customer customer){  
  5.       customers.add(customer);  
  6.     
  7.    //通知,对订阅主题的客户发布通知,比如“外卖已好”  
  8.    public void notify(){  
  9.       for(Customer c: customers){  
  10.          c.getFood();  
  11.        
  12.     
  13.  
  14.   
  15. //订阅者,这里是客户  
  16. class Customer{  
  17.    Customer (SnackShop shop){  
  18.       //将客户加入到快餐店列表  
  19.       shop.add(this);  
  20.     
  21.    //回调函数,当接到通知后,客户的动作  
  22.    public void getFood(){  
  23.       //取得外卖  
  24.     
  25.  
//主题,这里是快餐店class SnackShop{   private List<Customer> customers = new Arraylist();   public void add(Customer customer){      customers.add(customer);   }   //通知,对订阅主题的客户发布通知,比如“外卖已好”   public void notify(){      for(Customer c: customers){         c.getFood();      }   }}//订阅者,这里是客户class Customer{   Customer (SnackShop shop){      //将客户加入到快餐店列表      shop.add(this);   }   //回调函数,当接到通知后,客户的动作   public void getFood(){      //取得外卖   }}

Client:
 
  1. SnackShop snackShop new SnackShop();  
  2. Customer zhangSan new Customer(snackShop);  
  3. snackShop.notify();  
SnackShop snackShop = new SnackShop();Customer zhangSan = new Customer(snackShop);snackShop.notify();
策略模式
  1. //密钥对生成接口  
  2. interface IKeyPairGenerable{  
  3.    KeyPair create();  
  4.  
  5.   
  6. class KeyPair{  
  7.  
  8.   
  9. class DesKeyPairGenerator implements IKeyPairGenerable{  
  10.  
  11.   
  12. class IdeaKeyPairGenerator implements IKeyPairGenerable{  
  13.  
  14.   
  15. class RsaKeyPairGenerator implements IKeyPairGenerable{  
  16.  
  17.   
  18. class KeyPairManager{  
  19.    private IKeyPairGenerable generator;  
  20.    private List keyPairList new Arraylist();  
  21.    public void setGenerator(IKeyPairGenerable generator){  
  22.       this.generator generator;  
  23.     
  24.    public KeyPair create(){  
  25.       KeyPair keyPair null 
  26.       if(null != generator){  
  27.          keyPair generator.create();  
  28.          keyPairList.add(keyPair);  
  29.        
  30.       return keyPair;  
  31.     
  32.  
//密钥对生成接口interface IKeyPairGenerable{   KeyPair create();}class KeyPair{}class DesKeyPairGenerator implements IKeyPairGenerable{}class IdeaKeyPairGenerator implements IKeyPairGenerable{}class RsaKeyPairGenerator implements IKeyPairGenerable{}class KeyPairManager{   private IKeyPairGenerable generator;   private List keyPairList = new Arraylist();   public void setGenerator(IKeyPairGenerable generator){      this.generator = generator;   }   public KeyPair create(){      KeyPair keyPair = null;      if(null != generator){         keyPair = generator.create();         keyPairList.add(keyPair);      }      return keyPair;   }}


Client:
  1. IKeyPairGenerable desGenerator  new DesKeyPairGenerator();  
  2. IKeyPairGenerable rsaGenerator   new RsaKeyPairGenerator();  
  3. IKeyPairGenerable ideaGenerator  new IdeaKeyPairGenerator();  
  4.   
  5. KeyPairManager manager new KeyPairManager();  
  6.   
  7. //使用 DES 算法生成密钥  
  8. manager.setGenerator(desGenerator);  
  9. KeyPair keyPair manager.create();  
  10.   
  11. //使用 RSA 算法生成密钥  
  12. manager.setGenerator(rsaGenerator);  
  13. KeyPair keyPair manager.create();  
IKeyPairGenerable desGenerator =  new DesKeyPairGenerator();IKeyPairGenerable rsaGenerator =   new RsaKeyPairGenerator();IKeyPairGenerable ideaGenerator =  new IdeaKeyPairGenerator();KeyPairManager manager = new KeyPairManager();//使用 DES 算法生成密钥manager.setGenerator(desGenerator);KeyPair keyPair = manager.create();//使用 RSA 算法生成密钥manager.setGenerator(rsaGenerator);KeyPair keyPair = manager.create();



可以看出,KeyPairManager 仅仅依赖于接口 IKeyPairGeneratble, 改变密钥对生成算法不改变KeyPairManager 类。

策略模式一般用来封装算法的不同实现。

 

原创粉丝点击