Java design Pattern-Strategy Pattern and strategypattern

Source: Internet
Author: User

Java design Pattern-Strategy Pattern and strategypattern

Rule Mode: Defines the algorithm family and encapsulates them separately so that they can replace each other. This pattern makes the algorithm changes independent of the customers who use the algorithm.

No matter what the definition is, let's look at an example first. Suppose you want to design a person (including a player and an NPC) in the game. How should you design it? As we know, a game character must have basic attributes, actions (such as the blood volume attribute and walking actions), be able to use different weapons and replace different equipment ...... how to Design elastically. Here, we can use the policy mode.

Design principles1: Identify potential changes in the application and separate them from those that do not change..Here, we can see that game players can use different weapons, be able to change different equipment, belong to the changed part, can come out independently, and the walking action is a constant part, it does not need to be independent. It can be implemented as a class first.

Basic character code:

public class GamePerson{    private int bloodValue;    public GamePerson(int value){        this.bloodValue=value;    }    public int getBloodValue(){        return bloodValue;    }        private void move(){        System.out.println("I'm moving");    }}    

  

Now we are thinking about how to achieve the realization of equipment, weapons, and so on, so that they can be flexible and dynamically changed during the game. Therefore, you needDesign principles2: Programming for interfaces, rather than implementing programming.Each action is represented by an interface, such as EquipmentInterface and WeaponInterface. You do not need GamePerson to implement these two interfaces. Instead, you can create a specific installation class and weapon class to implement their own interfaces. For example, ClothingEquipment, WristEquipment, SwordWeapton, and GunWeapton.

The Code is as follows:

Equipment interface:

public interface EquipmentInterface{    void equipment();}

Clothing and Equipment:

public class ClothingEquipment implement EquipmentInterface{    public void equipment(){         Systemout.out.println("I'm clothing");           }  }

Wrist Equipment

public class WristEquipment implement EquipmentInterface{    public void equipment(){         Systemout.out.println("I'm wrist");           }  }

Weapon Interface

public interface WeaptonInterface{      void weapton();  }

Sword weapons

public class SwordWeapton implement WeaptonInterface{     void weapton(){        System.out.println("I'm sword");    }  }

Gun and weapon

public class GunWeapton implement WeaptonInterface{     void weapton(){        System.out.println("I'm gun");    }  }

Well, we need to combine the two. Specifically, WeaptonInterface and EquipmentInterface must be added to game characters. As follows:

public class GamePerson{    private int bloodValue;
WeaptonInterface weapton;
   EquipmentInterfac equipment;
    public void setWeapton(WeaptonInterface w){        this.weapton = w;    }
    public void setEquipment(EquipmentInterface w){        this.equipment = w;    }
    public GamePerson(int value,WeaptonInterface w,EquipmentInterface e){        this.bloodValue=value;
     this.weapton = w;
     this.equipment =e;
} public int getBloodValue(){ return bloodValue; } private void move(){ System.out.println("I'm moving"); }}

Therefore, the specific handler class can inherit GamePerson, such:

public class NPCPerson extends GamePerson{    public void NPCPerson(int b,WeaptonInterface w,EquipmentInterface e){      super(b,w,e);          }      }

Test:

public class TestApp{    public static void main(String[] args){        NPCPerson p = new NPCPerson(100,new SwordWeapton(),new WristWeapton());        p.move();        p.setWeapton(new GunWeapton());     }}

RememberDesign Principle 3: multi-purpose combination, less inheritance.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.