大话设计模式--工厂模式 factory -- C++实现实例
实现《大话设计模式》的C++版本。。。
1. 工厂模式 使用的范围是 同一个基类,下面很多子类。
(1)这里很容易出现的一个问题n多的子类继承自抽象基类,我们不得不在每次要用到子类的地方就编写诸如new ×××;的代码。这里带来两个问题1)客户程序员必须知道实际子类的名称(当系统复杂后,命名将是一个很不好处理的问题,为了处理可能的名字冲突,有的命名可能并不是具有很好的可读性和可记忆性,就姑且不论不同程序员千奇百怪的个人偏好了。),2)程序的扩展性和维护变得越来越困难。
(2)还有一种情况就是在父类中并不知道具体要实例化哪一个具体的子类。这里的意思为:假设我们在类A中要使用到类B,B是一个抽象父类,在A中并不知道具体要实例化那一个B的子类,但是在类A的子类D中是可以知道的。在A中我们没有办法直接使用类似于new ×××的语句,因为根本就不知道×××是什么。
注意: 如下的结构图, 假设还要添加一个算法, 则需要多实现一个算法类, 并且修改简单工厂类的case,这违反 “开放--封闭”原则, 对扩展开放,对修改封闭。。。
这是工厂模式的缺点,后文有工厂方法模式的比较。。
下面为(1)的示例:一个关于计算器的设计, 设计有 “+ - * /”等运算,基类 operation , 下面有“+ - * /”的实现子类。
operation.h
#ifndef OPERATION_H
#define OPERATION_H class operation
{
public:
operation();
double virtual getResult(); double strA;
double strB;
double result;
}; #endif // OPERATION_H
operation.cpp
#include "operation.h" operation::operation()
{
strA = 0;
strB = 0;
result = 0;
} double operation::getResult()
{
return result;
}
operationFunc.h
#ifndef OPERATIONFUNC_H
#define OPERATIONFUNC_H #include "operation.h" class OperationAdd : public operation
{
public:
double getResult();
}; class OperationSub : public operation
{
public:
double getResult();
}; class OperationMul : public operation
{
public:
double getResult();
}; class OperationDiv : public operation
{
public:
double getResult();
}; #endif // OPERATIONFUNC_H
operationFunc.cpp
#include "operationFunc.h" double OperationAdd::getResult()
{
result = strA + strB;
return result;
} double OperationSub::getResult()
{
result = strA - strB;
return result;
} double OperationMul::getResult()
{
result = strA * strB;
return result;
} double OperationDiv::getResult()
{
result = strA / strB;
return result;
}
operationfactory.h
#ifndef OPERATIONFACTORY_H
#define OPERATIONFACTORY_H #include <string>
#include "operation.h"
#include "operationFunc.h"
using namespace std; class OperationFactory
{
public:
OperationFactory();
operation* createOperation(string operStr);
}; #endif // OPERATIONFACTORY_H
operationfactory.cpp
#include "operationfactory.h" OperationFactory::OperationFactory()
{
} operation* OperationFactory::createOperation(string operStr)
{
operation *oper = NULL; if( operStr == "+" )
oper = new OperationAdd();
else if( operStr == "-" )
oper = new OperationSub();
else if( operStr == "*" )
oper = new OperationMul();
else if( operStr == "/" )
oper = new OperationDiv(); return oper;
}
main.cpp
#include <iostream>
#include <string>
#include "operationfactory.h" using namespace std; int main()
{
cout << "Simple Factory test" << endl; operation *oper = NULL;
OperationFactory factory;
oper = factory.createOperation("/");
oper->strA = 1.1;
oper->strB = 2.2;
double result = oper->getResult();
cout << "result: " << result << endl; return 0;
}
大话设计模式--工厂模式 factory -- C++实现实例的更多相关文章
- 设计模式 - 工厂模式(factory pattern) 具体解释
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/27081511 工厂模式(factory pa ...
- [设计模式]工厂模式factory
参考:http://wxg6203.iteye.com/blog/740229 简单工厂模式(simple factory)是类的创建模式,又叫静态工厂方法(static factory method ...
- 23种设计模式--工厂模式-Factory Pattern
一.工厂模式的介绍 工厂模式让我们相到的就是工厂,那么生活中的工厂是生产产品的,在代码中的工厂是生产实例的,在直白一点就是生产实例的类,代码中我们常用new关键字,那么这个new出来的实例 ...
- 设计模式--工厂模式Factory(创建型)
工厂模式属于创建型模式,分为三类,简单工厂模式.工厂方法模式.抽象工厂模式. 一.简单工厂模式 在工厂中做判断,根据产品类型从而创造相应的产品,当增加新产品时需要修改工厂类. 例如: enum CTY ...
- <大话设计模式>工厂模式,策略模式
第一章:工厂模式: 通过封装,继承,多态解耦合 业务逻辑和界面逻辑分开 用单独的类创造实例,工厂:创造实例 工厂模式还可以用反射来实现,nsstringFromClass UML类图 聚合表示一众弱的 ...
- 大话设计模式--策略模式 strategy -- C++实现实例
1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...
- 设计模式——工厂模式(Factory Method)
工厂方法模式,定义一个用于创建对象的接口,让子类决定实例化哪个类.工厂方法使一个类的实例化延迟到其子类. UML图: 运算基类: package com.cnblog.clarck; /** * 数据 ...
- 大话设计模式--访问者模式 Visitor -- C++实现实例
1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...
- 大话设计模式--解释器模式 interpreter -- C++实现实例
1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...
随机推荐
- O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求
筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...
- php7.0 出现 curl_setopt(): Disabling safe uploads is no longer supported in 报错!
项目换成php7.0,进行了测试,使用curl时,出现: curl_setopt(): Disabling safe uploads is no longer supported in xxx.定位到 ...
- grep -rl tttt /data/ 命令在 /data 目录下面搜寻包含tttt字符的命令
grep --help -R, -r, --recursive equivalent to --directories=recurse -l, --files-with-matches print o ...
- args *args **kwargs区别
python 函数中的参数类型有两种,分别为 位置参数和关键字参数: 一 .位置参数(该类参数位置固定不变) args: 表示默认位置参数,该参数是具象的,有多少个参数就传递多少参数,且参数位 ...
- memcached在Java中的应用以及magent的配置-每天进步一点点
memcached在Java中的应用: http://nhy520.iteye.com/blog/1775893 magent配置memcached分布式集群的应用: http://www.jians ...
- 【JMeter4.0学习(六)】之逻辑控制器说明
主要demo例子在: 链接: https://pan.baidu.com/s/1OFdsrNG7PTOYQ8TdjiVtBQ 密码: tkd2 汇总参考文档:<Jmeter之逻辑控制器(Logi ...
- Unity5 怎样做资源管理和增量更新
工具 Unity 中的资源来源有三个途径:一个是Unity自己主动打包资源.一个是Resources.一个是AssetBundle. Unity自己主动打包资源是指在Unity场景中直接使用到的资源会 ...
- mysql-5.1.73多实例安装启动
一.源码包下载:http://download.softagency.net/MySQL/Downloads/MySQL-5.1/ 二.编译安装 groupadd mysql useradd -r - ...
- 查找 TextBox 对象中非法数据的示例
private void GetErrors(StringBuilder sb, DependencyObject obj){ foreach (object child in LogicalTree ...
- ASP.NET动态网站制作(14)-- CSS3
前言:这节课主要讲解CSS之前没有讲到过的知识点以及CSS3的一些内容. 内容: 1.内容参考博文:http://www.cnblogs.com/ruanmou/p/4832214.html. 后记: ...