Design Pattern

By ukmodak | March 31st 2024 10:36:23 AM | viewed 234 times

Pattern

Patern is a way to describe best preactice and good designs. It show a flexible solution to common programming problems.

There are a lot of design pattern. The following are some of theme problem.

  1. Singleton
  2. Factory
  3. Adaptor
  4. Bridge
  5. Observer
  6. Decorator
  7. MVC

Singleton Pattern

Singleton ensure that there can be one instant of a class and provide a global access point to that instance. Singleton is gang of four creational pattern

The Singleton Pattern is often implemented in database clases,loggers, Front Controller or Request or Response object

 
in Config.php 

$host="localhost";
$dbname ="ukmodak";
$username="root";
$password="";


in Singleton.php

class SingletonDatabase{
 public $host;
 public $dbname;
 public $username;
 public $password;
 public $connection;
 public $dbselect;
 public static $dbinstance;
 
 private function __construct(){
 
      include("Config.php");
	  
	     $this->host = $host;
		 $this->dbname = $dbname;
		 $this->username = $username;
		 $this->password = $password;
		 
		 
		 $this->dbconnect();
		 $this->dbselect();	  
 }
 
 public static function dbinstance(){
		 if(! isset(self::$dbinstance)){
			
			  $className =__class__;
			  self::$dbinstance = new $className;
			
		 }
	    return self::$dbinstance;
 }
 
 public function dbconnect(){
      $this->dbconnect = mysql_pconnect($this->host,$this->username,$this->password);
	  
	  return  $this->dbconnect;
 }
 
 public function dbselect(){
   $this->dbselect = mysql_select_db($this->dbname,$this->dbconnect);
   return $this->dbselect;
 }
 
 public function query(){
 }
 
 public function __clone(){
    trigger_error("clone is not allowed", E_USER_ERROR);
 }
 
 public function __wakeup(){
    trigger_error("Unserialized is not allowed",E_USER_ERROR);
 }
 

}


Factory Pattern

Factory allow to instantiate of object at runtime. It is called Factory Pattern since it is responsible for manufacturing an Object. A paramitarized Factory receives the same of the class to instantiate as argument

in Mytest.php 

class MyTest{
  public function __construct(){
  }
  
  public function name(){
    return "Hi Uzzal";
  }
}


in Store.php


class Store{
   public  function  __construct(){
   }
   
   public static function factory($className){
     if(include_once($className.'.php')){
	    $class = new $className;
		return  $class;
	 }else{
	    throw new Exception("Directory not found");
	 }
   }
}


$ob = Store::factory("mytest");
$output = $ob->name();
print_r($output);   // Hi Uzzal 


Adapter Pattern

Convert one class "BookAdapter" to be what expect another class "AdapterBookAdapter"

in BookAdapter.php
class BookAdapter(){
  private $author;
  private $title;
  
  function __construct($author,$title){
    $this->author = $author;
	$this->title = $title;
  }
  
   public function getAuthor(){
      return $this->author;
   }
   
   public function getTitle(){
     return $this->title;
   }
   
   
}

in AdapterBookAdapter.php

include_once("BookAdapter.php");

class AdapterBookAdapter(){
   private $setBook;
   
   public function __construct(BookAdapter $book){
     $this->setBook = $book;
   }
   
   public function getTitleByAuthor(){
      $this->setBook->getTitle()." by ". $this->setBook->getAuthor();
   }
     
}

in AdapterTest.php
include_once("BookAdapter.php");
include_once("AdapterBookAdapter.php");

$book = new BookAdapter("Charle Babase","Computer");
$adapterBookAdapter = new AdapterBookAdapter($book);

$authorByTitle = $adapterBookAdapter->getTitleByAuthor();

print_r($authorByTitle);   // Computer by charle

Bridge Pattern

Note: Bridge Pattern is helpful when you want to decouple a class from its implementation.

in BridgeBookImp.php

