Design Pattern ->Factory Method
Layering & Contract Philosophy With additional indirection
Factory Method

The example code is as following:
class CProduct //interface declaration
class CConcreteProductOne: public CProduct;
class CConcreteProductTwo: public CProduct; class CCreator
{
public: virtual CProduct* FactoryMethod()= ;
}
class CConcreteCreator: public CCreator
{
public: CProduct* FactoryMethod(){ return new CConcreteProductOne() OR new CConcreteProductTwo()};
}
class CClient
{
CCreator *pCreator = new CConreteCreator();
CProduct *pProduct = pCreator->FacotryMethod();
}
IoC = Inversion Of Control.
控制反转还有一个名字叫做依赖注入(Dependency Injection)。简称DI
DIP = Dependence-Inversion Principle ( low-level module is depended on high-level module by defining abstract interface in high-level).
Interface-Oriented Programming, not Implement-Oriented Programming
Applicability
Use the Factory Method pattern when
- A class can't anticipate the class of objects it must create.
- A class wants its subclasses to specify the objects it creates.
- Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
- The new proper/appropriate instance of the concrete product can be got at run-time from the real situation.
Participants
- Product: defines the interface of objects the factory method creates.
- ConcreteProduct : implements the Product interface.
- Creator : declares the factory method, which returns an object of type Product.Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object.
- ConcreteCreator: overrides the factory method to return an instance of a ConcreteProduct.
Collaborations
- Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.
Design Pattern ->Factory Method的更多相关文章
- Design Pattern ——Factory Method&Abstract Factory
今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...
- design pattern factory method #Reprinted#
引入人.工厂.和斧子的问题: (1),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...
- [Design Pattern] Factory Pattern 简单案例
Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...
- Java Design Pattern(Factory,Singleton,Prototype,Proxy)
一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...
- Design Pattern :Factory and Reflect in java
interface page { void Render(); } class pageA implements page { @Override public void Re ...
- [design pattern](5) Factory Method
前言 在前面一章博主介绍了简单工厂模式(Simple Factory),接着上面的章节,今天博主就来介绍下工厂方法模式(Factory Method). 思考题 首先,让我们来思考下面的问题: 在上一 ...
- 打造属于你的提供者(Provider = Strategy + Factory Method) 设计模式 - Provider Pattern(提供者模式)
打造属于你的提供者(Provider = Strategy + Factory Method) 1.1.1 摘要 在日常系统设计中,我们也许听说过提供者模式,甚至几乎每天都在使用它,在.NET F ...
- 设计模式-模板方法设计模式--Template Method design pattern
/** * Abstract implementation of the {@link org.springframework.context.ApplicationContext} * interf ...
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
原文:乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pa ...
随机推荐
- Ruby truthy and falsey
在Ruby里只有false 和nil表示falsey link: https://gist.github.com/jfarmer/2647362
- poj1094 拓扑排序(出度入度简单使用)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37764 Accepted: 13 ...
- jq ajax超时设置
var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...
- sp_executesq用法
第一种用法: --@sqlstring :就是你要执行的sql语句字符串--@ParmDefinition: @sqlstring里边用到的参数在这里声明 输出的参数要加output --sp_exe ...
- spring boot 系统启动时运行代码(1)-@PostConstruct
Application.java import org.springframework.boot.SpringApplication; import org.springframework.boot. ...
- my25_Mysql操作技巧汇总
1. drop database 在数据量很大的情况下,最好先对表进行truncate,然后再drop database:不然会卡住很长的时间. 2. 数据的逻辑导入导出 如果数据量大,又需要进行逻辑 ...
- my23_pxc其中一个节点重建记录
PXC报废了一个节点,时间大概在周五,而此时故障的数据库节点比较多,警告信息也成百上千,此信息混合于已有的故障节点信息中,没有被及时发现:然后周六.周日各报废一个,在周一的时候,业务已经没有节点可以写 ...
- set与map
set /*set里面没有相同的元素 所以可以用于数组去重*/ //内部去重用的是=== 对象不等 但是 NaN等 //其本身是构造函数 let s=new Set([1,2,{},{},3,4,Na ...
- 【ACM】蛇形填数 - 逻辑怪
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 1 ...
- mapreduce统计总数
现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,名为buyer_favorite1. buyer_favorite1包含:买家id,商品id,收藏日期这三个字段,数据以“\t ...