A Better Way to Design Your Software
A general repeatable solution to a commonly occurring problem in software design
Providing tested, proven development paradigms
Common Problem, Well known Proven solution
Reduce Complexity
Does not need to start from scratch
Widely used and thus reduce the technical risk
Highly flexible and language independent (but OOP)
Patterns do not lead to direct code reuse.
Complex in nature
Teams may suffer from patterns overload.
Deceptively simple
Everything has some
Besides these drawbacks definitely it's outcome is much more fruitful
A class of which only a single instance can exist in one Request Life cycle
<?php
class Singleton
{
/**
* Returns the *Singleton* instance of this class.
* @staticvar Singleton $instance The
* *Singleton* instances of this class.
* @return Singleton The *Singleton* instance.
*/
public static function getInstance()
{
static $instance = null;
if (null === $instance) {
$instance = new static;
}
return $instance;
}
/**
* Protected constructor to prevent creating
* a new instance of the *Singleton* via the
* `new` operator from outside of this class.
*/
protected function __construct(){}
/**
* Private clone method to prevent cloning
* of the instance of the *Singleton* instance.
*/
private function __clone(){}
/**
* Private unserialize method to prevent
* unserializing of the *Singleton* instance.
*/
private function __wakeup(){}
}
$obj = Singleton::getInstance();
\var_dump($obj === Singleton::getInstance()); // bool(true)
Example : Config, Logger,
Database Connection
Pros :
Cons :
Bridge between Multiple classes.
Return Instance of Several possible classes based on provided data, which have same parents or implementation.
Example : Database Server
Pros :
Cons :
Encapsulate specific families of algorithms, allowing the client class responsible for instantiating a particular algorithm to have no knowledge of the actual implementation.
Example :
Output Interface [Array, JSON]
Pros :
Cons :
Example :
TravelBookingService
[Flights, Hotels, Ship Bookings]
Pros :
Cons :
Wrapper for many sub-system
By Robert C. Martin,
Known as Uncle Bob
Object-oriented design(OOD) principles
A Class should have only one single responsibility.
One reason to change.
Pros :
Cons :
Pros :
A Class should be OPEN for Extension, and CLOSED for modification
Pros :
Every subclass is substitutable by its parent class.
Object of a derived class should be able to replace an object of the base class without bringing any errors in the system or modifying the behaviour of the base class
Normal inheritance IS-A relation
LSP IS-SUBSTITUABL-FOR relation
Pros :
Interface should not force its client to implement anything that client will not use.
Pros :
Entities must depend on abstractions not on concretions.
High level module must not depend on the low level module, but they should depend on abstractions.
Cons :
KISS - Keep It Simple, Stupid
DRY - Don't Repeat Yourself
WET - Write Everything Twice
YAGNI - You Ain't Gonna Need It
All Images are taken from online