Tag Archives: Design Pattern

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.

Facade Pattern – Design Pattern

facade

Intent of facade is to provide the ease of use to any library(subsystem) through a an interface/class.

The Facade Pattern provides a unifide interface to a set of Interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.

Here We can take example of a complex theater system. For playing a movie we may need to set a different programs of each subsystem, Now We can look at facade is some thing like that reduces steps in performing a task, rather than tuning each individual program to work.

In below example TheaterFacade object makes easier to operate programs.

<?php
class TheaterFacade {
    public $dvd, $music, $movie, $player;

    public function TheaterFacade(DVD $dvd, Music $music, Movie $movie, Player $player)
    {
        $this->dvd = $dvd;
        $this->music = $music;
        $this->movie = $movie;
        $this->player = $player;
    }

    public function playMovie($movieName)
    {
        $this->music->setMusic(7);
        $this->movie->setMovie($movieName);
        $this->player->play();
        ...
        ...
    }

    public function stopMovie()
    {
        $this->music->stopMusic();
        $this->player->stop();
        ...
        ...
    }

}

class Music {
    
    public function setMusic($volume)
    {
      //setting volume function
    }

    public function stopMusic()
    {
      //stop function
    }
}

class Player {
    public function play(){
       //play function
    }

    public function stop(){
       //stop player function
    }

}

class Movie {

    public function setMovie ($movie) {
        //set movie to play
    }
}

Adapter pattern – Design Pattern

adapter pattern

Lets Have a Look at adapter pattern definition, that states clearly about its use case.

The Adapter pattern converts the interface of a class into another Interface that client expect. Adapter lets classes work together that could not just because of incompatible interfaces

lets have a look at below code that will give you a better idea.

<?php

class ENumeration Implements Iterator {

    protected $enumobj; // will hold the Adaptee class instance

    public function ENumertion(Enum enumObject)
    {
        $this->enumobj = enumObject;
    }

    public function hasNext()
    {
        return $this->enumobj->hasNextElement();

        //NOTE here hasNextElement method is lying in Enum class, call to hasNext() is being tranfered to Enum class objects hasNextElement() method.
    }

    public function next()
    {
        return $this->enumobj->getNextElement();
        
        //NOTE here getNextElement method is lying in Enum class, call to next() is being tranfered to Enum class object's getNextElement() method.
    }

}

Now any call using Enumeration class object will be calling Enum class methods, so acting as an adapter to Enum class, caller class doesnot know that it call is being transfered to Enum class.

Hope above example make sense, although if you have any query you can connect with me over comments or by any means.

What is design pattern and benefits of adopting it.

A design pattern comes in to picture, when you are designing a project that may come with vital changes(An obious nature of software projects) in your flow depending on customers needs.

Sometimes it’s easy a add a new feature in your application, and some time it becomes difficult due to its impact on the system to add a new feature changes, although it may be too small from a users point of view.

So main use of design pattern comes to picture in identifying how you will be designing your application of a specific domain, so that it can adopt changes(means to add a new features easily from programming point of view) easily to your application, without affecting previous functionalities.

So What comes main things while designing a software from a software architect point of view.

1- Identifying objects and there interactions.

2- How frequent the behavior is going to be changed for an object in your application.

3- How a new object may come in to picture (probably adding a new feature)

4- How it will be serving to end users(speed, accuracy, user traffic load etc.).

 

Above described parameters may help to any software architect/programmer to design a system that can easily incorporate changes as well.

It may look some time that a particular practice is not being followed in your project scenarios, that is ok, sometimes its your design patterns need, as your project design pattern have to be focused mainly on how well it can adopt changes as well as how it will be serving to end users.