Design Pattern

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


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 

$dbname ="ukmodak";

in Singleton.php

class SingletonDatabase{
 public $host;
 public $dbname;
 public $username;
 public $password;
 public $connection;
 public $dbselect;
 public static $dbinstance;
 private function __construct(){
	     $this->host = $host;
		 $this->dbname = $dbname;
		 $this->username = $username;
		 $this->password = $password;
 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){
	    $class = new $className;
		return  $class;
	    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


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

$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 


class BridgeBookStarImp extends BridgeBookImp{

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

in BridgeBookCapImp.php 


class BridgeBookCapImp extends BridgeBookImp{

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

in BridgeBook.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;
	     $this->chOption = new BridgeBookCapImp;
   function showAuthor(){
     return $this->chOption->showAuthor($this->bAuthor);
   function showTitle(){
     return $this->chOption->showTitle($this->bTitle);

in BridgeBookAuthorTitle.php


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

in BridgeTitleAuthor.php


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

in TestBridge.php


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

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


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


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


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

in PatternSubject.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()){
   public function notify(){
      foreach($this->psArgument as $patternObserver){
	public function updateFavorite($favorite){
	   $this->psFavorite = $favorite;
	public function getFavorite(){
	  return $this->psFavorite;


Total : 20165

Today :12

Today Visit Country :

  • China
  • United States
  • United Kingdom
  • Portugal
  • Canada