设计模式系列:(3)Abstract Factory Patterns
2016-07-09 00:22
435 查看
1、GoF Definition
Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
2、Concept
In this pattern, we provide an encapsulation mechanism to a group of individual factories. These factories have a theme in common. In this process, an interface is used to create related objects. Here we do not call their implementer or concrete classes directly. We sometimes refer to this pattern as a factory of factories or a Super factory.
With this pattern, we can interchange the specific implementations without changing the user’s code. But to achieve this, we need to compensate for the complexity of the system. As a result, debugging may be difficult in many scenarios.
3、Real-Life Example
Suppose we are decorating our room. Now suppose we need two different types of almirah (or, say, table)— one must be made of wood and one of steel. For the wooden almirah, we need to visit a carpenter shop and for the other type, we can go to a readymade steel almirah shop. Both of these are almirah (or table) factories. Based on our demand, we decide what kind of factory we need. This scenario can be considered an example of this pattern.
4、代码
IActionMovie.java
IComedyMovie.java
IMovieFactory.java
BollywoodActionMovie.java
BollywoodComedyMovie.java
BollywoodMovieFactory.java
TollywoodActionMovie.java
TollywoodComedyMovie.java
TollywoodMovieFactory.java
5、Note
1. We use this pattern when our system does not care about how its products will be created or composed.
2. We use this pattern when we need to deal with multiple factories.
3. This pattern separates concrete classes and makes interchanging the products
easier. It can also enhance the reliabilities among products. But, at the same
time, we must acknowledge the fact that creating a new product is difficult with
this pattern (because we need to extend the interface and, as a result, changes
will be required in all of the subclasses that already implemented the interface).
Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
2、Concept
In this pattern, we provide an encapsulation mechanism to a group of individual factories. These factories have a theme in common. In this process, an interface is used to create related objects. Here we do not call their implementer or concrete classes directly. We sometimes refer to this pattern as a factory of factories or a Super factory.
With this pattern, we can interchange the specific implementations without changing the user’s code. But to achieve this, we need to compensate for the complexity of the system. As a result, debugging may be difficult in many scenarios.
3、Real-Life Example
Suppose we are decorating our room. Now suppose we need two different types of almirah (or, say, table)— one must be made of wood and one of steel. For the wooden almirah, we need to visit a carpenter shop and for the other type, we can go to a readymade steel almirah shop. Both of these are almirah (or table) factories. Based on our demand, we decide what kind of factory we need. This scenario can be considered an example of this pattern.
4、代码
IActionMovie.java
public interface IActionMovie { String MovieName(); }
IComedyMovie.java
public interface IComedyMovie { String MovieName(); }
IMovieFactory.java
public interface IMovieFactory { IActionMovie getActionMovie(); IComedyMovie getComedyMovie(); }
BollywoodActionMovie.java
public class BollywoodActionMovie implements IActionMovie { @Override public String MovieName() { return "This is Bollywood Action Movie"; } }
BollywoodComedyMovie.java
public class BollywoodComedyMovie implements IComedyMovie { @Override public String MovieName() { return "This is Bollywood Comedy Movie"; } }
BollywoodMovieFactory.java
public class BollywoodMovieFactory implements IMovieFactory { @Override public IActionMovie getActionMovie() { return new BollywoodActionMovie(); } @Override public IComedyMovie getComedyMovie() { return new BollywoodComedyMovie(); } }
TollywoodActionMovie.java
public class TollywoodActionMovie implements IActionMovie { @Override public String MovieName() { return "This is Tollywood Action Movie"; } }
TollywoodComedyMovie.java
public class TollywoodComedyMovie implements IComedyMovie { @Override public String MovieName() { return "This is Tollywood Comedy Movie"; } }
TollywoodMovieFactory.java
public class TollywoodMovieFactory implements IMovieFactory { @Override public IActionMovie getActionMovie() { return new TollywoodActionMovie(); } @Override public IComedyMovie getComedyMovie() { return new TollywoodComedyMovie(); } }
5、Note
1. We use this pattern when our system does not care about how its products will be created or composed.
2. We use this pattern when we need to deal with multiple factories.
3. This pattern separates concrete classes and makes interchanging the products
easier. It can also enhance the reliabilities among products. But, at the same
time, we must acknowledge the fact that creating a new product is difficult with
this pattern (because we need to extend the interface and, as a result, changes
will be required in all of the subclasses that already implemented the interface).
相关文章推荐
- 识设计大师(Logo Design Studio) v3.1.0.0 零售版 下载
- 基于Bootstrap实现Material Design风格表单插件 附源码下载
- JAVA正则表达式 Pattern和Matcher
- 【MaterialEditText】 Material Design 的 EditText 3ff8
- 订阅发布者模式
- 开源硬件的价值评估
- 基于Bootstrap实现Material Design风格表单插件 附源码下载
- 《Design patterns》读书笔记
- Eclipse中批量改变文件的默认打开方式
- CPU显卡内存与3DMAX渲染的关系
- Design Pattern 新解
- awk学习笔记七-Pattern
- 39_03_Linux集群系列之十三——高可用集群之corosync基础概念及安装配置(笔记)
- 20个漂亮的单页传单设计欣赏
- 转 C ++书单
- 单例分享(循环引用及内存占用解决)
- 。「设计营商周」每年均吸引来自设计、商界、中小企及教育界等全球优秀的设计精英汇聚香港
- 「设计营商周论坛」,非常荣幸邀请到多位国际知名大师级讲者出席
- 五种常见的php设计模式
- html5自适应网站如何做到对百度友好