Design Patterns

Sraban Pahadasingh    April 14, 2020 05:46 PM

In software engineering, a design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.

Some Of Design Patterns:

Singleton

  • Whenever only dingle instance of a object is required through out the system
  • Example DB connection & Logger writing Object.
  • In Angular, any services with @injectable() are singleton always.

Facade

  • A facade is an object that serves as a front-facing interface masking more complex underlying or structural code
  • Provide a unified interface to a set of interfaces in a subsystem.
  • Facade deals with interfaces, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside.

Adaptor

  • The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients
  • It allows two or more previously incompatible objects to interact.
  • It allows reusability of existing functionality.
  • For example, sharing post in diferent social medias like fb,tweet,linkedin can be done using one interface implements the common methods in different classes of social media to share the data.

Observable

  • An Observer Pattern says that "just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically".
  • It describes the coupling between the objects and the observer.
  • It provides the support for broadcast-type communication.
  • When the change of a state in one object must be reflected in another object without keeping the objects tight coupled.

Proxy

  • Simply, proxy means an object representing another object
  • It provides the protection to the original object from the outside world.
  • So, we can perform many operations like hiding the information of original object, on demand loading etc.
  • A proxy, in its most general form, is a class functioning as an interface to something else.
  • In proxy pattern, we create object having original object to interface its functionality to outer world.

Front Controller

  • Where single entry point like index.php, index.html inframeworks

Factory

  • Factory pattern helps in instantiation and creation of Objects
  • According to definition from wikipedia, Factory Pattern is "A factory is an object for creating other objects"
  • In simple factory pattern, we have a factory class which has a method that returns different types of object based on given input
  • For an example a Factory produces different type of fans, and all type of fan are kept for Testing, instead of swtiching on / off each fan, a factory function should be generated which will test the fan by passing the type of fan, no need to go detail to check how fans are switched off and switched on. It is kind of abstracting the deail functionality of various fans(table fan, ceiling fan, powered fan, wall fan).
  • In this pattern, a class simply creates the object you want to use. Consider the following example of the factory pattern:

class Automobile
{
    private $vehicleMake;
    private $vehicleModel;

    public function __construct($make, $model)
    {
        $this->vehicleMake = $make;
        $this->vehicleModel = $model;
    }

    public function getMakeAndModel()
    {
        return $this->vehicleMake . ' ' . $this->vehicleModel;
    }
}

class AutomobileFactory
{
    public static function create($make, $model)
    {
        return new Automobile($make, $model);
    }
}

// have the factory create the Automobile object
$veyron = AutomobileFactory::create('Bugatti', 'Veyron');

print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"


Strategy Pattern

  • With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for instantiating a particular algorithm to have no knowledge of the actual implementation. This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe just an array of data

interface OutputInterface
{
    public function load();
}

class SerializedArrayOutput implements OutputInterface
{
    public function load()
    {
        return serialize($arrayOfData);
    }
}

class JsonStringOutput implements OutputInterface
{
    public function load()
    {
        return json_encode($arrayOfData);
    }
}

class ArrayOutput implements OutputInterface
{
    public function load()
    {
        return $arrayOfData;
    }
}

class SomeClient
{
    private $output;

    public function setOutput(OutputInterface $outputType)
    {
        $this->output = $outputType;
    }

    public function loadOutput()
    {
        return $this->output->load();
    }
}

// Want some JSON?
$client->setOutput(new JsonStringOutput());
$data = $client->loadOutput();





Comments powered by Disqus