Understanding of the design pattern
The pattern is not code, but a generic solution for design problems. You can apply them to specific applications, and design patterns tell us how to organize classes and objects and solve some kind of problem.
The need to use design patterns requires a comprehensive understanding of encapsulation inheritance polymorphism, and familiarity with commonly used OO design principles.
The best way to use patterns is to put patterns into your brain and then find out where you can use them in your design and existing applications, formerly code reuse, and now experience reuse .
Policy mode
Definition: The algorithm family is defined, encapsulated separately, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customer using the algorithm.
The following is an example of an actual development that analyzes the strategy pattern, and if there is a requirement, in an action adventure game, there are four game characters, namely: Queen (Queen), King, Troll (troll), Knight (Knight), each character can use different weapons, For example, daggers, bows, axes, swords, each character can use only one weapon at a time, but you can switch weapons during the game.
First analyze the behavior, the role can use weapons, the use of weapons is a common behavior of the role, so first define a Weaponbehavior weapon using interface, there is only one Useweapon () method:
Public Interface Weaponbehavior { publicvoid useweapon ();}
Each character can use a different weapon, so put the specific use of each weapon in a separate class, a class represents a way of using, all of these classes implement the Weaponbehavior interface (this is a key step, we are here to the equivalent of changing the part of the same part of the same).
public class axebehavior implements Weaponbehavior {@Override void Useweapon () { // TODO auto-generated method stub System.out.println ("Implement Axe chop!") "); }}
public class Bowandarrowbehavior Span style= "color: #0000ff;" >implements Weaponbehavior {@Override void Useweapon () { // TODO auto-generated method stub System.out.println ("Implement with bow and arrow! "); }}
public class knifebehavior implements Weaponbehavior {@Override void Useweapon () { // TODO auto-generated method stub System.out.println ("the implementation was assassinated with a dagger!") "); }}
Public class Implements Weaponbehavior { @Override publicvoid useweapon () { // TODO auto-generated Method stub SYSTEM.OUT.PRINTLN ("The realization uses the sword to chop!") "); }}
Next analyze the role, each character will use the weapon to fight so the character has a fight (), and in order to achieve in the game to switch weapons, must have a setweapon (), so here to create a class called character, There is a fight method and a Setweapon method in this abstract class;
Public Abstract class Character { weaponbehavior weaponbehavior; // Note that this is a weapon behavior, not an implementation of a specific behavior. Public Character () { } publicvoid fight () { weaponbehavior.useweapon ( ); } Public void Setweapon (Weaponbehavior wb) { = wb; }}
Then there are each role-specific implementation class, all of which inherit character, and in each role we initialize a weapon (here is the concrete implementation of each weapon).
public class Queen "extends Character { public Queen () {
// TODO auto-generated constructor stub weaponbehavior =new
Knifebehavior (); }}
public class King extends Character { public King () { // TODO auto-generated Constru ctor stub weaponbehavior=new Swordbehavior (); }}
public class Troll "extends Character { public Troll () { // TODO auto-generated Constr Uctor stub weaponbehavior=new Axebehavior (); }}
Public class extends Character { public Knight () { // TODO auto-generated constructor stub New bowandarrowbehavior (); }}
Let's test it.
Public classSimulator { Public Static voidMain (string[] args) {//TODO auto-generated Method StubCharacter queen=NewQueen (); Queen.fight (); Character King=NewKing (); King.fight (); Character Troll=NewTroll (); Troll.fight (); Character Knight=NewKnight (); Knight.fight (); }}
Print results
Achieve with dagger assassination! Achieve with a sword to chop! Achieve with an axe hack chop! Implementation with bow and arrow shooting!
Switch weapons in the game
Public class Simulator { publicstaticvoid main (string[] args) { // TODO auto-generated method stub Character queen=new Queen (); Queen.fight (); Queen.setweapon (new Bowandarrowbehavior ()); Queen.fight (); }}
Print results
Achieve with dagger assassination! Implementation with bow and arrow shooting!
The above is an example of using the policy model, which tells us the following principles:
1. Encapsulate the changed parts (e.g., weapons, or characters that change in the example);
2. Multiple use combinations, less inheritance (do not use inheritance only for reuse purposes);
3. For interface programming, do not program for implementation.
Consider the question: if the above example adds a guard code that can use a dagger, how to modify it (is it possible to find that the original code does not need to be modified ).
Design pattern--Strategy mode