设计模式之工厂模式
来源:互联网 发布:域名有什么作用 编辑:程序博客网 时间:2024/06/03 23:41
一般来说,有三种工厂模式,即简单工厂模式(simple factory),工厂方法模式(factory method),抽象工厂模式(abstract factory)
工厂模式属于创建型模式,主要是为了解决直接用new来生成对象是产生的object与client之间的紧耦合,直接用new那么你必须要熟知每个object,采用工厂模式,你就用一个抽象的接口来创建各个不同的object,采用接口就很好的去除了这种耦合,下面是三种工厂模式的C++实现,代码可在本人github网页上下载:https://github.com/hugewave/DesignPattern
一、简单工厂模式
/* * Example of 'Simple Factory' design pattern. * Copyright (C) 2016 Leo Wang * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{ public: virtual void Specification(){};};class CProductA:public CProduct{ public: void Specification() { cout<<"I'm ProductA!"<<endl; };};class CProductB:public CProduct{ public: void Specification() { cout<<"I'm ProductB!"<<endl; };};class CFactory{public: CProduct* MakeProduct(char producttype) { CProduct* cp_product=NULL; switch (producttype) { case 'A': cp_product=new CProductA(); break; case 'B': cp_product=new CProductB(); break; default: break; } return cp_product; };};void main(){ vector<CProduct*> v_product; CFactory* cp_factory=new CFactory(); CProduct* cp_product; cp_product=cp_factory->MakeProduct('A'); v_product.push_back(cp_product); cp_product=cp_factory->MakeProduct('B'); v_product.push_back(cp_product); vector<CProduct*>::iterator itr=v_product.begin(); for(;itr!=v_product.end();itr++) { (*itr)->Specification(); };};
二、工厂方法模式
/* * Example of 'Factory Method' design pattern. * Copyright (C) 2016 Leo Wang * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{ public: virtual void Specification(){};};class CProductA:public CProduct{ public: void Specification() { cout<<"I'm ProductA!"<<endl; };};class CProductB:public CProduct{ public: void Specification() { cout<<"I'm ProductB!"<<endl; };};class CFactory{public: virtual CProduct* MakeProduct()=NULL;};class CFactoryA:public CFactory{public: CProduct* MakeProduct() { return new CProductA(); };};class CFactoryB:public CFactory{public: CProduct* MakeProduct() { return new CProductB(); };};void main(){ vector<CProduct*> v_product; CFactory* cp_factoryA=new CFactoryA(); CFactory* cp_factoryB=new CFactoryB(); CProduct* cp_product; cp_product=cp_factoryA->MakeProduct(); v_product.push_back(cp_product); cp_product=cp_factoryB->MakeProduct(); v_product.push_back(cp_product); vector<CProduct*>::iterator itr=v_product.begin(); for(;itr!=v_product.end();itr++) { (*itr)->Specification(); };};
三、抽象工厂模式
/* * Example of 'Abstract Factory' design pattern. * Copyright (C) 2016 Leo Wang * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */#include <iostream>#include <vector>using namespace std;class CProduct{public: virtual void Specification(){};};class CProductA1:public CProduct{public: void Specification() { cout<<"I'm ProductA1!"<<endl; };};class CProductA2:public CProduct{public: void Specification() { cout<<"I'm ProductA2!"<<endl; };};class CProductB1:public CProduct{public: void Specification() { cout<<"I'm ProductB1!"<<endl; };};class CProductB2:public CProduct{public: void Specification() { cout<<"I'm ProductB2!"<<endl; };};class CFactory{public: virtual CProduct* MakeProductA()=NULL; virtual CProduct* MakeProductB()=NULL;};class CFactoryA:public CFactory{public: CProduct* MakeProductA() { return new CProductA1(); }; CProduct* MakeProductB() { return new CProductB1(); };};class CFactoryB:public CFactory{public: CProduct* MakeProductA() { return new CProductA2(); }; CProduct* MakeProductB() { return new CProductB2(); };};void main(){ vector<CProduct*> v_product; CFactory* cp_factoryA=new CFactoryA(); CFactory* cp_factoryB=new CFactoryB(); CProduct* cp_product; cp_product=cp_factoryA->MakeProductA(); v_product.push_back(cp_product); cp_product=cp_factoryA->MakeProductB(); v_product.push_back(cp_product); cp_product=cp_factoryB->MakeProductA(); v_product.push_back(cp_product); cp_product=cp_factoryB->MakeProductB(); v_product.push_back(cp_product); vector<CProduct*>::iterator itr=v_product.begin(); for(;itr!=v_product.end();itr++) { (*itr)->Specification(); };};
0 0
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之---工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之-工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之 ------工厂模式
- 设计模式之工厂模式
- 欢迎使用CSDN-markdown编辑器
- 自定义View(二)--Paint-Paint的setMaskFilter属性
- Linux命令之"du"
- OpenCV Tutorial: 影像平滑(medianBlur、bilateralFilter)
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- 设计模式之工厂模式
- Java软件工程师就业思维图
- MVC5创建自定义图片链接
- 二叉搜索树的插入和删除
- leetcode_136
- A Discriminative Feature Learning Approach for Deep Face Recognition, ECCV16.
- Android之内存泄露LeakCanary检测
- 快速排序
- 第82篇 webrtc一对一研究(十)及php