Sunday 26 June 2016

Adapter Design Pattern

This pattern involves a single class which is responsible to join functionalities of independent or incompatible classes.
This particular pattern can be used when your code is dependent on some external API, or any other class that is prone to change frequently.
<?php
Class Book {
private $title = '';
private $author = '';
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
public function getTitle() {
return $this->title;
}
public function getAuthor() {
return $this->author;
}
}
Class ExtendedBook {
private $book;
public function __construct($book){
$this->book = $book;
}
public function getTitleAndAuthor() {
echo "Title is : ".$this->book->getTitle()." and author is : ".$this->book->getAuthor();
}
}
$book = new Book('design patterns', 'vivek');
$extendedBook = new ExtendedBook($book);
$extendedBook->getTitleAndAuthor();
?>
Here you can see that the ExtendedBook class doesnot have to worry about the changes in
the Book class.
A more practical example would be, if we are using any third party api in our project,
and if we directly use their classes and functions everywhere in our code, then there might
be a situation where the third party decide to modify their api classes and functions, so
now ,we have to change our code where ever we have used their classes.
But if we are using adapter pattern and made a class which acts as a bridge between
the third party api and our implementation of their classes, then we have to only
change our code at only one place i.e. in our adapter class.

Prototype Design Pattern

The prototype pattern is used when the type of objects to create is determined by a  prototypical instance, which is cloned to produce new objects. This pattern is used to:
  • avoid subclasses of an object creator in the client application..
  • avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.

<?php
Class Book {
private $title = '';
public function setTitle($title) {
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
public function __clone() {}
}
$newBook = new Book();
$newBook->setTitle("oops");
$title = $newBook->getTitle();
echo $title;
echo "<br>";
$newBook2 = clone $newBook;
$newBook2->setTitle("designPattern");
$title = $newBook2->getTitle();
echo $title;
echo "<br>";
$newBook3 = $newBook;
$newBook3->setTitle("newTittle");
echo $newBook->getTitle();
?>

Strategy Design Pattern

This pattern is used when we have several ways (algorithms) to perform the same operation and we want the application to pick the specific way based on the parameters you have. The point is to separate algorithms into classes that can be plugged in at run time.
In the below example, the function in the Sort class decides which object to create in order to carry out the sorting based on the size of the array which is passed in the constructor.
<?php
Class Sort {
public function __construct($data) {
$this->sort($data);
}
public function sort($data) {
if (count($data) > 5) {
$sortData = new QuickSort();
$sortData->sort($data);
} else {
$sortData = new MergeSort();
$sortData->sort($data);
}
}
}
Class QuickSort {
public function sort($data) {
echo "sorting the data using quicksort";
}
}
Class MergeSort {
public function sort($data) {
echo "sorting the data using mergesort";
}
}
$mySort = new Sort(array(1,3,2,3,2,3,2,3,3));
?>