Java Design Pattern Rookie series (I) Modeling and implementation of strategy mode

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39721563


Let's talk about Java design patterns today. Here will be combined with UML graphics to explain, there is not familiar with UML modeling can refer to my other blog post UML modeling.

First, the individual feels that the design of a pattern is a process of separating things from change and unchanging (stable) things. There may be many areas in our application that need to be changed, and the pattern is to "extract" them and "encapsulate" and "implement", so more time we are programming for interfaces. The following is a combination of some specific cases in the book "Head First design mode" and some of my understanding. If you can tell what kind of pattern you are looking at and how you can implement and apply the code, then I think the common design patterns of Java are almost the same.
On the policy model, the Internet has the idea that the policy mode allows the user to choose a method of performing an action, that is, the user can choose different policies to operate. Personally think the strategy mode can use this formula: different XXX has different xxx for the user to choose. For example, different chess pieces have different ways for the user to choose.


Here are some simple examples of different weapons for users to choose from, depending on the characters in the game:

first, UML model diagram

Second, the code implementation
/** * Weapon-template */interface weaponbehavior {void Useweapon ();} Class Knifebehavior implements Weaponbehavior {@Overridepublic void Useweapon () {System.out.println ("Implement with dagger assassination ...");} Class Bowandarrowbehavior implements Weaponbehavior {@Overridepublic void Useweapon () {System.out.println ("Implementation with bow and arrow design ... ");}} Class Axebehavior implements Weaponbehavior {@Overridepublic void Useweapon () {System.out.println ("Implementation with axe hack ...");} Class Swordbehavior implements Weaponbehavior {@Overridepublic void Useweapon () {System.out.println ("Implements wield with sword ...");} /** * Role */abstract class Character {//interface as an abstract role field in order to encapsulate protected weaponbehavior weaponbehavior;public void Setweapon ( Weaponbehavior W) {weaponbehavior = W;} /** * Here is a bit like "proxy mode" */public void Performweapon () {//do something...weaponbehavior.useweapon ();//Do something ...} public abstract void Fight ();} /** * king uses sword to wield */class King extends Character {public King () {weaponbehavior = new Swordbehavior ();} @Overridepublic void Fight () {System.out.println ("King wielding with sword ...");}} /** Queen uses dagger to assassinate */class Queen extends Character {public Queen () {weaponbehavior = new Knifebehavior ();} @Overridepublic void Fight () {System.out.println ("The Queen uses a dagger to assassinate ...");} /** * Knight and Troll and so on, it's not written here. *//** * Client Test * * @author Leo */public class Test {public static void main (string[] args) { Character King = new King (); King.performweapon ();//This is somewhat similar to the "state mode" King.setweapon (new Axebehavior ()); King.performweapon (); Character Queen = new Queen (); Queen.performweapon (); Queen.setweapon (new Bowandarrowbehavior ()); Queen.performweapon ( );}}

third, the application scenario(on behalf of individual views only)game in the role of weapons, board games, such as chess pieces in the way.

Iv. Summary

If there are different opinions or omissions in the above content, please give us some valuable suggestions or comments.

Finally, you can try to implement the following UML model for specific code, such as: attached source



Java Design Pattern Rookie series (I) Modeling and implementation of strategy mode

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.