State Pattern – Design Pattern

The state pattern allows an object to alter its behavior. When its internal states changes, the object itself will appear to change its class

This pattern encapsulates states in to separate classes and delegate to the object representing current state.

Change in class is done by using composition over the base class (in our example Context Class’s property currentState)

Here, what we are trying to do is that attaching a subclass behavior based on previous actions performed on that object (In our case it will be Context class’s currentState property object assigned, that we will see in our example ).

<?php

// class being called to test our program
class Test
{
    public $context ;
    
    
    public function testnow()
    {
        $this->context = new Context;

        $this->context->requestOne();    // Could be any methods
        $this->context->requestTwo();

        print_r($this->context);
        $this->context->requestOne();    // Could be any methods

        // here you may see the changes in underlying classes on execution of any mathod 
        // if it tries to change it's current state, and change in state of current object will be responsible for changing the underlying state class.
        
    }
    
}


//Context   -- main class being called

Class Context {
    
    public State $StateA;
    public State $StateB;
    public State $StateC;
    public State ... ;// More state classes may involve depend on the behavior of game.

    public State $currentState; // depicting the current state of a Game
    
    public function __construct()
    {
         $this->StateA = new StateA($this);
         $this->StateB = new StateB($this);
         $this->StateC = new StateC($this);
         ...

         $this->currentState = new StateA($this); // suppose default state initailly
    }
    
    function requestOne()
    {
        ...
        $this->currentState->requestOne(); // will be calling the methods of 'currentState' assigned class, may be any of state classes.
    }

    function requestTwo()
    {
        ...
        $this->currentState->requestTwo(); // will be calling the methods of 'currentState' assigned class, may be any of state classes.
    }

    function requestThree()
    {
        ...
        $this->currentState->requestThree(); // will be calling the methods of 'currentState' assigned class, may be any of state classes.

    }

}



interface State
{
    // All these methods needs to be implemented by the class
    public function requestOne();
    public function requestTwo();
    public function requestThree();
} 


//Whenever there is a request made using class Context, its request mthod delegate(propagetes) to state subclass (e.g. StateA) , to handle the behavior of object.


class StateA implements State {

    public function requestOne()
    {
        ...
        ...
        //changing state classes if there needed
    }


    public function handleTwo()
    {
        ...
        ...
        //changing state classes if there needed
    }


    public function handelThree()
    {
        ...
        ...
        //changing state classes if there needed
    }

}

//Similarly for other classes like StateB, StateC, and so on..


class StateB implements State 
{
    
    // All methods similar to method StateA class methods
    // and additional methods may exist there too.
}

This kind of a pattern can be useful in designing Games especially, where an object behavior changes based on its previous operations.

Hope that make a little bit clear.

Leave a Reply

Your email address will not be published. Required fields are marked *