Design Pattern: Abstract Factory 模式

  学习是分享和合作式的!

转载请注明出处:http://blog.csdn.net/wdzxl198/article/details/9219421; 

文章摘自: http://www.riabook.cn/doc/designpattern/;  

假设您要制作一个对话方块(Dialog)元件,您希望的是这个对话方块可以有不同的视感(Look-and- feel),最基本的想法是,藉由Setter将不同视感的元件设定给这个对话方块,例如:
  • CustomDialog.java
   1: public class CustomDialog {
   2:     private IButton button;
   3:     private ITextField textField;
   4:     
   5:     public void setButton(IButton button) {
   6:         this.button = button;    
   7:     }
   8:     
   9:     public void setTextField(ITextField textField) {
  10:         this.textField = textField;
  11:     }
  12:  
  13:     public void layoutAllComponents() {
  14:         // ....
  15:     }
  16:     
  17:     public void showDialog() {
  18:         this.paintDialog();
  19:         button.paintButton();
  20:         textField.paintTextField();
  21:     }
  22:   
  23:     public void paintDialog() {
  24:         System.out.println("custom dialog paints....");
  25:     }
  26: }
很简单,这是最基本的介面依赖,Setter依赖于IButton与ITextField两个介面,而不是其实作类别,不过这边还有个进一步的要求,使用 上面的方式还必须亲自呼叫Setter、layout等方法,您希望视感的更换可以更简单些,例如只要透一个元件的替换就可以完成对话方块上所有元件的视 感更换。
您可以使用Abstract Factory模式,将所有对话方块所需要的产生的元件加以封装,对话方块依赖于Abstract Factory,实际上具体的Factory实现则分别产生对话方块所需要的视感元件,下面的 UML 类别图展现这种概念。

现在如果要更换所有的视感元件,就只要抽象掉具体的Factory就可以了,例如:

   1: CustomDialog windowsDialog =
   2:  new CustomDialog(new WindowsWidgetFactory());
   3: windowsDialog.showDialog();
   4: CustomDialog macDialog =
   5:  new CustomDialog(new MacWidgetFactory());
   6: macDialog.showDialog();

来将上面的UML图具体实现出来。

  • CustomDialog.java
   1: public class CustomDialog {
   2:     private IButton button;
   3:     private ITextField textField;
   4:     
   5:     public CustomDialog(IWidgetFactory widgetFactory) {
   6:         setWidgetFactory(widgetFactory);
   7:     }
   8:     
   9:     // 由于客户端只依赖于抽象的工厂,工厂如何实作并无关客户端的事
  10:     // 要抽换工厂并不需要改动客户端的程式
  11:     public void setWidgetFactory(IWidgetFactory widgetFactory) {
  12:         setButton(widgetFactory.getButton());
  13:         setTextField(widgetFactory.getTextField());
  14:      
  15:     }
  16:  
  17:     public void layoutAllComponents() {
  18:         // layout all components
  19:     }
  20:     
  21:     // 这边也是依赖抽象,实际改变了元件实例
  22:     // 客户端代码也不用更改
  23:     public void setButton(IButton button) {
  24:         this.button = button;    
  25:     }
  26:     
  27:     public void setTextField(ITextField textField) {
  28:         this.textField = textField;
  29:     }
  30:     
  31:     public void showDialog() {
  32:         this.paintDialog();
  33:         button.paintButton();
  34:         textField.paintTextField();
  35:     }
  36:   
  37:     public void paintDialog() {
  38:         System.out.println("custom dialog paints....");
  39:     }
  40: } 
  • IButton.java
   1: public interface IButton {
   2:     public void paintButton();
   3: } 
  • ITextField.java
   1: public interface ITextField {
   2:     public void paintTextField();
   3: } 
  • IWidgetFactory.java
   1: public interface IWidgetFactory {
   2:     public IButton getButton();
   3:     public ITextField getTextField();
   4: } 
  • MacButton.java
   1: public class MacButton implements IButton {
   2:     public void paintButton() {
   3:         System.out.println("Mac button paints....");
   4:     }
   5: } 
  • WindowsButton.java
   1: public class WindowsButton implements IButton {
   2:     public void paintButton() {
   3:         System.out.println("Windows button paints....");
   4:     }
   5: } 
  • MacTextField.java
   1: public class MacTextField implements ITextField {
   2:     public void paintTextField() {
   3:         System.out.println("Mac textField paints....");
   4:     }
   5: } 
  • WindowsTextField.java
   1: public class WindowsTextField implements ITextField {
   2:     public void paintTextField() {
   3:         System.out.println("Windows textField paints....");
   4:     }
   5: } 
  • MacWidgetFactory.java
   1: public class MacWidgetFactory implements IWidgetFactory {
   2:     public IButton getButton() {
   3:         return new MacButton();
   4:     }
   5:     
   6:     public ITextField getTextField() {
   7:         return new MacTextField();
   8:     }
   9: } 
  • WindowsWidgetFactory.java
   1: public class WindowsWidgetFactory 
   2:                           implements IWidgetFactory {
   3:     public IButton getButton() {
   4:         return new WindowsButton();
   5:     }
   6:     
   7:     public ITextField getTextField() {
   8:         return new WindowsTextField();
   9:     }
  10: }
下图是Abstract Factory模式的UML结构图:

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

Edit bt Atlas

Time 2013/7/1 09:49

你可能感兴趣的:(设计模式)