Wednesday 8 May 2013

Design Patterns

Design Patterns

Design patterns: It is optimized, reusable solutions to the programming problems. 
It is not just a class or library, It is technique to get the easy solution of complicated problems.


Following are Design Pattern used in Software Industry.

Factory Pattern: This pattern is used to create the multiple object from a single function.

For example, see below code.

abstract class Button {
 protected $_html;
 
 public function getHtml()
 {
  return $this->_html;
 }
}

class ImageButton extends Button {
 public $_html = '<input height="60" name="image" src="rainbow.gif" type="image" width="60" />';
}

class InputButton extends Button {
 public $_html = '<input name="button" type="button" value="Input Button" />';
}

class FlashButton extends Button {
 public $_html = '<embed height="#" src="filename.swf" width="#"></embed>';
}
  
class mybutton{
  static function factory($buttonObj='InputButton'){
    $obj = new $buttonObj();
    return $obj-&gt;_html; 
  
  }  
}  

/** get buttons by factory patter **/
echo mybutton::factory('ImageButton');
echo mybutton::factory('InputButton');
echo mybutton::factory('FlashButton');


In Factory pattern, like factory you can create number of object and each perform different task.


Singleton Pattern: In this pattern, only one instance survive in whole page. If you try to create another object then it will not let you do this.

For example, see below code.
class Login
{
 private static $instance;
 
 public static function getInstance()
 {
  if( is_null(self::$instance) ) {
   self::$instance = new self();
  }
  return self::$instance;
 } 
  /**
     * Singleton pattern implementation makes "new" unavailable
     *
     * @return void
     */
    protected function __construct()
    {}

    /**
     * Singleton pattern implementation makes "clone" unavailable
     *
     * @return void
     */
    protected function __clone()
    {}
  

}
$obj = Login::getInstance(); 
 
Only one instance exist, in whole page.


The observer pattern:  In this pattern there are two objects i.e Observer and Observable.
Observable have a method that add Observer. When the observable object changes, it sends a message to the registered observers.

For example, see below code.
 
interface IObserver
{
  function onChanged( $sender, $args );
}

interface IObservable
{
  function addObserver( $observer );
}

class UserList implements IObservable
{
  private $_observers = array();

  public function addCustomer( $name )
  {
    foreach( $this-&gt;_observers as $obs )
      $obs-&gt;onChanged( $this, $name );
  }

  public function addObserver( $observer )
  {
    $this-&gt;_observers []= $observer;
  }
}

class UserListLogger implements IObserver
{
  public function onChanged( $sender, $args )
  {
    echo( "'$args' added to user list\n" );
  }
}

$ul = new UserList();
$ul-&gt;addObserver( new UserListLogger() );
$ul-&gt;addCustomer( "Tester " );  


Strategy Design Pattern: It is also known as the policy pattern. In strategy pattern algorithms are extracted from complex classes so they can be replaced easily. A Strategy defines a set of algorithms that can be used interchangeably.

For Example: 
Modes of transportation to an airport is an example of a Strategy Pattern. There are Several options available such as driving one's own car, taking a taxi, Bus,scooter etc. Traveler can use any of above to reach the airport, he can use single one to go to airport(By Hire taxi) OR can use two or more depend on requirement.



Front Controller Pattern: There is single entry point in this design pattern. Means all action of application have to go through in single file/function.
Example : Cakephp and zend framework uses the front controller design pattern. As all url goes through index.php file in webroot / public folder .