Design Pattern Notes 22 - State Mode (state)

APP Sweepstakes Problem

Please write a program to complete the APP lottery. The specific requirements are as follows:

1) If 50 points are deducted from the user every time they participate in this activity, the probability of winning is 10%

2) The number of prizes is fixed, and there will be no draw after the draw

3) There are four states in the event: can draw, can not draw, give out prizes and finish receiving prizes

4) Four state transition diagrams of activities

 

 

basic introduction

1) State Pattern: It is mainly used to solve the problem that the object needs to output different behaviors when it transitions between multiple states. There is a one-to-one correspondence between state and behavior, and states can be converted to each other

2) When an object's internal state changes, allowing its behavior to change , the object appears to change its class

 

Principle Class Diagram of State Mode

 

Explanation of the principle class diagram - that is (roles and responsibilities of the state mode)

1) The Context class is an environmental role, used to maintain the State instance, which defines the current state

2) State is an abstract state role, defining an interface to encapsulate behaviors related to a characteristic interface of Context

3) ConcreteState has a specific state role, and each subclass implements a behavior related to a state of Context

 

Core code:

    public static void main(String[] args) {         // TODO Auto-generated method stub         // create activity object, there is 1 prize         RaffleActivity activity = new RaffleActivity(1);


        // We draw 300 prizes in a row
        for (int i = 0; i < 30; i++) {             System.out.println("-------" + (i + 1) + "th draw- ---------");             // Participate in the lottery, the first step is to click to deduct points             activity.debuctMoney();


            // The second step of lottery drawing
            activity.raffle();
        }
    }

And every time the activity.debuctMoney() method is called, the state method is actually called
    //deduct points, call the current state deductMoney
    public void debugMoney(){         state.deductMoney();     }

state.deductMoney() will give an unreasonable implementation method according to the state object:
    // Points have been deducted, and no more points can be deducted
    @Override
    public void deductMoney() {         System.out.println("Points have been deducted");     }

In this way, when the state is changed, the behavior also changes.

 

public static void main(String[] args) {

// TODO Auto-generated method stub

// Create an event object, the prize has 1 prize

RaffleActivity activity = new RaffleActivity(1);

 

// We draw 300 prizes in a row

for (int i = 0; i < 30; i++) {

System.out.println("-------th" + (i + 1) + "time lottery----------");

// Participate in the lottery, the first step is to click to deduct points

activity.debuctMoney();

 

// second step draw

activity.raffle();

}

}

 

And every time the activity.debuctMoney() method is called, the state method is actually called

// Deduct points, call deductMoney in the current state

public void debuctMoney(){

state.deductMoney();

}

 

state.deductMoney() will give unreasonable implementation methods according to the state object:

// Points have been deducted, no more deductions

@Override

public void deductMoney() {

System.out.println("Points have been deducted");

}

 

In this way, when the state is changed, the behavior also changes.

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103306293