创建型模式--抽象工厂模式(Abstract Factory)
2007-06-17 22:16
281 查看
Abstract Factory:Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
抽象工厂(Abstract Factory)模式是所有工厂模式中最抽象和最具有一般性的形态,和工厂方法模式的最大区别在于,工厂方法模式针对的是一个产品等级结构,而抽象工厂模式则需要对多个产品等级结构。
一:引入
最早起源应用于不通操作系统的视窗构件,如一个Button类在windows环境和unix环境下有对应的实现,如果使用工厂方法模式,需要每个Concrete Product类对应一个Concrete Factory,在太多构件的情况下显然不合适。
改为有两个Concrete Factory(因为有两大类(当然也可以有多个类)),每个Factory类负责创建这一类的所有对象。
public class WinFactory implements AbstractFactory...{
public Button createButton()
...{
return new WinButton();
}
public Text createText()
...{
return new WinText();
}
}
public class UnixFactory implements AbstractFactory...{
public Button createButton()
...{
return new UnixButton();
}
public Text createText()
...{
return new UnixText();
}
}
二:结构
三:实际应用
最早起源应用于不通操作系统的视窗构件。
四:适用情形
Use the Abstract Factory pattern when
a system should be independent of how its products are created, composed, and represented.
a system should be configured with one of multiple families of products.
a family of related product objects is designed to be used together, and you need to enforce this constraint.
you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
抽象工厂(Abstract Factory)模式是所有工厂模式中最抽象和最具有一般性的形态,和工厂方法模式的最大区别在于,工厂方法模式针对的是一个产品等级结构,而抽象工厂模式则需要对多个产品等级结构。
一:引入
最早起源应用于不通操作系统的视窗构件,如一个Button类在windows环境和unix环境下有对应的实现,如果使用工厂方法模式,需要每个Concrete Product类对应一个Concrete Factory,在太多构件的情况下显然不合适。
改为有两个Concrete Factory(因为有两大类(当然也可以有多个类)),每个Factory类负责创建这一类的所有对象。
public class WinFactory implements AbstractFactory...{
public Button createButton()
...{
return new WinButton();
}
public Text createText()
...{
return new WinText();
}
}
public class UnixFactory implements AbstractFactory...{
public Button createButton()
...{
return new UnixButton();
}
public Text createText()
...{
return new UnixText();
}
}
二:结构
三:实际应用
最早起源应用于不通操作系统的视窗构件。
四:适用情形
Use the Abstract Factory pattern when
a system should be independent of how its products are created, composed, and represented.
a system should be configured with one of multiple families of products.
a family of related product objects is designed to be used together, and you need to enforce this constraint.
you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
相关文章推荐
- 创建型模式01:简单工厂模式、工厂方法模式、抽象工厂模式
- 创建型模式:抽象工厂模式(Abstract Factory Pattern)
- java设计模式(创建型)之抽象工厂模式
- 创建型模式のAbstractFactory抽象工厂模式
- 创建型模式-抽象工厂模式(abstract_factory)
- [创建型模式系列]Abstract factory of Design Pattern模式设计之抽象工厂模式
- 创建型设计模式——抽象工厂模式
- 创建型模式----抽象工厂模式
- (四)(创建型模式)抽象工厂模式
- 设计模式(三) : 创建型模式--抽象工厂模式
- Java设计模式之创建型模式-抽象工厂模式(Abstract Factory)
- Java与设计模式(十一)创建型--抽象工厂模式
- 创建型模式之抽象工厂模式
- Abstract Factory模式(抽象工厂模式 创建型)
- (创建型模式)ABSTRACT FACTORY——抽象工厂模式
- 设计模式之创建型抽象工厂模式
- 7种创建型模式 之3 Abastract Factory 抽象工厂模式 《Java与模式》学习笔记
- GoF23种设计模式之创建型模式之抽象工厂模式
- 23种设计模式——抽象工厂模式(创建型模式)
- "围观"设计模式(8)--创建型之简单工厂模式、工厂方法模式、抽象工厂模式