实现《大话设计模式》的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++实现实例的更多相关文章

  1. 设计模式 - 工厂模式(factory pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/27081511 工厂模式(factory pa ...

  2. [设计模式]工厂模式factory

    参考:http://wxg6203.iteye.com/blog/740229 简单工厂模式(simple factory)是类的创建模式,又叫静态工厂方法(static factory method ...

  3. 23种设计模式--工厂模式-Factory Pattern

    一.工厂模式的介绍       工厂模式让我们相到的就是工厂,那么生活中的工厂是生产产品的,在代码中的工厂是生产实例的,在直白一点就是生产实例的类,代码中我们常用new关键字,那么这个new出来的实例 ...

  4. 设计模式--工厂模式Factory(创建型)

    工厂模式属于创建型模式,分为三类,简单工厂模式.工厂方法模式.抽象工厂模式. 一.简单工厂模式 在工厂中做判断,根据产品类型从而创造相应的产品,当增加新产品时需要修改工厂类. 例如: enum CTY ...

  5. <大话设计模式>工厂模式,策略模式

    第一章:工厂模式: 通过封装,继承,多态解耦合 业务逻辑和界面逻辑分开 用单独的类创造实例,工厂:创造实例 工厂模式还可以用反射来实现,nsstringFromClass UML类图 聚合表示一众弱的 ...

  6. 大话设计模式--策略模式 strategy -- C++实现实例

    1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...

  7. 设计模式——工厂模式(Factory Method)

    工厂方法模式,定义一个用于创建对象的接口,让子类决定实例化哪个类.工厂方法使一个类的实例化延迟到其子类. UML图: 运算基类: package com.cnblog.clarck; /** * 数据 ...

  8. 大话设计模式--访问者模式 Visitor -- C++实现实例

    1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...

  9. 大话设计模式--解释器模式 interpreter -- C++实现实例

    1. 解释器模式: 给定一个语言,定义它的文法的一种表示 并 定义一个解释器,这个解释器使用该表示文法 来解释语言中的句子. 如果一种特定类型的问题发生的频率很高,那么可能就值得将该问题的各个实例表述 ...

随机推荐

  1. Android的View 事件传递

    欢迎转载,请附出处: http://blog.csdn.net/as02446418/article/details/47422891 1.基础知识 (1) 全部 Touch 事件都被封装成了 Mot ...

  2. 自定义Spring Shell

    目录 概述 自定义内置命令 禁用内置命令 覆盖内置命令 自定义命令提示符 自定义命令行选项行为 自定义参数转换器 概述 官网:https://projects.spring.io/spring-she ...

  3. The best way to predict the future is to invent it,预测未来最好的方法是创造它!

    The best way to predict the future is to invent it,预测未来最好的方法是创造它! ——Smalltalk发明人Alan Kay “预测未来的最好方法, ...

  4. iOS swift objc_setAssociatedObject和objc_getAssociatedObject使用

    oc中的AssociationsManager在swift中也是可以实现的 使用方法请看下面一个例子 import UIKit extension UIButton { func fk_addActi ...

  5. 你不知道的Google Search

    0.导读 这篇文章讲了这三个事儿: 如何訪问Google?----------什么?不是直接输入地址么? Google的地址是什么? ------ 你在逗我?难道不是www.google.com? G ...

  6. C分配struct变量一个不理解的地方

  7. Crashing Robots - poj 2632

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8352   Accepted: 3613 Description In ...

  8. python 写一个类似于top的监控脚本

    最近老板给提出一个需要,项目需求大致如下:      1.用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Serve ...

  9. 异常:The JSP specification requires that an attribute name is preceded by whitespace

    The JSP specification requires that an attribute name is preceded by whitespace: 其实这句话翻译就是 属性后面要必须有空 ...

  10. iostat命令简单使用

    1.iostat使用范围 iostat命令可以生成3种类型的报告: (1)CPU使用情况的报告 (2)设备使用情况的报告 (3)网络文件系统(NFS)使用情况的报告 2.每种报告的格式说明 关于CPU ...