设计模式---工厂模式
来源:互联网 发布:老男孩linux培训课件 编辑:程序博客网 时间:2024/06/06 19:50
工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。
#include<iostream>
using namespace std;
class LeiFeng
{
public:
void Sweep()
{
cout<<"sweep ..."<<endl;
}
void Wash()
{
cout<<"wash ..."<<endl;
}
void BuyRice()
{
cout<<"buyrice ..."<<endl;
}
};
class Undergraduate:public LeiFeng
{
};
class Volunteer:public LeiFeng
{
};
class IFactory
{
public:
LeiFeng* CreateLeiFeng(){}
};
class UndergraduateFactory:public IFactory
{
public:
LeiFeng* CreateLeiFeng()
{
return new Undergraduate();
}
};
class VolunteerFactory:public IFactory
{
public:
LeiFeng* CreateLeiFeng()
{
return new Volunteer();
}
};
int main()
{
IFactory *factory = new UndergraduateFactory();
LeiFeng *student = factory->CreateLeiFeng();
student->BuyRice();
student->Sweep();
student->Wash();
return 0;
}
阅读全文
0 0
- 设计模式------工厂模式
- 设计模式------工厂模式
- 设计模式 [ 工厂模式 ]
- 设计模式--工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式 工厂模式
- 设计模式-工厂模式
- 设计模式 -- 工厂模式
- 设计模式-工厂模式
- 设计模式----工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式---工厂模式
- 设计模式-工厂模式
- 设计模式--工厂模式
- 设计模式 - 工厂模式
- Java面试题(类初始化顺序总结)
- 拓扑排序之变量序列(三种算法实现)
- 【二叉树】最近公共祖先Lowest Common Ancestor of a Binary Tree
- 分享到QQ,新浪微博
- 仿CSDN客户端首页(一)----TabLayout实现选项卡滑动效果
- 设计模式---工厂模式
- C#中使用系统剪切板
- 【推荐】微服务大型分布式企业框架 Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa
- 创建CubieBoard2交叉编译链
- iterator adapter reverse_iterator
- uva699 The Falling Leaves 二叉树
- is-a && has-a && like-a
- 【如何校验邮件地址的有效性】第一篇 :为什么要校验邮箱地址有效性?使用场景在哪里?
- 24.返回栈