设计模式之抽象工厂(C++实现)
来源:互联网 发布:nginx 页面状态码修改 编辑:程序博客网 时间:2024/06/10 23:52
现在可以和工厂模式对比一下,抽象工厂返回的接口不再是产品A和产品B的共同基类Product了,而是产品A、产品B基类(在工厂模式中它们为具体实现类,这里变成了基类)了。此时工厂的抽象和简单工厂中的工厂方法也很类似,就是这些特征区使其别于工厂模式而变成抽象工厂模式了,因此抽象工厂解决的是创建一系列有共同风格的产品(鲁菜还是粤菜),而工厂方法模式解决的创建有共同特征的一系列产品(红烧肉、清蒸鱼它们都是食物)。当然简单工厂的缺陷在抽象工厂中又再次出现了,我要新增加一个产品,工厂抽象接口就要改变了。因此抽象工厂并不比工厂模式完美,只不过是各自的适用领域不同而已。
/*************************************************************Linux.h********************************************/
#ifndef LINUX_H#define LINUX_H#include <iostream>using namespace std;class Linux{public:virtual void showVersion() = 0;};#endif/*************************************************************LinuxPC.h********************************************/
#ifndef LINUXPC_H#define LINUXPC_H#include "Linux.h"class LinuxPC : public Linux{public:void showVersion(){cout<<"This Version is LinuxPC"<<endl;};};#endif
/*************************************************************LinuxMobile.h********************************************/
#ifndef LINUXMOBILE_H#define LINUXMOBILE_H#include "Linux.h"class LinuxMobile : public Linux{public:void showVersion(){cout<<"This Version is LinuxMobile"<<endl;};};#endif
/*************************************************************Windows.h********************************************/
#ifndef WINDOWS_H#define WINDOWS_H#include <iostream>using namespace std;class Windows{public:virtual void showVersion()=0;};#endif
/*************************************************************WindowsPCh********************************************/
#ifndef WINDOWSPC_H#define WINDOWSPC_H#include "Windows.h"class WindowsPC : public Windows{public:void showVersion(){cout<<"This Version is WindowsPC"<<endl;}};#endif
/*************************************************************WindowsMobile.h********************************************/
#ifndef WINDOWSMOBILE_H#define WINDOWSMOBILE_H#include "Windows.h"class WindowsMobile : public Windows{public:void showVersion(){cout<<"This Version is WindowsMobile"<<endl;}};#endif
/*************************************************************Factory.h********************************************/
#ifndef FACTORY_H#define FACTORY_H#include "LinuxMobile.h"#include "LinuxPC.h"#include "WindowsMobile.h"#include "WindowsPC.h"class Factory{Windows *windows;Linux *linux;public:Windows * ReturnWindowsVer(char s){switch(s){case 'm':windows = new WindowsMobile;return windows;break;case 'p':windows = new WindowsPC;return windows;break;}}Linux * ReturnLinuxVer(char s){switch(s){case 'm':linux = new LinuxMobile;return linux;break;case 'p':linux = new LinuxPC;return linux;break;}}};#endif
/*************************************************************main.cpp********************************************/
#include "LinuxMobile.h"#include "LinuxPC.h"#include "WindowsMobile.h"#include "WindowsPC.h"#include "Factory.h"int main(){Factory factory;Linux *linux = factory.ReturnLinuxVer('m');Windows *windows = factory.ReturnWindowsVer('p');linux->showVersion();windows->showVersion();getchar();return 0;}
- 设计模式的实现(C++)------抽象工厂模式(AbstractFactory)
- 设计模式之抽象工厂模式(c++)
- C语言实现《设计模式之禅》之工厂模式——抽象工厂
- 设计模式之抽象工厂,C++实现(一)
- 设计模式之抽象工厂(C++实现)
- 使用Object-C实现23种设计模式之抽象工厂模式
- 设计模式之Factory(工厂模式,抽象工厂)
- 24种设计模式之工厂模式--(抽象工厂)
- 抽象工厂模式 (C语言实现)
- 设计模式之抽象工厂
- 设计模式 之 抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 设计模式之抽象工厂
- 开发小注意
- Volley
- 【实务附案例】有关电子合同的法律问题全解
- laravel中pipeline中间件调用
- Java反射(二)----- 动态加载类
- 设计模式之抽象工厂(C++实现)
- linux 中断机制的处理过程
- Java(六) final用法实例讲解
- spring data jpa分页查询
- HDOJ 2089 不要62
- Lambda Functions in C++11
- 面试题:求任意时间分针和时针之间的度数
- 大数据生态之开源工具简介
- 用FileWriter 和FileReader实现文本文件的复制