Adapter Pattern OO Design PHP Example

Quoting from Url http://www.oodesign.com/adapter-pattern.html
“The adapter pattern is adapting between classes and objects.
Like any adapter in the real world it is used to be an interface, a bridge between two objects.
In real world we have adapters for power supplies, adapters for camera memory cards, and so on. Probably everyone have seen some adapters for memory cards. If you can not plug in the camera memory in your laptop you can use and adapter.
You plug the camera memory in the adapter and the adapter in to laptop slot. That’s it, it’s really simple.”

It is more clear to understand with example.
PHP Examples

  1. Example 1
    class clsSong {
    	private $title;
    	private $genre
    	function __construct($title, $genre) {
    		$this->title = $title;
    		$this->genre = $genre;		
    	}
    	
    	function getTitle() {
    		return $this->title;
    	}
    	
    	function getGenre() {
    		return $this->genre;
    	}
    }
    
    class clsSong_Adapter {
    	private $objSong;
    	function __construct(clsSong $objSong) {
    		$this->objSong = $objSong;			
    	}
    	
    	function getTitleGenre() {
    		return $this->objSong->getTitle()."/".$this->objSong->getGenre();
    	}
    }
    
    //implementation 
    $objSong = new clsSong("Nothing Else Matter", "Heavy Metal");
    echo $objSong->getTitle()."/".$objSong->getGenre();
    echo "<br>";
    
    $objSngAdapt = new clsSong_Adapter($objSong);
    echo $objSngAdapt->getTitleGenre();
    
    

    There is clsSong as an original class with its methods for returning member’s data i.e Title & Genre.
    Let say we want to preview Song’s Title & Genre with some format. To do it we created another class as an Adapter which is clsSong_Adapter.
    This Adapter class expects clsSong object and will returns Song’s Title & Genre with a format. So basically clsSong_Adapter class is adapting clsSong to give specific output.

    Client just use Adapter class to do that and no need to change the original clsSong class to give required output.
    Even oftenly the original class is already encapsulated. This is the purpose of adapter pattern in OO design.

  2. Example 2
    class clsProducts {
    	private $categories;
    	private $products;
    	function __construct($dataSet) {
    		$this->products = $dataSet;
    		$this->categories = array_keys($this->products);		
    	}
    	
    	function getCategories() {
    		return $this->categories;
    	}
    	
    	function getProductsByCategory($category) {
    		return $this->products[$category];
    	}
    }
    
    interface iView {    
        function view();    
    }
    
    class clsProducts_ListViewAdapter implements iView {
    	private $objInstance;
    	function __construct(clsProducts $objProducts) {
    		$this->objInstance = $objProducts;
    	}
    	
    	function view() {
    		$html = "<table><tr><td><b>Category</b></td><td><b>Product</b></td></tr>";
    		$arrCategories = $this->objInstance->getCategories();
    		foreach ($arrCategories as $cat) {
    			$arrProducts = $this->objInstance->getProductsByCategory($cat);
    			foreach ($arrProducts as $prod) {
    				$html = $html."<tr><td>".$cat."</td><td>".$prod."</td></tr>";
    			}
    		}
    		$html = $html."</table>";
    		return $html;
    	}
    }
    
    class clsProducts_TableViewAdapter implements iView {
    	private $objInstance;
    	function __construct(clsProducts $objProducts) {
    		$this->objInstance = $objProducts;
    	}
    	
    	function view() {
    		$html = "<table>";
    		$arrCategories = $this->objInstance->getCategories();
    		$i = 1;
    		foreach ($arrCategories as $cat) {
    			if ($i % 2 !== 0) {
    				$bgcolor = "lightgrey";
    			} else {
    				$bgcolor = "white";
    			}
    			$html = $html."<tr><td>";
    			$html = $html."<table style=\"background-color:'".$bgcolor."'\"><tr><td colspan='2'><b><font size='4'>".$cat."</font></b></td></tr>";
    			$arrProducts = $this->objInstance->getProductsByCategory($cat);
    			foreach ($arrProducts as $prod) {
    				$html = $html."<tr><td width=6></td><td>".$prod."</td></tr>";
    			}
    			$html = $html."</table></td></tr>";
    			$i++;
    		}
    		$html = $html."</table>";
    		return $html;
    	}
    }
    
    //implementation 
    $dataSet = array();
    $dataSet["Bike"] = array("Mountain Bike", "Road Bike");
    $dataSet["Car"] = array("Race Car", "Family Wagon", "City Car");
    $dataSet["SparePart"] = array("Tires", "Oils", "Brakes");
    
    $objProducts = new clsProducts($dataSet);
    
    $objListViewAdapter = new clsProducts_ListViewAdapter($objProducts);
    echo $objListViewAdapter->view();
    
    echo "<br><br>";
    
    $objTableViewAdapter = new clsProducts_TableViewAdapter($objProducts);
    echo $objTableViewAdapter->view();
    

    Same explanation as example one. Client just implement suitable Adapter class to get specific output/result.

Regards,
Agung G

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s