Sunday, 26 June 2016

Observer Design pattern

Observer means that someone is looking at your activity, and it may be possible that the observer takes some action depending on your activity. The same concept is applicable to  design pattern as well. We should implement this pattern when we have a one-to-many dependency between our object and one object needs to be changed/notified when any changes are made to any other object.
interface IObserver { function onChanged( $sender, $args ); } interface IObservable { function addObserver( $observer ); } class UserList implements IObservable { private $_observers = array(); public function addCustomer( $name ) { foreach( $this->_observers as $obs ){ $obs->onChanged( $this, $name ); } public function addObserver( $observer ) { $this->_observers []= $observer; } } class UserListLogger implements IObserver { public function onChanged( $sender, $args ) { echo( "'$args' added to user list\n" ); } }
$ul = new UserList(); $ul->addObserver( new UserListLogger() ); $ul->addCustomer( "vivek" ); ?>

Factory Design Pattern

The factory method as the name suggests  is a method/process whose main purpose in life is to create objects. Most commonly (and really per definition) the factory method is an interface method that delegates object instantiation decisions to sub classes. It works on the principle of " separation of concerns ". 
Advantage of Factory Pattern - Loose Coupling and Segregation of Responsibilities. Now instead of hard binding the complete logic to decide the nature and shape of the object on the basis of some conditions, you are assigning the responsibility to some other object and hence making the relationship loosely coupled and hence maintainable.In the following example, the Company class's only job is to create objects of other classes, there by giving the responsibility to other classes to implement the respective functionality.

<?php
Class Company {
public function getEmpDetails() {
new Employee();
}
public function getSalesInfo() {
new Sales();
}
public function getProductsInfo() {
new Products();
}
}
Class Employee {
public function __construct() {
echo "Employee Details are : ";
}
}
Class Sales {
public function __construct() {
echo "Sales Info : ";
}
}
Class Products {
public function __construct() {
echo "Products Info : ";
}
}
$company = new Company();
$company->getEmpDetails();
$company->getSalesInfo();
$company->getProductsInfo();
?>

Singleton design pattern

As the name suggests this pattern is related to a notion like " only one of something ".
More specifically , when you want only one instance of a class in your program.
A very common example where this type of design pattern can be applied is when you are doing database connections.
Suppose , if you writing your database connection related code inside a function of a class called DatabaseConn . Now , everytime you create an object of that class and call that function , you are getting a "new"  database connection , which ofcouse is not necessary and is a overhead to the database server. To avoid such problems , you can apply singleton pattern, and ensure that there is only one instance of that class is generated.
The following is an example ( in php ):

//Class to make db connection
Class DbConnect {
//Static member variable to hold the connection object
private static $conn = NULL;
private static $instance = NULL;
private function __construct() {
// Create connection
self::$conn = new mysqli("hostname", "username", "password", "dbname");
// Check connection
if (self::$conn->connect_error) {
echo "Connection failed: " . $conn->connect_error;
}
}
public static function connect() {
if (self::$instance === NULL) {
self::$instance = new DbConnect();
return self::$instance;
} else {
return self::$instance;
}
}
}
$myDbConn = DbConnect::connect();
$myDbConn2 = DbConnect::connect(); //If you var dump these two objects , you will find that they are same instances.
var_dump($myDbConn);
var_dump($myDbConn2);

Monday, 20 June 2016

What do you mean by Design Patterns ?

* Design Pattern is a methodology ,which can be used to plan and structure your prtgrams.

*  Design patterns can speed up the development process by providing tested, proven development paradigms to the common programming problems.

* Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called "Gang of Four" (Gamma et al.), which is frequently abbreviated as "GoF".

Reusing design patterns helps to prevent subtle issues that can cause major problems and it also improves code readability for coders and architects who are familiar with the patterns