- 浏览: 323237 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
arlenliugj:
才发现这贴子好早
如何在Ubuntu下安装windows7 -
arlenliugj:
请问一下,这样安装上windows会不会把已经装好的linux ...
如何在Ubuntu下安装windows7 -
zhaomengbin:
写的很不错,可以写个文件给合并的方法么?,将分割后的几份文件还 ...
文件分割程序 -
junhe0723:
3Q,刚出现这个问题解决了。
jvm terminated exit code 1 -
Anddy:
shell 双击选中太不智能了。
XSHELL快捷键设置
假如你要制作一个对话框控件,你希望这个对话框可以有不同的Look&Feel,最基本的想法是,使用setter将不同的Look&Feel注入到这个对话框,例如:
CustomDialog.java
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就可以了,例如:
Java代码
1. CustomDialog windowsDialog =
2. new CustomDialog(new WindowsWidgetFactory());
3. windowsDialog.showDialog();
4.
5. CustomDialog macDialog =
6. new CustomDialog(new MacWidgetFactory());
7. macDialog.showDialog();
将上面的UML图实现出来:
CustomDialog.java
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.createButton());
13. setTextField(widgetFactory.createTextField());
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
Java代码
1. public interface IButton {
2. public void paintButton();
3. }
ITextField.java
Java代码
1. public interface ITextField {
2. public void paintTextField();
3. }
IWidgetFactory.java
Java代码
1. public interface IWidgetFactory {
2. public IButton createButton();
3. public ITextField createTextField();
4. }
MacButton.java
Java代码
1. public class MacButton implements IButton {
2. public void paintButton() {
3. System.out.println("Mac button paints....");
4. }
5. }
WindowsButton.java
Java代码
1. public class WindowsButton implements IButton {
2. public void paintButton() {
3. System.out.println("Windows button paints....");
4. }
5. }
MacTextField.java
Java代码
1. public class MacTextField implements ITextField {
2. public void paintTextField() {
3. System.out.println("Mac textField paints....");
4. }
5. }
WindowsTextField.java
Java代码
1. public class WindowsTextField implements ITextField {
2. public void paintTextField() {
3. System.out.println("Windows textField paints....");
4. }
5. }
MacWidgetFactory.java
Java代码
1. public class MacWidgetFactory implements IWidgetFactory {
2. public IButton createButton() {
3. return new MacButton();
4. }
5.
6. public ITextField createTextField() {
7. return new MacTextField();
8. }
9. }
WindowsWidgetFactory.java
Java代码
1. public class WindowsWidgetFactory
2. implements IWidgetFactory {
3. public IButton createButton() {
4. return new WindowsButton();
5. }
6.
7. public ITextField createTextField() {
8. return new WindowsTextField();
9. }
10. }
下图是Abstract Factory模式的UML结构图:
简单的说,在Abstract Factory模式中将具体的Product封装在Factory实现中,而库户仍只要面对Factory与Product的抽象界面,避免依赖于具体的 Factory与Product,由于Factory封装了所有必须的Product,所以要更换所有的控件,只需要简单地替换掉Factory的具体实 现就可以了,不需要修改客户端的程序。
事例2:
The intent of Abstract Factory is to provide for the creation of a family of related, or dependent, objects. see pic:
Additional note is below:( reference from http://www.dofactory.com) :
• AbstractFactory (ContinentFactory)
o declares an interface for operations that create abstract products
• ConcreteFactory (AfricaFactory, AmericaFactory)
o implements the operations to create concrete product objects
• AbstractProduct (Herbivore, Carnivore)
o declares an interface for a type of product object
• Product (Wildebeest, Lion, Bison, Wolf)
o defines a product object to be created by the corresponding concrete factory
o implements the AbstractProduct interface
• Client (AnimalWorld)
o uses interfaces declared by AbstractFactory and AbstractProduct classes
using the term: 产品族(Product Family),the former pic is like:
In this pattern, speaking loosely, a package is usually a "family" of classes, and an abstract factory produces a "family" of objects.
• /* GUIFactory example --
• The output should be either "I'm a WinButton" or "I'm an OSXButton"
• depending on which kind of factory was used. Note that the Application
• has no idea what kind of GUIFactory it is given or even what kind of
• Button that factory creates.*/
•
• interface GUIFactory {
• public Button createButton();
• }
•
•
• class WinFactory implements GUIFactory {
• public Button createButton() {
• return new WinButton();
• }
• }
•
•
• class OSXFactory implements GUIFactory {
• public Button createButton() {
• return new OSXButton();
• }
• }
•
•
•
• interface Button {
• public void paint();
• }
•
•
• class WinButton implements Button {
• public void paint() {
• System.out.println("I'm a WinButton");
• }
• }
•
•
• class OSXButton implements Button {
• public void paint() {
• System.out.println("I'm an OSXButton");
• }
• }
•
•
• class Application {
• public Application(GUIFactory factory){
• Button button = factory.createButton();
• button.paint();
• }
• }
•
• public class ApplicationRunner {
• public static void main(String[] args) {
• new Application(createOsSpecificFactory());
• }
•
• public static GUIFactory createOsSpecificFactory() {
• int sys = readFromConfigFile("OS_TYPE");
• if (sys == 0) {
• return new WinFactory();
• } else {
• return new OSXFactory();
• }
• }
• }
CustomDialog.java
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就可以了,例如:
Java代码
1. CustomDialog windowsDialog =
2. new CustomDialog(new WindowsWidgetFactory());
3. windowsDialog.showDialog();
4.
5. CustomDialog macDialog =
6. new CustomDialog(new MacWidgetFactory());
7. macDialog.showDialog();
将上面的UML图实现出来:
CustomDialog.java
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.createButton());
13. setTextField(widgetFactory.createTextField());
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
Java代码
1. public interface IButton {
2. public void paintButton();
3. }
ITextField.java
Java代码
1. public interface ITextField {
2. public void paintTextField();
3. }
IWidgetFactory.java
Java代码
1. public interface IWidgetFactory {
2. public IButton createButton();
3. public ITextField createTextField();
4. }
MacButton.java
Java代码
1. public class MacButton implements IButton {
2. public void paintButton() {
3. System.out.println("Mac button paints....");
4. }
5. }
WindowsButton.java
Java代码
1. public class WindowsButton implements IButton {
2. public void paintButton() {
3. System.out.println("Windows button paints....");
4. }
5. }
MacTextField.java
Java代码
1. public class MacTextField implements ITextField {
2. public void paintTextField() {
3. System.out.println("Mac textField paints....");
4. }
5. }
WindowsTextField.java
Java代码
1. public class WindowsTextField implements ITextField {
2. public void paintTextField() {
3. System.out.println("Windows textField paints....");
4. }
5. }
MacWidgetFactory.java
Java代码
1. public class MacWidgetFactory implements IWidgetFactory {
2. public IButton createButton() {
3. return new MacButton();
4. }
5.
6. public ITextField createTextField() {
7. return new MacTextField();
8. }
9. }
WindowsWidgetFactory.java
Java代码
1. public class WindowsWidgetFactory
2. implements IWidgetFactory {
3. public IButton createButton() {
4. return new WindowsButton();
5. }
6.
7. public ITextField createTextField() {
8. return new WindowsTextField();
9. }
10. }
下图是Abstract Factory模式的UML结构图:
简单的说,在Abstract Factory模式中将具体的Product封装在Factory实现中,而库户仍只要面对Factory与Product的抽象界面,避免依赖于具体的 Factory与Product,由于Factory封装了所有必须的Product,所以要更换所有的控件,只需要简单地替换掉Factory的具体实 现就可以了,不需要修改客户端的程序。
事例2:
The intent of Abstract Factory is to provide for the creation of a family of related, or dependent, objects. see pic:
Additional note is below:( reference from http://www.dofactory.com) :
• AbstractFactory (ContinentFactory)
o declares an interface for operations that create abstract products
• ConcreteFactory (AfricaFactory, AmericaFactory)
o implements the operations to create concrete product objects
• AbstractProduct (Herbivore, Carnivore)
o declares an interface for a type of product object
• Product (Wildebeest, Lion, Bison, Wolf)
o defines a product object to be created by the corresponding concrete factory
o implements the AbstractProduct interface
• Client (AnimalWorld)
o uses interfaces declared by AbstractFactory and AbstractProduct classes
using the term: 产品族(Product Family),the former pic is like:
In this pattern, speaking loosely, a package is usually a "family" of classes, and an abstract factory produces a "family" of objects.
• /* GUIFactory example --
• The output should be either "I'm a WinButton" or "I'm an OSXButton"
• depending on which kind of factory was used. Note that the Application
• has no idea what kind of GUIFactory it is given or even what kind of
• Button that factory creates.*/
•
• interface GUIFactory {
• public Button createButton();
• }
•
•
• class WinFactory implements GUIFactory {
• public Button createButton() {
• return new WinButton();
• }
• }
•
•
• class OSXFactory implements GUIFactory {
• public Button createButton() {
• return new OSXButton();
• }
• }
•
•
•
• interface Button {
• public void paint();
• }
•
•
• class WinButton implements Button {
• public void paint() {
• System.out.println("I'm a WinButton");
• }
• }
•
•
• class OSXButton implements Button {
• public void paint() {
• System.out.println("I'm an OSXButton");
• }
• }
•
•
• class Application {
• public Application(GUIFactory factory){
• Button button = factory.createButton();
• button.paint();
• }
• }
•
• public class ApplicationRunner {
• public static void main(String[] args) {
• new Application(createOsSpecificFactory());
• }
•
• public static GUIFactory createOsSpecificFactory() {
• int sys = readFromConfigFile("OS_TYPE");
• if (sys == 0) {
• return new WinFactory();
• } else {
• return new OSXFactory();
• }
• }
• }
发表评论
-
访问者模式
2011-02-21 17:25 915话说有一个银行,有三个窗口,但是每个窗口的智能都是一样的,即都 ... -
迭代器模式
2011-02-21 17:25 971【迭代器模式】 迭代器可以顺序访问一个聚集中的元素而不必显露聚 ... -
装饰器模式
2011-02-21 17:25 1911浅谈装饰器模式 序: 今天,为了满足我们项目组长的愿望, ... -
解释器模式
2011-02-21 17:24 1006一、引子 其实没有什么好的例子引入解释器模式,因为它描述了 ... -
命令模式
2011-02-21 17:24 10021.意图: 将一个请求或操作封装到对象中。 2 ... -
策略模式
2011-02-21 17:23 821当我们掌握了Java的语法,当我们了解了面向对象的封装、继承、 ... -
状态模式
2011-02-21 17:23 10981,状态模式允许一个"对象"在其内部状态改 ... -
模板模式
2011-02-21 17:23 1097模板方法(Template Method)模式是GOF设计模式 ... -
备忘录模式 (下)
2011-02-21 17:22 2101一、什么是备忘录模式 Memento模式也叫备忘录模式 ... -
备忘录
2011-02-21 17:22 1033备忘录(Memento Pattern)模式 备忘录模式又叫 ... -
原型模式Prototype(深拷贝)
2011-02-21 17:21 18321、定义:原型模式就是 ... -
原型模式
2011-02-21 17:21 1067原型模式(Prototype Patter ... -
工厂方法模式
2011-02-21 17:20 10971、工厂模式的核心思想及分类 工厂方法模式的作用是 ... -
生成器模式
2011-02-21 17:19 1063实际上,既然Builder和Factory同属创建型模式,那么 ... -
构造者模式
2011-02-21 17:19 1109多种设计模式能够解决 ... -
亨元模式
2011-02-21 17:18 1096翻译为亨元模式,或直译为轻量级模式。所谓亨元,就是被其它对象共 ... -
责任链模式
2011-02-21 17:18 6629责任链模式(Chain of Resp ... -
代理模式
2011-02-21 17:17 822代理模式:给某一对象提供代理对象,并由代理对象控制具体对象的引 ... -
观察者模式
2011-02-21 17:17 993size=9] 论坛上很多人都讲设计模式,也讲了很多设计模式, ... -
多线程下的单例模式(上)
2011-02-21 17:07 1047Abstract 在开发中,如果某个实例的 ...
相关推荐
java设计模式 抽象工厂模式详解 一张图让你彻底明白抽象工厂模式
介绍的是工厂模式 包括简单工厂模式、工厂方法模式、抽象工厂模式 包括PPT和代码
设计模式-抽象工厂模式(讲解及其实现代码)
工厂方法模式和抽象工厂模式的区别,对工厂方法模式和抽象工厂模式做了简单的比较
设计模式 抽象工厂模式 反射 配置文件 设计模式 抽象工厂模式 反射 配置文件 设计模式 抽象工厂模式 反射 配置文件 设计模式 抽象工厂模式 反射 配置文件 设计模式 抽象工厂模式 反射 配置文件 设计模式 抽象工厂...
深入浅出设计模式之抽象工厂模式+工厂方法模式+策略模式实现手机加工厂(加类图)
抽象工厂模式ppt。抽象工厂模式ppt。抽象工厂模式ppt。
设计模式--抽象工厂模式实现多数据库连接 设计模式,.net设计模式,抽象工厂
java设计模式【之】抽象工厂模式【源码】【场景:制作咖啡】 * 抽象工厂 * * 创建对象实例时,不要直接 new 类, 而是把这个new 类的动作放在一个工厂的方法中,并返回。 * 在工厂方法的基础上,进一步抽象工厂...
设计模式 - 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern) 引入: 在前面介绍的两个创建型模式里面,我们解决的都是有关"new"的问题,用它们来避免显式指定类创建对象。我写的也非常简单易懂,相信看过的朋友们都应该对简单工厂...
抽象工厂模式.ppt抽象工厂模式.ppt
通过C++实现了简单工厂模式、工厂模式、抽象工厂模式三大设计模式的经典案例。
设计模式 抽象工厂模式 三层架构 设计模式 抽象工厂模式 三层架构 设计模式 抽象工厂模式 三层架构 设计模式 抽象工厂模式 三层架构 设计模式 抽象工厂模式 三层架构 要您一个资源分,也算是对我的尊重,谢谢下载。
抽象工厂模式实现计算器源码,学习C#设计模式。
在C#项目开发中设计模式中的抽象工厂模式应用实例实现。
抽象工厂模式的实现源代码,清晰易懂,还有详细的说明
通过抽象工厂搜提供的接口,可以创建产品的家族,利用这个接口书写代码,我们...这是与博客《抽象工厂模式--GOF的23个之一》配套实例源代码,具体说明欢迎阅读。 如有好的建议,可留言或发至笔者邮箱:fzb_xxzy@163.com
项目需求: 有两间塑料加工厂(A厂仅生产容器类产品;B厂仅生产模具类产品);... 抽象工厂模式以一种倾斜的方式支持增加新的产品,它为新产品族的增加提供方便,而不能为新的产品等级结构的增加提供这样的方便。
c++ java 抽象工厂模式