Tag Archives: OO Design

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

    Continue reading

Advertisements