PHP Adapter Design Pattern Example

This post contains two examples of Adapter Design Pattern using PHP. I am giving these examples in order to understand more comprehend about Adapter Pattern.

First, implementation of Adapter Design Pattern statement
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Adapters are helpful if you want to use a class that doesn’t have quite the exact methods you need.

Second, implementation of using legacy class and you can’t change that legacy class but you want to port its method to another needful method.

First Example

An example class ‘clsProducts’ methods can be used inside many Adapter class to returns another result.
clsProducts object instance is passed as Adapter classes constructor argument and clsProducts methods are being used inside those Adapter classes.

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();


$objListViewAdapter->view() will make view below

On the other class $objTableViewAdapter->view()

Second Example

Let’s say you have a legacy library or legacy class that you can’t change codes inside them. This class methods are not very suitable to your current requirements.
Then you should make an Adapter class to port legacy class methods to Adapter methods that match your needs.

Save below file to clsSong.php and think of this file can’t be edited.

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;
	}
}

Then you can make Adapter Class in another file which includes previous clsSong.php

include "clsSong.php";
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();


As you can see this clsSong_Adapter Class method have different method to display Song’s title & genre.

Regards,
Agung Gugiaji

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