- 浏览: 328247 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
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 951话说有一个银行,有三个窗口,但是每个窗口的智能都是一样的,即都 ... -
迭代器模式
2011-02-21 17:25 1007【迭代器模式】 迭代器可以顺序访问一个聚集中的元素而不必显露聚 ... -
装饰器模式
2011-02-21 17:25 1985浅谈装饰器模式 序: 今天,为了满足我们项目组长的愿望, ... -
解释器模式
2011-02-21 17:24 1073一、引子 其实没有什么好的例子引入解释器模式,因为它描述了 ... -
命令模式
2011-02-21 17:24 10351.意图: 将一个请求或操作封装到对象中。 2 ... -
策略模式
2011-02-21 17:23 906当我们掌握了Java的语法,当我们了解了面向对象的封装、继承、 ... -
状态模式
2011-02-21 17:23 11461,状态模式允许一个"对象"在其内部状态改 ... -
模板模式
2011-02-21 17:23 1199模板方法(Template Method)模式是GOF设计模式 ... -
备忘录模式 (下)
2011-02-21 17:22 2157一、什么是备忘录模式 Memento模式也叫备忘录模式 ... -
备忘录
2011-02-21 17:22 1109备忘录(Memento Pattern)模式 备忘录模式又叫 ... -
原型模式Prototype(深拷贝)
2011-02-21 17:21 18871、定义:原型模式就是 ... -
原型模式
2011-02-21 17:21 1100原型模式(Prototype Patter ... -
工厂方法模式
2011-02-21 17:20 11621、工厂模式的核心思想及分类 工厂方法模式的作用是 ... -
生成器模式
2011-02-21 17:19 1111实际上,既然Builder和Factory同属创建型模式,那么 ... -
构造者模式
2011-02-21 17:19 1164多种设计模式能够解决 ... -
亨元模式
2011-02-21 17:18 1160翻译为亨元模式,或直译为轻量级模式。所谓亨元,就是被其它对象共 ... -
责任链模式
2011-02-21 17:18 6672责任链模式(Chain of Resp ... -
代理模式
2011-02-21 17:17 883代理模式:给某一对象提供代理对象,并由代理对象控制具体对象的引 ... -
观察者模式
2011-02-21 17:17 1041size=9] 论坛上很多人都讲设计模式,也讲了很多设计模式, ... -
多线程下的单例模式(上)
2011-02-21 17:07 1094Abstract 在开发中,如果某个实例的 ...
相关推荐
工厂模式分为三种主要类型:简单工厂模式、工厂方法模式和抽象工厂模式。 1. **简单工厂模式** 简单工厂模式是最简单的工厂模式实现,它提供一个静态方法或者类来创建对象,这个类通常被称为“工厂”。用户只需要...
抽象工厂模式是设计模式中的一种创建型模式,它提供了一种创建对象集合的接口,而无需指定具体的类。这种模式允许系统独立于如何创建、组合和表示产品对象的细节进行设计,为产品族(一组相关或相互依赖的对象)提供...
在软件设计模式中,工厂模式是一组非常基础且实用的设计模式,主要分为简单工厂模式、工厂方法模式和抽象工厂模式。这些模式都是为了解决对象创建的问题,通过封装对象的创建过程,使得代码更加灵活,易于扩展和维护...
java设计模式 抽象工厂模式详解 一张图让你彻底明白抽象工厂模式
工厂方法模式和抽象工厂模式是两种常见的设计模式,它们都属于创建型模式,用于解决对象的创建问题。在软件设计中,这两种模式都是用来隔离对象的创建和使用,以提高系统的灵活性和可扩展性。 首先,工厂方法模式的...
设计模式 - 抽象工厂模式 抽象工厂模式是一种创建型设计模式,它提供了一种方式来创建一组相关或相互依赖的对象,而不需要指定具体的类。该模式允许客户端使用抽象的接口来创建一组相关的产品,而不需要关系实际...
本文将探讨三个重要的设计模式:抽象工厂模式、工厂方法模式以及策略模式,并结合一个实际的场景——手机加工厂,来具体阐述它们的应用。 首先,我们来看**抽象工厂模式**。这个模式主要用于创建相关或依赖对象的...
抽象工厂模式是软件工程中常用的一种创建型设计模式,它的核心思想是为创建一组相关或相互依赖的对象提供一个接口,而不需要指定它们具体的类。在Java中,抽象工厂模式被广泛应用,它有助于整合代码,提高系统的可...
抽象工厂模式是设计模式中的一种创建型模式,它提供了一个创建对象族的接口,而无需指定它们的具体类。在.NET开发中,这种模式常用于实现多数据库连接,比如连接到MySQL、SQL Server或Oracle等不同的数据库系统。...
抽象工厂模式是一种设计模式,属于创建型模式,它提供了一种创建对象族的接口,而无需指定其具体的类。这种模式的关键在于“族”,它表示一系列相关或相互依赖的对象。在不指定具体类的情况下,抽象工厂模式使得...
抽象工厂模式是设计模式中的一种创建型模式,它提供了一种创建对象族的接口,而无需指定具体的类。在C#编程中,这种模式经常被用于实现跨平台或跨框架的代码,使得代码与具体实现解耦,提高系统的灵活性和可扩展性。...
### 抽象工厂模式简介与应用实例 #### 一、抽象工厂模式定义 抽象工厂模式是一种创建型设计模式,它能够让我们从一个公共接口中创建一系列相关或相互依赖的对象,而无需指定它们的具体类。该模式的核心在于提供了...
抽象工厂模式是设计模式中的一种创建型模式,它提供了一种创建对象族的接口,而无需指定其具体的类。这种模式的关键在于“族”,即一系列相关的对象。在抽象工厂模式中,我们创建一个抽象工厂接口,然后为每一种具体...
在这个压缩包中,包含了三种工厂模式的C++实现:简单工厂模式、工厂方法模式以及抽象工厂模式。让我们一一探讨这些模式。 1. 简单工厂模式: 简单工厂模式是一种静态工厂方法,它提供一个公共的工厂类来创建对象。...
抽象工厂模式是设计模式中的一种,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。在软件工程中,当系统需要在运行时选择不同的产品族时,或者想要隔离具体产品的实现细节时,抽象工厂...
抽象工厂模式是设计模式中的一种结构型模式,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。在Android开发中,这种模式尤其有用,因为Android平台有多种设备,每种设备可能有不同的UI...
抽象工厂模式是设计模式中的一种,它属于创建型模式,主要解决的是当系统有多个产品族,而每个产品族又有多个具体产品时,如何组织代码的问题。在Java中,抽象工厂模式提供了一种创建对象组的方式,使得这些对象属于...
抽象工厂模式是一种面向对象的设计模式,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。在C#中,这种模式经常用于软件工程中的框架设计,允许系统独立于具体产品的实现进行扩展和修改。...
在软件设计模式中,工厂方法模式(Factory Method Pattern)和抽象工厂模式(Abstract Factory Pattern)是两种常用的创建型设计模式,它们都属于“工厂”家族,但有着不同的应用场景和目标。 工厂方法模式的核心...
抽象工厂模式是一种创建型设计模式,它提供了一种创建对象族的方法,而无需指定它们的具体类。在计算器的实现中,这个模式可以帮助我们构建不同类型的计算器,比如简单计算器、科学计算器等,而无需修改现有代码。 ...