PHP DESIGN PATTERN

Arman Hakim Sagar
Sep 7, 2018 · 4 min read

To organize our code senior developer already developed some pattern.Thats are call design pattern.

Creational patterns: Design creation mechanisms.

Example: singleton,factory.

Behavioral pattern:Relationships between object with another objects.

Example: strategy pattern.

Structural design:Relationships between entities objects.

Example: facade, Adaptar,composit,decorator.

Strategy pattern(Behavioral pattern):

Strategy pattern is all about separate our main strategy points of apps & make it reusable,fast & changeable.Set your main strategy in interface & implement it on change of interface behavior.

Example : Client can call or message to someone.So action depends on client insert value.

$knock = "phone";

if($knock >= "phone") {

$phone = new phone();

$phone->call($phone);

} else {

if($knock >= "message") {

$message = new message();

$message->call($message);

}

Now problem:

  • You have to call every time when you need.
  • consider what you want to be able to change without redesign or add another method.

Apply Strategy pattern:

interface client{
public function knock_phone();
public function knock_message();
}

and implement the interface in each of your client:

class phone implements client{
// implement interface methods
}

class message implement client{
// implement interface methods
}

Facade design pattern(Structural design pattern):

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Facade design pattern(Creational pattern)

Factory pattern(Creational Design Patterns):

one of the best ways to create an object.Suppose you have a car factory, your giving car model its creating car.Example: Laravel factory seeder

Observer pattern(Behavioral pattern):

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

It is mainly used to implement distributed event handling systems, in “event driven” software.

The observer pattern is also a key part in the familiar model–view–controller (MVC) architectural pattern.

Repository pattern(Behavioral pattern):

I still think this is one of the more common ways to work with the data layer.The implements Repository interface section isn’t strictly necessary but it adds an extra layer of structure to our code.

Facility:Stop creating multiple object for single class.Its also helpfull if occur any change in database.

Singleton pattern(Creational patterns):

Singletons are designed to ensure there is a single (hence the name singleton) class instance and that is global point of access for it, along with this single instance we have global access and lazy initialization.In the singleton pattern a class can distribute one instance of itself to other classes.

What Is Composite Pattern(Behavioral pattern)?

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that is treated the same way as a single instance of the same type of object. The intent of a composite is to “compose” objects into tree structures to represent part-whole hierarchie.

What Is Builder Pattern(Behavioral pattern)?

The Builder is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation.In one word what we need to built a robot we can define in builder part.

What Is Adaptar Pattern(Behavioral pattern)?

In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface.

$book = new Book("data1","data2");$bookAdapter = new BookAdapter($book);class BookAdapter {    private $book;
// dependency injection
function __construct(SimpleBook $book_in) { $this->book = $book_in; }}

What Is decorator pattern?

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.Such as abstract class.Parent methods must add value in extends.Child class can also add extra method of parent class.Adding extra feature dynamically without hitting main class.

What Is Prototype pattern?

In the Prototype Pattern we create one standard object for each class, and clone that object to create new instances.

$phpProto = new PHPBookPrototype();$book1 = clone $sqlProto;$book1->setTitle('SQL For Cats');
$book2 = clone $phpProto;$book2->setTitle('OReilly Learning PHP 5');

Arman Hakim Sagar

Written by

I am Arman Hakim Sagar.Working area:Algorithm & Data Structure,API OAuth & Security,Broadcasting,High Traffic Management System,System Design

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade