I recently looked at some of the content in the design model and found that my previous capabilities in object-oriented programming are really too watery. There are still many things to learn, from the beginning of this article, I will write down the learned design patterns and attach my own understanding and relevant experiment code.
First of all, let's talk about the simple factory model and try to imagine that a factory in our daily life usually has only the same type of products, such as shoes, so this factory will produce different kinds of shoes, such as leather shoes, canvas shoes, boarding shoes, and running shoes. The customer needs some kind of shoes, and of course it won't make it by itself. It must make the factory produce the shoes, and then the customer will pick up the goods. Then the customer needs to tell the factory which kind of shoes are needed, and the factory will produce them for the customer. This is the idea of the simple factory model. by defining a factory class specifically to create instances of other classes, the created instances usually share a common base class. The roles in the simple factory model and their responsibilities are as follows:
- Factory role: this is the core of the simple factory model and is responsible for creating the internal logic of all classes. Of course, the factory class must be called by the outside world to create the required product objects.
- Abstract Product role: the parent class of all objects created in simple factory mode. Note that the parent class can be an interface or an abstract class, which describes the common public interfaces of all instances.
- Specific product roles: specific instance objects created in a simple factory. These specific products often share a common parent class.
Here we use a simple calculator as an example to implement the addition and subtraction operations. Think about how we operate on a real calculator, such as "3 + 5 = ?", Press "3", then press "+", then press "5", and then press the equal sign to obtain the result. This process should be implemented in the console as follows:
This example is very simple. Anyone who has learned C language can write several lines of code in a main function to implement this function. How can I implement this calculator in the simple factory mode? Let's design it like this:
In this example, the product produced by the factory is an "operation" from the abstract point of view. Therefore, there must be a base class-coperation operation class, which contains a virtual function getresult () it is used to perform operations and return results. Addition and subtraction operations are specific products and are subclasses of Operation classes. They correspond to CADD and csub classes respectively. Their Respective getresult () Operations implement addition and subtraction operations. Then there is the factory class calculationfactory, which defines a static function create (). This function creates a specific object corresponding to the operation type that the customer wants and returns it to the customer, then, the two operands entered by the customer are given to the calculation object to obtain the corresponding calculation result. Therefore, the operations performed on the client are as follows:
First, enter the first operand firstnum, operator plus ('+' or '-'), and second operand secondnum, and then define a coperation type object op through the pointer, it indicates that the object referred to by OP represents an operation, while the CREATE () function of the calculationfactory class generates a specific operation object based on the operator parameter, and assigns the object pointer to op, in this way, op is a pointer to a specific operation object:
COperation* op = CalculationFactory::Create(oper);
After passing the two operands to the operation object through the setter, call its getresult () function to get the operation result that the customer wants:
op->GetResult()
So we will compare the calculator completed in simple factory mode and the calculator implemented directly by putting all the code into the main function. It will be continued...
Complete code is attached:
#include <iostream>using namespace std;class COperation//运算基类{public:COperation():mFirst(0), mSecond(0)//构造函数{}virtual ~COperation(){}virtual double GetResult()//基类运算方法{return 0;}int GetFirst()//第一个数的访问器{return mFirst;}int GetSecond()//第二个数的访问器{return mSecond;}void SetFirst(int _first)//第一个数的设置器{mFirst = _first;}void SetSecond(int _second)//第二个数的设置器{mSecond = _second;}private:int mFirst;//第一个数int mSecond;//第二个数};class CAdd :public COperation//加法类{public:virtual double GetResult()//加法运算{return GetFirst() + GetSecond();}};class CSub :public COperation//减法类{public:virtual double GetResult()//减法运算{return GetFirst() - GetSecond();}};class CalculationFactory//工厂类{public:static COperation* Create(char _operator)//根据运算符产生运算对象{COperation *oper;//COperation类型的指针switch(_operator){case ‘+‘:oper = new CAdd();//指向加法运算对象break;case ‘-‘:oper = new CSub();//指向减法运算对象break;default:oper = new CAdd();//默认加法运算对象break;}return oper;}};int main(){int firstNum, secondNum;char oper;cout << "Please input first number:" << endl;cin >> firstNum;cout << "Please input operator:" << endl;cin >> oper;cout << "Please input second number:" << endl;cin >> secondNum;COperation* op = CalculationFactory::Create(oper);op->SetFirst(firstNum);op->SetSecond(secondNum);cout << "The result is: " << op->GetResult() << endl;delete op;return 0;}
Design Model (I): simple factory Model