学习是分享和合作式的!
转载请注明出处:http://blog.csdn.net/wdzxl198/article/details/9219421;
文章摘自: http://www.riabook.cn/doc/designpattern/;
- 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: }
您可以使用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模式中将具体的Product封装在具体Factory实现中,而客户仍只要面对Factory与Product的抽象介面,避免依赖于具 体的Factory与Product,由于Factory封装了所必须的Product,所以要更换掉所有的元件,只要简单的抽换掉Factory就可以 了,不用修改客户端的程式。
Edit bt Atlas
Time 2013/7/1 09:49