抽象工厂模式 AbstractFactory

Abstract Factory模式中将具体的Product封装在具体Factory实现中,而客户仍只要面对Factory与Product的抽象介面,避免依赖于具 体的Factory与Product,由于Factory封装了所必须的Product,所以要更换掉所有的元件,只要简单的抽换掉Factory就可以 了,不用修改客户端的程式。

Java代码   收藏代码
  1. /** 
  2.  * @author Jerval 
  3.  * @date 2011-4-23 
  4.  */  
  5. public interface IButton {  
  6.   
  7.     public void drawButton();  
  8. }  
  9.   
  10. /** 
  11.  * @author Jerval 
  12.  * @date 2011-4-23 
  13.  */  
  14. public interface ILabel {  
  15.       
  16.     public void drawLabel();  
  17. }  
  18. /** 
  19.  * @author Jerval 
  20.  * @date 2011-4-23 
  21.  */  
  22. public class XpButton implements IButton {  
  23.   
  24.     @Override  
  25.     public void drawButton() {  
  26.         System.out.println("draw xp button....");  
  27.     }  
  28.   
  29. }  
  30.   
  31. /** 
  32.  * @author Jerval 
  33.  * @date 2011-4-23 
  34.  */  
  35. public class XpLabel implements ILabel {  
  36.   
  37.     @Override  
  38.     public void drawLabel() {  
  39.         System.out.println("draw xp label...");  
  40.     }  
  41.   
  42. }  
  43.   
  44. /** 
  45.  * @author Jerval 
  46.  * @date 2011-4-23 
  47.  */  
  48. public class VistaButton implements IButton {  
  49.   
  50.     @Override  
  51.     public void drawButton() {  
  52.         System.out.println("draw vista button....");  
  53.     }  
  54.   
  55. }  
  56.   
  57. /** 
  58.  * @author Jerval 
  59.  * @date 2011-4-23 
  60.  */  
  61. public class VistaLabel implements ILabel {  
  62.   
  63.     @Override  
  64.     public void drawLabel() {  
  65.         System.out.println("draw vista label...");  
  66.     }  
  67.   
  68. }  
  69.   
  70.   
  71. /** 
  72.  * @author Jerval 
  73.  * @date 2011-4-23 
  74.  */  
  75. public interface IStyleFactory {  
  76.   
  77.     public IButton getButton();  
  78.     public ILabel getlILabel();  
  79. }  
  80.   
  81. /** 
  82.  * @author Jerval 
  83.  * @date 2011-4-23 
  84.  */  
  85. public class XpStyleFactory implements IStyleFactory{  
  86.   
  87.     @Override  
  88.     public IButton getButton() {  
  89.         return new XpButton();  
  90.     }  
  91.   
  92.     @Override  
  93.     public ILabel getlILabel() {  
  94.         return new XpLabel();  
  95.     }  
  96.   
  97. }  
  98. /** 
  99.  * @author Jerval 
  100.  * @date 2011-4-23 
  101.  */  
  102. public class VistaStyleFactory implements IStyleFactory{  
  103.   
  104.     @Override  
  105.     public IButton getButton() {  
  106.         return new VistaButton();  
  107.     }  
  108.   
  109.     @Override  
  110.     public ILabel getlILabel() {  
  111.         return new VistaLabel();  
  112.     }  
  113.   
  114. }  
  115.   
  116.   
  117. /** 
  118.  * @author Jerval 
  119.  * @date 2011-4-23 
  120.  */  
  121. public class CustomMsgBox {  
  122.   
  123.     private IButton button;  
  124.     private ILabel label;  
  125.   
  126.     public CustomMsgBox(IStyleFactory styleFactory) {  
  127.         setStyleFactory(styleFactory);  
  128.     }  
  129.   
  130.     // 客户端依赖于抽象工厂,更换工厂不需要改动客户端  
  131.     public void setStyleFactory(IStyleFactory styleFactory) {  
  132.         setButton(styleFactory.getButton());  
  133.         setLabel(styleFactory.getlILabel());  
  134.     }  
  135.   
  136.     // 依赖抽象,改变了元件实例客户端代码也不用更改  
  137.     public void setButton(IButton button) {  
  138.         this.button = button;  
  139.     }  
  140.   
  141.     public void setLabel(ILabel label) {  
  142.         this.label = label;  
  143.     }  
  144.   
  145.     public void show() {  
  146.         drawCustomMsgBox();  
  147.         button.drawButton();  
  148.         label.drawLabel();  
  149.     }  
  150.   
  151.     private void drawCustomMsgBox() {  
  152.         System.out.println("draw CustomMsgBox...");  
  153.     }  
  154. }  
  155.   
  156. /** 
  157.  * @author Jerval 
  158.  * @date 2011-4-23 
  159.  */  
  160. public class MainClass {  
  161.   
  162.     public static void main(String[] args) {  
  163.         //show xp style msgBox  
  164.         CustomMsgBox xpStyleMsgBox = new CustomMsgBox(new XpStyleFactory());  
  165.         xpStyleMsgBox.show();  
  166.         //show vista style msgBox  
  167.         CustomMsgBox vistaStyleMsgBox = new CustomMsgBox(new VistaStyleFactory());  
  168.         vistaStyleMsgBox.show();  
  169.     }  
  170. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值