Strategy Model-design of the Red Guard 2Policy mode: Abstracts different implementations of similar algorithms into algorithm interfaces and implements specific policies respectively. This algorithm can have multiple interfaces. In this way, algorithm changes are independent of algorithm users. The user only uses the policy interface. The following is an example:In Red Alert 2, the major diseases of the United States are as follows: Rocket fighters, scouts, and spatiotemporal fighters. However, they have different ways of moving and attacking.How to design such a weapon, so that users no longer care about specific details, just care about them as soldiers.Ideas:Design an attack interface Attackable, design a mobile interface Moveable, and define an abstract parent class Soldier ). /**
* Attack Interface
*
* @ Author leizhimin 11-12-18 pm
*/
Public interface Attackable {
// Attack
Void attacking ();
}/**
* Mobile Interface
*
* @ Author leizhimin 11-12-18 pm
*/
Public interface Moveable {
Void moving ();
}/**
* Soldier abstract class
*
* @ Author leizhimin 11-12-18 pm
*/
Public abstract class Soldier {
Private Attackable attackable; // attack interface object
Private Moveable moveable; // Mobile Interface object
Public Soldier (Attackable attackable, Moveable moveable ){
This. attackable = attackable;
This. moveable = moveable;
}
// Every soldier will be suspended, screaming and removed from the unit set
Public void dead (){
DoScreech (); // scream
System. out. println ("hung up, removed from the unit queue by the System! ");
}
// All the soldiers will scream before they die. This is a scream and needs to be implemented by specific soldiers.
Abstract void doScreech ();
Public Attackable getAttackable (){
Return attackable;
}
Public Moveable getMoveable (){
Return moveable;
}
} Specific attack methods :/**
* Pistol attack
*
* @ Author leizhimin 11-12-18 pm
*/
Public class HandgunAttack implements Attackable {
Public void attacking (){
System. out. println ("Pistol attack, thank you ~ Region ~ ~~~ ");
}
}/**
* AK47 attack
*
* @ Author leizhimin 11-12-18 pm
*/
Public class Ak47Attack implements Attackable {
Public void attacking (){
System. out. println ("AK47 attack, thank you ......");
}
} Specific moving method :/**
* Flight in the sky
*
* @ Author leizhimin 11-12-18 pm
*/
Public class SkyMove implements Moveable {
Public void moving (){
System. out. println ("flying in the sky! ");
}
}/**
* Land migration
*
* @ Author leizhimin 11-12-18 pm
*/
Public class implements move implements Moveable {
Public void moving (){
System. out. println ("land move! ");
}
}/**
* Land walking or swimming in Water
*
* @ Author leizhimin 11-12-18 pm
*/
Public class walkorsponmove implements Moveable {
Public void moving (){
System. out. println ("Land walking or swimming in water! ");
}
} Then define three types of soldiers :/**
* Ciya
*
* @ Author leizhimin 11-12-18 pm
*/
Public class TanyaSoldier extends Soldier {
Public TanyaSoldier (Attackable attackable, Moveable moveable ){
Super (attackable, moveable );
}
@ Override
Void doScreech (){
System. out. println (" ya) Er ~~~~~ ");
}
}/**
* Flight soldiers
*
* @ Author leizhimin 11-12-18 pm
*/
Public class SkySoldier extends Soldier {
Public SkySoldier (Attackable attackable, Moveable moveable ){
Super (attackable, moveable );
}
@ Override
Void doScreech (){
System. out. println ("(flying soldier) flight --");
}
}/**
* U.S. soldiers
*
* @ Author leizhimin 11-12-18 pm
*/
Public class UsSoldier extends Soldier {
Public UsSoldier (Attackable attackable, Moveable moveable ){
Super (attackable, moveable );
}
@ Override
Void doScreech (){
System. out. println ("(US soldier) Ah ----");
}
} Client test: import java. util. ArrayList;
Import java. util. List;
/**
* Client
*
* @ Author leizhimin 11-12-18 pm
*/
Public class Test {
Public static void main (String [] args ){
// Specific attack Policy
Attackable ak47Attack = new Ak47Attack ();
Attackable handgunAttack = new HandgunAttack ();
// Specific mobile Policy
Moveable move = new move ();
Moveable walkorsponmove = new walkorsponmove ();
Moveable skyMove = new SkyMove ();
// Construct a specific category
Soldier tanyaSoldier = new TanyaSoldier (handgunAttack, walkorw.move );
Soldier usSoldier = new UsSoldier (ak47Attack, please move );
Soldier skySoldier = new SkySoldier (handgunAttack, skyMove );
List <Soldier> soldierList = new ArrayList <Soldier> ();
SoldierList. add (tanyaSoldier );
SoldierList. add (usSoldier );
SoldierList. add (skySoldier );
For (Soldier soldier: soldierList ){
DoSomething (soldier );
}
}
/**
* Do things
*
* @ Param soldier
*/
Public static void doSomething (Soldier soldier ){
System. out. println ("------------------------------------");
Soldier. getMoveable (). moving ();
Soldier. getAttackable (). attacking ();
Soldier. dead ();
}
}------------------------------------
Land walking or swimming in the water!
Small Pistol attack ~ Region ~ ~~~
Proya) Er ~~~~~
Hung up, removed from the unit queue by the system!
------------------------------------
Land walking!
AK47 attack ......
(US soldier) Ah ----
Hung up, removed from the unit queue by the system!
------------------------------------
Flying in the sky!
Small Pistol attack ~ Region ~ ~~~
(Flying soldier) crash --
Hung up, removed from the unit queue by the system!
Process finished with exit code 0 Haha, just for a deep understanding of the policy mode, rough place please also brick.
This article is from the "melyan" blog, please be sure to keep this source http://lavasoft.blog.51cto.com/62575/744903