Java design pattern: analyzing State pattern from [transition of weapons in Game]

Source: Internet
Author: User

Suppose we are playing an instant strategy game. We design a soldier. He was an infantry when he was just produced, but he can switch weapons. The first switch will become an archer, the second switch will become an armored soldier holding a shield, and the third switch will become an infantry ...... How to implement this switching mechanism? At the beginning, we will think that adding a switch statement to the infantry class is not conducive to code expansion and modification, so we can use the state mode.

The State mode allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

In this example, the troops are in three states: infantry, Archer, and armored. We inherit these three states from the same base class SoldierState. We also need to define a category to contain a SoldierState object so that it is used to store these states, in this example, the class name is KnightContext. Let's look at the Java code first:

Abstract class SoldierState {public abstract void show (); protected void changeState (KnightContext knight, SoldierState soldierState) {knight. changeState (soldierState);} public abstract void transform (KnightContext knight);} class KnightContext {SoldierState state; public KnightContext () {state = SwordState. getState ();} public void changeState (SoldierState state) {this. state = state; state. show ();} Public void transform () {state. transform (this) ;}} class SwordState extends SoldierState {private static SoldierState state = new SwordState (); public static SoldierState getState () {return state;} public void show () {System. out. println ("the current is the infantry mode. ");} Public void transform (KnightContext knight) {changeState (knight, BowState. getState () ;}} class BowState extends SoldierState {private static SoldierState state = new BowState (); public static SoldierState getState () {return state;} public void show () {System. out. println ("the current is the bow and arrow mode. ");} Public void transform (KnightContext knight) {changeState (knight, ShieldState. getState () ;}} class ShieldState extends SoldierState {private static SoldierState state = new ShieldState (); public static SoldierState getState () {return state;} public void show () {System. out. println ("the current is the shield mode. ");} Public void transform (KnightContext knight) {changeState (knight, SwordState. getState () ;}} public class State {public static void main (String [] args) {KnightContext knight = new KnightContext (); knight. transform (); knight. transform (); knight. transform (); knight. transform (); knight. transform ();}

SwordState, BowState, and ShieldState. There is a SoldierState Member state in KnightContext, which indicates the current state of the weapon. Please note that we do not want to write the switching process in the KnightContext (the general idea is to add the switch block to changeState, which will make the KnightContext too heavy ), instead, it writes the transformation in the transform Method of SoldierState. This avoids adding a lot of code to the KnightContext to switch the status. To add a new state, you only need to modify and add the SoldierState subclass. Each status is represented by a separate subclass. Therefore, when there are many statuses, many subclasses will appear, which is also a disadvantage of the Status mode.

The program runs as follows:

This is the bow and arrow mode.

This is the shield mode.

Now it is the infantry mode.

This is the bow and arrow mode.

This is the shield mode.

The above is the description of the Status mode, hoping to help you.

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.