From the design mode to see the glory of the King (VI. State mode)

See design mode (Status Mode) from the king of glory

I. Introduction

Three different states hero Xiang Yu in the presence of an enemy hero attacks.
1. In the health value of life - ordinary state, each time it is attacked, the current value of life = residual value of life - an enemy hero hurt the value of
2. life is lower than a certain value - passive state, each time it is attacked, the current value of life = residual value of life - an enemy hero damage value / 2
3. hero Xiang Yu life is lower than 0-- death state

II. Status Mode

Status Mode: also known as state of the object pattern (Pattern of Objects for States), the state of behavior pattern model object, the state of an object model allows changes in its internal state to change its behavior when. This object looks like it has changed the class

  • Among these design principles are:
  1. State mode to study the behavior of an object wrapped in a different state objects where each object belongs to a state of a subclass of the abstract class status.
  2. The intention is to make the state a model object changes in its internal state when its behavior also changed
  3. State role model involved are:

    Environment (Context) role, as is also the context: the interface definition client is interested in, and retain a particular instance of the state class. Examples of this particular class is given the state of the current state of the object in this environment.
    Abstract state (State) Roles: defining an interface for a particular package environment status (Context) corresponding to the target behavior.
    Specific state (ConcreteState) role: every concrete state classes implement the environment (Context) in a state corresponding behavior.

  • State pattern advantages:
  1. Interface-oriented programming, the implementation details cleverly packaged in various classes state, state transitions to the state classes themselves to achieve, without concern outside;
  2. By a large number of operations, the removal of a large number of code determination logic, the internal state of the associated logic implemented by the class of transition state, better readability;
  • Mode disadvantage combined mode
  1. After the state will increase when adding a new class of state, but also in adding new state class, environmental class needs to be modified accordingly, not in line with the principle of opening and closing

III. Structure Figure

Structure chart

IV. FIG design class

Class Diagram

V. code implementation

LifeState class (abstract state class)

package com.game.LifeState;
import com.game.LifeManager.LifeManager;
/*
 * 英雄项羽的游戏状态接口
 * @param life 当前生命值
 * @param lifeManager 状态上下文,用来在实现状态对应的功能处理的时候,可以回调上下文的数据
 */

public interface LifeState {
    //英雄项羽被攻击的状态
    public abstract void beAttacted(int life, LifeManager lifeManager);
}

NormalBeAttackedState class (class specific state) - normal health state

package com.game.LifeState.impl;

import com.game.LifeManager.LifeManager;
import com.game.LifeState.LifeState;
/*
 * 英雄项羽在健康生命值下被攻击的状态
 */

public class NormalBeAttackedState implements LifeState {
    public void beAttacted(int life, LifeManager lifeManager) {
        //当前生命值=之前生命值-敌方英雄的攻击力
        int newLife=lifeManager.getLife()-lifeManager.getHeroHurt();
        System.out.print("英雄项羽正受到"+lifeManager.heroHurt+"点伤害,");
        System.out.println("当前生命值为"+newLife);
        //设置当前生命值
        lifeManager.setLife(newLife);
    }
}

The passive state - AttactedUnderPassiveState class (class specific state)

package com.game.LifeState.impl;

import com.game.LifeManager.LifeManager;
import com.game.LifeState.LifeState;
/*
 * 当项羽生命值小于500,激发被动状态,所受伤害减半
 */

public class AttactedUnderPassiveState implements LifeState {

    public void beAttacted(int life, LifeManager lifeManager) {
        //设置在被动下项羽所受伤害值
        int newHeroHurt=lifeManager.getHeroHurt()/2;
        //设置被动状态下遭受攻击后当前血量
        int newLife=lifeManager.getLife()-newHeroHurt;
        //判断:在被动状态下如果生命值低于0,英雄死亡
        if(newLife>0) {
            System.out.println("英雄项羽激发被动陷阵之志,所受伤害减半");
            System.out.print("英雄项羽正受到"+newHeroHurt+"点受害,");
            System.out.println("当前的生命值为:"+newLife);
            lifeManager.setLife(newLife);
        }else {
            System.out.println("英雄项羽已经死亡");
        }
    }

}

dieState class (state specific class) - the state of death

package com.game.LifeState.impl;

import com.game.LifeManager.LifeManager;
import com.game.LifeState.LifeState;
/*
 * 英雄死亡状态
 */

public class dieState implements LifeState {

    public void beAttacted(int life, LifeManager lifeManager) {
        System.out.println("英雄项羽已经死亡");
    }

}

LifeManager category (Environmental)

package com.game.LifeManager;

import com.game.LifeState.LifeState;
import com.game.LifeState.impl.AttactedUnderPassiveState;
import com.game.LifeState.impl.NormalBeAttackedState;
import com.game.LifeState.impl.dieState;

/*
 * 环境类
 */

public class LifeManager {
    //持有状体处理对象
    private LifeState state = null;
    //项羽的初始生命值
    private int life=1000;
    //项羽收到敌方攻击的伤害值
    public int heroHurt=0;
    
    //构造方法初始化伤害值
    public LifeManager(int heroHurt) {
        this.heroHurt=heroHurt;
    }

    //get,set
    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }
    
    public int getHeroHurt() {
        return heroHurt;
    }

    public void setHeroHurt(int heroHurt) {
        this.heroHurt = heroHurt;
    }

    public void beAttacked() {
        if(life>500) {
            state=new NormalBeAttackedState();
        }else if(life>0 && life<=500) {
            state=new AttactedUnderPassiveState();
        }else if(life<=0) {
            state=new dieState();
        }
        //转调状态对象执行相应操作
        state.beAttacted(life, this);
    }   
}

LifeManagerTest class (class test)

package com.game.test;

import com.game.LifeManager.LifeManager;
/*
 * 测试类
 */

public class LifeManagerTest {
    public static void main(String [] args) {
        //设置项羽受到每次126的伤害
        LifeManager lm=new LifeManager(126);
        
        //设置项羽被相应伤害值攻击的次数
        for(int i=0;i<15;i++) {
            lm.beAttacked();
        }
    }
}

operation result
result

Guess you like

Origin www.cnblogs.com/miaowulj/p/11458635.html