Policy mode (strategy pattern)
Aside from the obscure definition, first look at an example:
We want to create a game that simulates ducks, and in this game there will be various types of ducks, such as Mallard duck,red head Duck,rubber duck (except rubber duck (rubber duck), see the other two ducks are curious, so look for related pictures, Found Mallard Duck is green-headed duck, red head Duck is a duck, his life has not seen, fun, haha! Three kinds of duck pictures are shown below).
Back to the topic, in this simulated duck game, all kinds of ducks have two abilities, that is, swimming (swim) and quack (quack). According to the above description, the class design.
Assuming we had not learned the strategy pattern before, we would certainly design a base class duck with swim and quack attributes, and then Mallard Duck and red head duck inherit the base class duck. The specific class diagram is as follows:
The following code can be written according to this type of design diagram:
1#include <iostream>2 using namespacestd;3 4 classDuck {5 Public:6 voidQuack () {cout <<"I ' m quacking!"<<Endl;} 7 voidSwim () {cout <<"I ' m swimming!"<<Endl;} 8 voiddisplay ();9 };Ten One classMallardduck: PublicDuck { A Public: - voiddisplay () { -cout <<"This is Mallard Duck:"<<Endl; the Quack (); - swim (); - } - }; + classRedheadduck: PublicDuck { - Public: + voiddisplay () { Acout <<"This is Red Head Duck:"<<Endl; at Quack (); - swim (); - } - }; - classRubberduck: PublicDuck { - Public: in voiddisplay () { -cout <<"This is Rubber Duck:"<<Endl; to Quack (); + swim (); - } the }; * $ Panax Notoginseng - the intMain () { + Mallardduck Mallard_duck; A Redheadduck Redhead_duck; the Rubberduck Rubber_duck; + Mallard_duck.display (); - Redhead_duck.display (); $ Rubber_duck.display (); $System"Pause"); - return 0; -}View Code
Most people will think of the above design method, this design way in the future expansion will encounter problems, if you want to add a fly function in this simulation duck game, then we may easily add a fly function in the base class duck, then the different kinds of ducks inherit this base class, carry out the flight, The changed class diagram is as follows:
Corresponding Code:
1#include <iostream>2 using namespacestd;3 4 classDuck {5 Public:6 voidQuack () {cout <<"I ' m quacking!"<<Endl;} 7 voidSwim () {cout <<"I ' m swimming!"<<Endl;} 8 voidFly () {cout <<"I ' m flying!"<<Endl;}9 voiddisplay ();Ten }; One A classMallardduck: PublicDuck { - Public: - voiddisplay () { thecout <<"This is Mallard Duck:"<<Endl; - Quack (); - swim (); - fly (); + } - }; + classRedheadduck: PublicDuck { A Public: at voiddisplay () { -cout <<"This is Red Head Duck:"<<Endl; - Quack (); - swim (); - fly (); - } in }; - classRubberduck: PublicDuck { to Public: + voiddisplay () { -cout <<"This is Rubber Duck:"<<Endl; the Quack (); * swim (); $ fly ();Panax Notoginseng } - }; the + A the + intMain () { - Mallardduck Mallard_duck; $ Redheadduck Redhead_duck; $ Rubberduck Rubber_duck; - Mallard_duck.display (); - Redhead_duck.display (); the Rubber_duck.display (); -System"Pause");Wuyi return 0; the}View Code
Program results:
At this time found a problem, Rubber Duck (Rubber duck) Incredibly can fly, we do not want to let rubber ducks fly, how to do it? We can cover the fly in the rubber duck class, and we don't do anything.
I'm tired this evening, I'll take a rest ...
Policy mode (strategy pattern)