设计模式:可复用面向对象软件的基础 书中对 Strategy 模式的定义如下:

定义了一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于它的用户而变化。

案例:设计一个商场收银软件,营业员根据客户所购买商品的单价和数量,向客户收费。商场有许多促销的策略,打折,满300减100等。。。

策略模式可以减少各种算法类与使用算法类之间的耦合;

简化单元测试,因为每个算法都有自己的类,可以通过自己的接口进行单元测试。

注意: 下面代码中用一个 CashContext 的 智能指针类来保存 基类的指针。。

#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cassert> /* abstract base class
* define interface
*/
class CashSuper
{
public:
virtual ~CashSuper() {}
virtual double acceptCash(double money) = 0;
}; class CashNormal : public CashSuper
{
public:
double acceptCash(double money){
return money;
}
}; class CashRebate : public CashSuper{
public:
CashRebate(std::string moneyRate = "0.0"){
std::stringstream s(moneyRate);
s >> this->moneyRate_;
} double acceptCash(double money){
return money * moneyRate_;
}
private:
double moneyRate_;
}; class CashReturn : public CashSuper{
public:
CashReturn(std::string moneyCondition, std::string moneyReturn){
std::stringstream s(moneyCondition);
s >> this->moneyCondition_;
std::stringstream s2(moneyReturn);
s2 >> this->moneyReturn_;
} double acceptCash(double money){
if(money >= moneyCondition_){
money -= std::floor(money / moneyCondition_) * moneyReturn_;
}
return money;
}
private:
double moneyCondition_, moneyReturn_;
}; /* handler class
* smart pointer
*/
class CashContext{
public:
CashContext(std::string type)
: pCashSuper_(NULL), use(new std::size_t(1))
{
if(type == "正常收费"){
pCashSuper_ = new CashNormal;
}
else if(type == "满300减100"){
pCashSuper_ = new CashReturn("300", "100");
}
else if(type == "打8折"){
pCashSuper_ = new CashRebate("0.8");
}
// else
} // copy control
CashContext(const CashContext &rhs)
: pCashSuper_(rhs.pCashSuper_), use(rhs.use)
{
++ *use;
}
// deal with self-assignment
CashContext& operator=(const CashContext &rhs){
++ *(rhs.use);
if(-- *use == 0){
delete pCashSuper_;
delete use;
}
pCashSuper_ = rhs.pCashSuper_;
use = rhs.use;
return *this;
} ~CashContext(){
if(-- *use == 0){
delete pCashSuper_;
delete use;
}
} double GetResult(double money){
assert(pCashSuper_ != NULL);
return pCashSuper_->acceptCash(money);
}
private:
CashSuper *pCashSuper_;
std::size_t *use;
};

参考资料:

1. 大话设计模式

2. 设计模式:可复用面向对象软件的基础

Design Patterns---- Strategy 模式的更多相关文章

  1. Java基础学习总结(37)——Java23中设计模式(Design Patterns)详解

    设计模式(Design Patterns) --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  2. Design Patterns in Smalltalk MVC 在Smalltalk的MVC设计模式

    Design Patterns in Smalltalk    MVC在Smalltalk的MVC设计模式 The Model/View/Controller (MVC) triad ofclasse ...

  3. 设计模式(Design Patterns)Java版

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  4. Groovy 设计模式 -- Strategy 模式

    策略模式 https://en.wikipedia.org/wiki/Strategy_pattern In computer programming, the strategy pattern (a ...

  5. 设计模式C++实现(1)——策略(Strategy)模式

    目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...

  6. 图书-软件架构:《Design Patterns: Elements of Reusable Object-Oriented Software》(即后述《设计模式》一书)

    ylbtech-图书-软件架构:<Design Patterns: Elements of Reusable Object-Oriented Software>(即后述<设计模式&g ...

  7. Design Patterns in Android

    对日常在 Android 中实用设计模式进行一下梳理和总结,文中参考了一些网站和大佬的博客,如 MichaelX(xiong_it) .菜鸟教程.四月葡萄.IAM四十二等,在这里注明下~另外强烈推荐图 ...

  8. 【Design Patterns】(1)概述

    设计模式 -- 概述 2019-07-17  22:43:32  by冲冲 1. 简介 ① 设计模式 是软件开发人员在软件开发过程中,针对一般问题的最佳解决方案,该方案能够被程序员反复应用于解决类似问 ...

  9. .NET Best Practices: Architecture & Design Patterns (5 Days Training)

    .NET Best Practices: Architecture & Design Patterns (5 Days Training) .NET最佳实践:架构及设计模式 5天培训课程 课程 ...

  10. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

随机推荐

  1. JS获取页面元素并修改

    //实现代码如下,非常简单<script> (function(){ var ele = document.getElementsByTagName("ul"); // ...

  2. ASP.NET中EVAL用法大全

    <%# Bind("Subject") %> //绑定字段<%# Container.DataItemIndex + 1%> //实现自动编号<%# ...

  3. Populating Next Right Pointers in Each Node II [Leetcode]

    Problem Description http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ ...

  4. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

  5. 【转】数据库范式(1NF 2NF 3NF BCNF)详解二

    以下内容转自:http://jacki6.iteye.com/blog/774889 -------------------------分割线----------------------------- ...

  6. Windows server2003 + sql server2005 集群配置安装

    http://blog.itpub.net/29500582/viewspace-1249319/

  7. SQL 修改数据库架构名

    SQl 修改数据库架构名 declare @name sysname declare csr1 cursor for select TABLE_NAME from INFORMATION_SCHEMA ...

  8. [转] jQuery源码分析-如何做jQuery源码分析

    jQuery源码分析系列(持续更新) jQuery的源码有些晦涩难懂,本文分享一些我看源码的方法,每一个模块我基本按照这样的顺序去学习. 当我读到难度的书或者源码时,会和<如何阅读一本书> ...

  9. 完美实现自己的GetProcAddress函数(转载)

    我们知道kernel32.dll里有一个GetProcAddress函数,可以找到模块中的函数地址,函数原型是这样的: WINBASEAPI FARPROC WINAPI GetProcAddress ...

  10. admob 广告Android不显示

    我弄个了ane,iOS上好好的,Android上打死不显示, 最后发现是少在xml里面增加一个配置,百度半天没搜索到. <android>         <manifestAddi ...