Java design patterns cainiao series (I) Modeling and implementation of policy patterns
Reprinted please indicate the source: http://blog.csdn.net/lhy_ycu/article/details/39721563
Let's start to talk about Java design patterns today. Here we will explain it with uml graphics. If you are not familiar with uml modeling, refer to another blog.Uml modeling.
First of all, I personally think that the pattern design is a process of separating changing things from unchanged (stable) things. There may be many things that need to be changed in our applications. What we need to do is to extract them and encapsulate them and implement them ", therefore, we are more interested in interface-oriented programming. The following describes some specific cases in the book Head First design patterns and my understanding. If you can figure out which mode can be used to implement and apply specific code, and vice versa, so I want you to know about the common design patterns of Java.
There is a saying on the Internet about the policy mode: the Policy mode allows users to select a method to execute an action, that is, users can select different policies for operations. I personally think this formula can be used for the policy mode: Different XXX has different XXX for users to choose from. For example, different chess pieces have different ways for users to choose.
The following is a simple case where different players in the game have different weapons to choose from:
I. UML Model Diagram
Ii. Code Implementation
/*** Weapons -- template */interface WeaponBehavior {void useWeapon ();} class KnifeBehavior implements WeaponBehavior {@ Overridepublic void useWeapon () {System. out. println ("implement the assassination with a dagger... ") ;}} class BowAndArrowBehavior implements WeaponBehavior {@ Overridepublic void useWeapon () {System. out. println ("implement bow and arrow design... ") ;}} class AxeBehavior implements WeaponBehavior {@ Overridepublic void useWeapon () {System. out. println ("achieve cutting with an ax... ") ;}} class SwordBehavior implements WeaponBehavior {@ Overridepublic void useWeapon () {System. out. println ("realize waving with a sword... ") ;}}/*** role */abstract class Character {// use the interface as the Field of the abstract role to encapsulate protected WeaponBehavior weaponBehavior; public void setWeapon (WeaponBehavior w) {weaponBehavior = w;}/*** here is a bit similar to "proxy mode" */public void extends mweapon () {// do something... weaponBehavior. useWeapon (); // do something ...} public abstract void fight ();}/*** the King waved with the sword */class King extends Character {public King () {weaponBehavior = new SwordBehavior ();} @ Overridepublic void fight () {System. out. println ("the King waved with a sword... ") ;}}/*** the Queen used the dagger to kill */class Queen extends Character {public Queen () {weaponBehavior = new KnifeBehavior () ;}@ Overridepublic void fight () {System. out. println ("the Queen stabbed him with a dagger... ") ;}}/*** Knight and Troll, and so on, here, we will not write * // *** client Test ** @ author Leo */public class Test {public static void main (String [] args) {Character king = new King (); king. extends mweapon (); // here it is a bit similar to "status mode" king. setWeapon (new AxeBehavior (); king. optional mweapon (); Character queen = new Queen (); queen. optional mweapon (); queen. setWeapon (new BowAndArrowBehavior (); queen. extends mweapon ();}}
Iii. application scenarios (personal opinions only) role weapons in games, chess and chess moves, etc.
Iv. Summary
If you have different or negligent opinions on the above content, please provide valuable suggestions or comments.
Finally, you can try to implement the specific code of the following uml model, for example:Source code