PHP State Mode Learning Notes

Source: Internet
Author: User

If, according to General thinking, every time we operate on a pawn, such as a tank, we have to use if to judge his state, so there will be a lot of if,else or Swith in the code.


But we can see that what we need is his behavior in a certain state, and if we encapsulate these behaviors in a state, we can reduce a lot of judgment.


The problem to be solved: encapsulate the state of the tank and let the state control its behavior.


Thinking: The state as the attribute, the soldier kind itself only controls the change of state, the concrete behavior is defined by the State class.


Status (State) mode example:

The code is as follows Copy Code

<?php

interface of the tank state

Interface Tankstate

{

The attack method of the tank

Public function attack ();

}

Tank Normal state

Class Tankstate_tank implements Tankstate

{

The attack method of the tank

Public Function Attack ()

{

Here the simple output current state

echo "Ordinary state";

}

}

The state of the tank's frame

Class Tankstate_siege implements Tankstate

{

The attack method of the tank

Public Function Attack ()

{

Here the simple output current state

echo "stands up";

}

}

Tank class

Class Tank

{

State

Public $state;

The attack method of the tank

Public Function __construct ()

{

The newly built tank is, of course, a normal state.

$this->state = new Tankstate_tank ();

}

Set the state of the method, assuming the parameter for the player to click on the keyboard

Public Function SetState ($key)

{

If you press the S

if ($key = ' s ')

{

$this->state = new Tankstate_siege ();

}

If you press the T

ElseIf ($key = ' t ')

{

$this->state = new Tankstate_tank ();

}

}

The attack method of the tank

Public Function Attack ()

{

Handling attacks by the current state itself

$this->state->attack ();

}

}

Build a new tank.

$tank = new tank ();

Assuming an enemy is passing by, the tank is attacked in normal mode.

$tank->attack ();

Stand up tanks.

$tank->setstate (' s ');

Tank attack again, this time it's a model.

$tank->attack ();

?>

Usage Summary: State mode can encapsulate the behavior and attributes associated with state, except when switching state, other places do not need a large number of the current state of the decision, as long as the current state of the method call.


Implementation summary: Use an interface specification State class needs to implement the method, such as the above Tankstate specified attack (). Encapsulate each state into a class, putting different methods in different states into their respective state classes, such as the attack method above, and all state execution interfaces. The original transaction class, such as the above tank class, only responsible for state switching, once the need for a method of call, as long as the current state can be

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.