<> One , An introduction to the categories of object patterns - Component collaboration model

There are three modes of component collaboration :
TemplateMethod
Observer(event)
strategy

<> Two , Introduction to current mode -strategy pattern

Strategy model strateg

<> Three , demand

Write a realization of Germany , China , Procedures for calculating different tax laws in the United States .

<> Four , Design one
#include <iostream> using namespace std; enum TAXTYPE { CN_TAX, US_TAX,
DE_TAX, }; class SalesOrder { public: void CalcTax(TAXTYPE type) { if (type ==
CN_TAX) { std::cout << "cn_tax" << std::endl;} else if(type == US_TAX) {
std::cout << "us_tax" << std::endl;} else if(type == DE_TAX) { std::cout <<
"de_tax" << std::endl;} } }; int main() { SalesOrder ob; ob.CalcTax(CN_TAX);
return 0; } // Printing cn_tax
<> Five , Requirement change

( Write a realization of Germany , China , Procedures for calculating different tax laws in the United States .) Add a new one FC_TAX( France ) Tax law treatment

<> Design change version
#include <iostream> using namespace std; enum TAXTYPE { CN_TAX, US_TAX,
DE_TAX, FC_TAX,// change }; class SalesOrder { public: void CalcTax(TAXTYPE type) {
if (type == CN_TAX) { std::cout << "cn_tax" << std::endl;} else if(type ==
US_TAX) { std::cout << "us_tax" << std::endl;} else if(type == DE_TAX) {
std::cout << "de_tax" << std::endl;} else if(type == FC_TAX) { std::cout <<
"fc_tax" << std::endl;} // change } }; int main() { SalesOrder ob;
ob.CalcTax(CN_TAX); return 0; }
<> Violation of principle

1. When adding , Violation of principle 2 Close and open - Closed principle , It should be open to expansion ,SalesOrder Of CalcTax It needs to change , It's a point of change , Change points should only be extended , It's not a new code .

<> Design 2
#include <iostream> using namespace std; class TaxStrategy { public: virtual
~TaxStrategy(){} virtual void CalcTax(int i) = 0; }; class CNTax :public
TaxStrategy { public: virtual ~CNTax(){} virtual void CalcTax(int i){ std::cout
<< "CNTax" << std::endl;}; }; class USTax :public TaxStrategy { public: virtual
~USTax(){} virtual void CalcTax(int i){ std::cout << "USTax" << std::endl;}; };
class DETax :public TaxStrategy { public: virtual ~DETax(){} virtual void
CalcTax(int i){ std::cout << "DETax" << std::endl;}; }; class SalesOrder {
public: void CalcTax(TaxStrategy *type) { type->CalcTax(1); } }; int main() {
CNTax *cn = new CNTax(); SalesOrder order; order.CalcTax(cn); return 0; }
<> Design 2 Revision
#include <iostream> using namespace std; class TaxStrategy { public: virtual
~TaxStrategy(){} virtual void CalcTax(int i) = 0; }; class CNTax :public
TaxStrategy { public: virtual ~CNTax(){} virtual void CalcTax(int i){ std::cout
<< "CNTax" << std::endl;}; }; class USTax :public TaxStrategy { public: virtual
~USTax(){} virtual void CalcTax(int i){ std::cout << "USTax" << std::endl;}; };
class DETax :public TaxStrategy { public: virtual ~DETax(){} virtual void
CalcTax(int i){ std::cout << "DETax" << std::endl;}; }; class FCTax :public
TaxStrategy { public: virtual ~FCTax(){} virtual void CalcTax(int i){ std::cout
<< "FCTax" << std::endl;}; }; class SalesOrder { public: void
CalcTax(TaxStrategy *type) { type->CalcTax(1); } }; int main() { FCTax *cn =
new FCTax(); SalesOrder order; order.CalcTax(cn); return 0; } // Printing FCTax
<> Design two is different from design one

1. difference : New classes only , And don't touch the rest . as :SalesOrder Not moving .
2. When adding , Generally, only one method is placed in this pattern ( The less, the better )

<> Pattern definition

definition : Define a series of algorithms , Package them one by one , And make them interchangeable ( change ). This pattern makes the algorithm independent of the client program that uses it ( stable ) And change ( extend , Subclassification ).

<> Pattern structure

<> Summary of key points

1. of large number if else( perhaps switch case)–》 Generally available strategy Pattern substitution
2. If if else Absolutely unchanged , Don't use it strategy. Like seven days a week .
3. replace if else One reason is that If else A large number of code segments may be loaded , But it's useless .

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network