Design Patterns - State Patterns

State Pattern : Allows an object to change its behavior when its internal state changes, and the object appears to modify its class. Its alias is Objects for States , and the state pattern is an object behavior pattern.

       ●  Environment class: An environment class, also known as a context class, is an object with multiple states. Since the state of the environment class is diverse and the behavior of objects in different states is different, the states are separated out to form a separate state class. An instance of the abstract state class State is maintained in the environment class . This instance defines the current state. In concrete implementation, it is anobject of a subclass of State .

       ●  Abstract state class: It is used to define an interface to encapsulate the behavior related to a specific state of the environment class, declare methods corresponding to various states in the abstract state class, and implement these methods in its subclasses, Since the behavior of objects in different states may be different, the implementation of methods in different subclasses may be different, and the same method can be written in the abstract state class.

       ●  Concrete state class: It is a subclass of the abstract state class. Each subclass implements a behavior related to a state of the environment class. Each concrete state class corresponds to a specific state of the environment. The behaviors of different concrete state classes aredifferent.


Environment Class: Player

class Player {

    private String name;

    private Integer jf = 0;

    private playerstate ps;

    public Player(String name,Integer jf) {
        this.name = name;
        this.jf = jf;
        this.ps = new Primary(this);
    }

    public Integer getJf() {
        return jf;
    }

    public void setJf(Integer jf) {
        this.jf = jf;
    }

    public playerstate getPs() {
        return ps;
    }

    public void setPs(playerstate ps) {
        this.ps = ps;
    }
}


abstract state class

abstract class playerstate{

    public playerstate(Player player) {
        this.player = player;
    }

    protected Player player;

    abstract void stateCheck();

    public void victory(){
        player.setJf(player.getJf() + 10);
        stateCheck ();
    }

    public void failure(){
        player.setJf(player.getJf() - 10);
        stateCheck ();
    }

    public void doubleScore(){
        System.out.println("No points doubled");
    }

    public void changeCards(){
        System.out.println("No card changing function");
    }

}


Concrete state class

class Primary extends  playerstate{

    public Primary(Player player) {
        super(player);
    }

    public void play(){
         System.out.println("Basic Game Function");
     }

    @Override
    void stateCheck() {
        if(player.getJf()>10){
            player.setPs(new Secondary(player));
        }
    }
}
class Secondary extends playerstate{

    public Secondary(Player player) {
        super(player);
    }

    public void play(){
        System.out.println("Basic Game Function");
    }

    @Override
    public void doubleScore(){
        System.out.println("Double game points");
    }

    @Override
    void stateCheck() {
        if(player.getJf()>20){
            player.setPs(new Professional(player));
        }else if(player.getJf()<=10){
            player.setPs(new Primary(player));
        }
    }
}
class Professional extends playerstate{

    public Professional(Player player) {
        super(player);
    }

    public void play(){
        System.out.println("Basic Game Function");
    }

    @Override
    public void doubleScore(){
        System.out.println("Double game points");
    }

    @Override
    public void changeCards(){
        System.out.println("Change card function");
    }

    @Override
    void stateCheck() {
        if(player.getJf()<=20){
            player.setPs(new Secondary(player));
        }
    }
}


main

class main{
    public static void main(String[] args) {
        Player pl1 = new Player("p1",0);
        pl1.getPs().victory();
        System.out.println(pl1.getPs() + pl1.getJf().toString());
        pl1.getPs().doubleScore();
        pl1.getPs().victory();
        System.out.println(pl1.getPs() + pl1.getJf().toString());
        pl1.getPs().doubleScore();
        pl1.getPs().victory();
        System.out.println(pl1.getPs() + pl1.getJf().toString());
        pl1.getPs().failure();
        System.out.println(pl1.getPs() + pl1.getJf().toString());
        pl1.getPs().failure();
        System.out.println(pl1.getPs() + pl1.getJf().toString());

    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852250&siteId=291194637