First look at the UML class diagram of the Policy mode:
The class diagram shows that there is no difference between the policy mode and the simple factory mode. From my understanding, the two major differences are:The simple factory mode is used to realize the diversity of objects, while the policy mode is suitable for class members to focus on methods. The simple factory mode can only solve the problem of object creation, and the policy mode should be used for frequently changing algorithms.
Let's take a look at the Code:
CPP Code
- // Policy base class
- ClassCoperation
- {
- Public:
- IntM_nfirst;
- IntM_nsecond;
- Virtual DoubleGetresult ()
- {
- DoubleDresult = 0;
- ReturnDresult;
- }
- };
- // Policy-specific class-addition class
- ClassAddoperation:PublicCoperation
- {
- Public:
- Addoperation (IntA,IntB)
- {
- M_nfirst =;
- M_nsecond = B;
- }
- Virtual DoubleGetresult ()
- {
- ReturnM_nfirst + m_nsecond;
- }
- };
- ClassContext
- {
- Private:
- Coperation * op;
- Public:
- Context (coperation * temp)
- {
- OP = temp;
- }
- DoubleGetresult ()
- {
- ReturnOp-> getresult ();
- }
- };
- // Client
- IntMain ()
- {
- IntA, B;
- CharC;
- Cin> A> B;
- Cout <"Enter the operator :;
- Cin> C;
- Switch(C)
- {
- Case'+ ':
- Context * context =NewContext (NewAddoperation (a, B ));
- Cout <context-> getresult () <Endl;
- Break;
- Default:
- Break;
- }
- Return0;
- }
// Policy base class coperation {public: int m_nfirst; int m_nsecond; virtual double getresult () {double dresult = 0; return dresult ;}}; // policy-specific class-addition class addoperation: Public coperation {public: addoperation (int A, int B) {m_nfirst = A; m_nsecond = B;} virtual double getresult () {return m_nfirst + m_nsecond ;}; class context {PRIVATE: coperation * op; public: Context (coperation * temp) {op = temp;} double getresult () {return op-> getresult () ;};// client int main () {int A, B; char C; CIN >>>a> B; cout <"Enter the operator:; CIN> C; Switch (c) {Case '+': Context * context = new context (New addoperation (a, B )); cout <context-> getresult () <Endl; break; default: break;} return 0 ;}
For convenience, I only put an addition class here. You can inherit a subtraction, multiplication, and so on, and then add the relevant classification in the main function switch. Here, we also see the policy MethodDisadvantages: All operations are judged on the client, and the customer's tasks are added.
As we all know, the simple factory model is just to concentrate all the judgment operations on the factory class, so we can think of combining the two models, and the following model emerged-the combination of strategy and factory model, the code is modified based on the above Code:
CPP Code
- ClassContext
- {
- Private:
- Coperation * op;
- Public:
- Context (CharCtype)
- {
- Switch(Ctype)
- {
- Case'+ ':
- OP =NewAddoperation (3, 8 );
- Break;
- Default:
- OP =NewAddoperation ();
- Break;
- }
- }
- DoubleGetresult ()
- {
- ReturnOp-> getresult ();
- }
- };
- // Client
- IntMain ()
- {
- IntA, B;
- Cin> A> B;
- Context * test =NewContext ('+ ');
- Cout <Test-> getresult () <Endl;
- Return0;
- }
Class context {PRIVATE: coperation * op; public: Context (char ctype) {Switch (ctype) {Case '+': op = new addoperation (3,8); break; default: OP = new addoperation (); break ;}} double getresult () {return op-> getresult () ;}; // client int main () {int A, B; cin> A> B; Context * test = new context ('+'); cout <Test-> getresult () <Endl; return 0 ;}