The strategy pattern defines the algorithm cluster , Package them separately , Let them replace each other , This pattern makes the algorithm changes independent of the user who uses the algorithm .

Friends who like the romance of the Three Kingdoms , They all praised Zhuge Liang as a divine military master . Although in fact , In addition to the teacher's depiction of the image of the dead , Xiao Zhu's scheme is far less profound , He is more of a politician .

Far away , In the romance of the Three Kingdoms , There is such a plot : 

Liu Bei uses Zhou Yu , When Cao Ren is fighting , Chengxu attacked Nanjun , Jingzhou , Xiangyang , Later he conquered Changsha and other four counties . Zhou Yu was very angry when he thought about it , There is no place for revenge to take back Jingzhou . Soon , Liu Bei suddenly lost his spouse , Zhou Yu has a plan , On Sun Quan :“ Your sister , beautiful , Strong , We recruited Liu Bei in the name of marriage against Cao , Trick him to Nanxu for detention , Force them to trade Jingzhou .” Sun Quan's great joy , Lang sent someone to Jingzhou to talk about marriage .

Liu Bei thought it was a hoax , Want to refuse , Zhuge Liang said with a smile :“ Why not send a good wife to your house ? You just go to Dongwu , I'll ask Zhao Yun to accompany you , Own arrangement , I promise you won't lose Jingzhou .” next , Zhuge Liang takes care of Zhao yundao secretly :“ I have three brochures here , There are three tricks in it . Open the first one when you get to Nanxu , Turn on the second by the end of the year , Open the third one when there is no way out ……”

Open different brochures in different scenes , You can save the danger , The brocade bag is the abstract strategy , The strategy in the specific brocade bag is the specific strategic role , The scene is Zhao Yun , The changing situation chooses the concrete strategy condition .

Three key words : Abstract strategy , Specific strategies , Environmental role , The three constitute the focus of our discussion today —— Strategy model .

C++ relatively C The biggest change in language , From process oriented to object-oriented . Process oriented stuff , It's about when and what to do , Step by step, in order . Object oriented is very straightforward , The whole implementation is packaged well , Call it directly when it is used . Process oriented , We love it
if else
To judge , After all, there are three major structures ( order , choice , loop ) one of . obviously , Once the business changes , You have to change a lot of things , This is exactly what programmers don't want to face when maintaining , They like to expand more ——
Open close principle .

If I say , Strategic model can replace if else, Would you shout out in surprise :やだ! in fact , This can be achieved . High energy ahead , Please shine your eyes .

We defined it Enum class ShowType Indicates the type of advertisement to be displayed , And then various types of Controller It is the corresponding policy class
Map<ShowType, Object> controllerMap = new HashMap();
controllerMap.put(ShowType.Banner, createBannerProperties(activity, slotId,
viewGroup, platforms); ...
We define a ShowType by key value ,Controller by value Value of HashMap, Then the corresponding key-value Put it in .
We originally came in according to the information ShowType Returns the corresponding Controller:
Object controller = null; switch (showType) { case Banner: properties =
createBannerProperties(activity, slotId, viewGroup, platforms); controller =
((BannerProperties) properties).getController();break; case Feed: properties =
createFeedProperties(activity, slotId, platforms); controller =
((MMUFeedProperties) properties).getController();break; case Insert: properties
= createInsertProperties(activity, slotId, platforms); controller =
((InsertProperties) properties).getController();break; case LoopImage:
properties = createLoopImageProperties(activity, slotId, viewGroup, platforms);
break; case Welcome: properties = createWelcomeProperties(activity, slotId,
viewGroup, platforms); controller = ((WelcomeProperties)
properties).getController();break; default: break; }
This is our original implementation , Now I have this HashMap after , The code can be modified in this way :
Controller controller = creatorMap.get(type);
There are associated containers in the data structure :map, Understand type as key, The strategy is value The words of , You will know that the execution efficiency of the strategy mode is better than that of the policy mode if else, How powerful is it .

more C++ Sample code , Please move to my own github.

Design patterns , May you write your code as art .

6/9/2017 11:41:34 AM

Technology
©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function