C + + rewrites pattern example 4 (strategy pattern) in Dahua design pattern

(statement: if you want to see the detailed analysis of the example, please see "Dahua design pattern". Here, the article is just to deepen the impression of learning design pattern and write it with C + + program and share the code with you. It's just to replace c language with C + + expression, not to make personal expression on the programs and examples in the book.)

In fact, the policy mode is very similar to the simple factory mode. One obvious difference is that the "judgment" of the initialization class is placed on the client. In this way, when adding a policy subclass, only the client needs to be modified, and no other classes need to be modified. However, it has the disadvantage of exposing more classes and details to the client.

Please refer to my simple factory model article: https://blog.csdn.net/qq_34784043/article/details/82925158

UML diagram:

CashSuper.h

#pragma once
#include <iostream>

//Abstract class of cash charge
class CashSuper {
public:
	virtual double acceptCash(double money) = 0;
};

//Normal charge subclass
class CashNormal :public CashSuper {
public:
	virtual double acceptCash(double money) {
		return money;
	}
};

//Discount charge subclass
class CashRebate :public CashSuper {
private:
	double moneyRebate;
public:
	CashRebate(double moneyRebate) {
		this->moneyRebate = moneyRebate;
	}
	virtual double acceptCash(double money) {
		return money;
	}
};

//Rebate charge sub category
class CashReturn :public CashSuper {
private:
	double moneyCondition;
	double moneyReturn;
public:
	CashReturn(double moneyCondition, double moneyReturn) {
		this->moneyCondition = moneyCondition;
		this->moneyReturn = moneyReturn;
	}
	virtual double acceptCash(double money) {
		double result = money;
		if (money >= moneyCondition) {
			result = money - int(money / moneyCondition)*moneyReturn;
		}
		return result;
	}
};

CashContent.h:

#pragma once
#include "CashSuper.h"

class CashContext {
private:
	CashSuper *cs;
public:
	//Transfer in specific charging strategies
	CashContext(CashSuper *csuper) {
		cs = csuper;
	}
	~CashContext() { delete cs; }
	//Get the calculation results
	double GetResult(double money) {
		return cs->acceptCash(money);
	}
};

main.cpp:

#include "CashContext.h"
#include <cstdlib>

int main() {
	double total = 0;//For total
	CashContext *cc = NULL;
	enum  Stragy { Normal charge, 100 after 300, Hit 20 percent off }stragy;
	stragy = 100 after 300;
	char* str = "";
	switch (stragy)//Select corresponding strategy
	{
	case Normal charge:
		str = "Normal charge";
		cc = new CashContext(new CashNormal);
		break;
	case 100 after 300:
		str = "100 after 300";
		cc = new CashContext(new CashReturn(300, 100));
		break;
	case Hit 20 percent off:
		str = "Hit 20 percent off";
		cc = new CashContext(new CashRebate(0.8));
		break;
	default:
		break;
	}
	double totalPrices = 0;
	double Price = 500;//commodity price
	int count = 2;//Quantity of goods
	totalPrices = cc->GetResult(Price*count);//Original price 500
	total = total + totalPrices;
	std::cout << "Unit Price:" << Price << " number:" << count << " " << str << " total:" << totalPrices << std::endl;
	delete cc;
	system("pause");
	return 0;
}

Operation result:

Keywords: C

Added by hucklebezzer on Sat, 21 Dec 2019 16:41:28 +0200