abstruct class BridgeBookImt{
  abstruct function showAuthor($author);
  abstruct function showTitle($title);
  
}

in BridgeBookStarImp.php 

include_one("BridgeBookImp.php");

class BridgeBookStarImp extends BridgeBookImp{

    function showAuthor($author){
	  return str_replace(" ","*",$author);
	}
	
	function showTitle($title){
	  return str_replace(" ","*",$title);
	}
	
	
}

in BridgeBookCapImp.php 

include_one("BridgeBookImp.php");

class BridgeBookCapImp extends BridgeBookImp{

    function showAuthor($author){
	  return strtoupper($author);
	}
	
	function showTitle($title){
	  return strtoupper($title);
	}
	
	
}

in BridgeBook.php

include_once("BridgeBookSatrImp.php");
include_once("BridgeBookCapImp.php");

abstruct class BridgeBook{
  private $bAuthor;
  private $bTitle;
  private $chOption;
  
  
  public function __construct($author,$title,$option){
      $this->bAuthor = $author;
	  $this->bTitle = $title;
	  $this->chOption = $chOption;
	  
	   if("star"== $option){
	     $this->chOption = new BridgeBookStarImp;
	   }else{
	     $this->chOption = new BridgeBookCapImp;
	   }
  }
  
   function showAuthor(){
     return $this->chOption->showAuthor($this->bAuthor);
   }
   
   function showTitle(){
     return $this->chOption->showTitle($this->bTitle);
   }
  
}



in BridgeBookAuthorTitle.php

include_once("BridgeBook.php");

class BridgeBookAuthorTitle extends BridgeBook{
  function showAuthorTitle(){
    return $this->showAuthor()." in ".$this->showTitle();  
  }
}



in BridgeTitleAuthor.php

include_once("BridgeBook.php");

class BridgeBookTitleAuthor extends BridgeBook{
  function showTitleAuthor(){
    return $this->showTitle()." in ".$this->showAuthor();  
  }
}



in TestBridge.php

include_once("BridgeBookAuthorTitle.php");
include_once("BridgeBookTitleAuthor.php");

$bookAuthor = new BridgeBookAuthorTitle("Uzzal","Modak","My TV","Star");

$bookAuthor->showAuthorTitle();   // Uzzal*Modak in My* TV



Observer

In the Observer pattern the subject class will notify the "Observer" class if the subject state change

Explain:The Subject pattern will attach observer pattern in array and detach and update favorite method when subject pattern update favourite then automatically subjects notify method call observers update method and observer update method call subjects getfavorite method

in Subject.php

include_once("Observer.php");

abstract class Subject{
  abstract attach(Observer $observer);
  abstract detach(Observer $observer);
  abstract notify();
}


include_once("Subject.php");

abstract class Observer{
  abstract update (Subject $subject);
}


in PatternSubject.php

include_once("Subject.php");
include_once("PatternObserver.php"); 

class PatternSubject extend Subject{
   private $psFavorite;
   private $psArgument = array();
   
   public function  __construct(){
     
   }
   
   public function attach(new PatternObserver()){
     $this->psArgument[] = new PatternObserver();     
     
   }
   
   public function detach(new PatternObserver()){
      foreach($this->psArgument as $key=>$observer){
	     if($observer == new PatternObserver()){
		   unset($this->psArgument[$key]);
		 }
	  }
   }
   
   public function notify(){
      foreach($this->psArgument as $patternObserver){
	     $patternObserver->update($this);
	  }
    }
	
	
	public function updateFavorite($favorite){
	
	   $this->psFavorite = $favorite;
	   $this->notify();
	}
	
	public function getFavorite(){
	  return $this->psFavorite;
	}
   
   
 }

  
bONEandALL
Visitor

Total : 20974

Today :28

Today Visit Country :

  • Germany
  • United States
  • Singapore
  • China
  • United Kingdom
  • South Korea
  • Czechia