Design Patterns In PHP

Table of Contents

Design Patterns are standard ways of approaching some recurring problems in programming. As the application grows, refactoring and implementing features becomes harder. To prevent these issues, development must be done following a design or architectural pattern. Here is a quick overview of the benefits, elements and classification of using design patterns.

Benefits

  • Maintainable code, code reusability, documentation and readability.
  • Ease in finding appropriate objects, determining object gradually, specifying object interfaces and implementing even for large software projects.


Elements

  • Name: This is a handle we can use to describe the problem.
  • Problem: This describes when to apply the pattern.
  • Solution: This describes the elements, relationships, responsibilities, and collaborations, in a way that we follow to solve a problem.
  • Consequences: This details the results and trade-offs of applying the pattern.

Classification

Abstract Factory (Creational Pattern)Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Builder (Creational Pattern)Separates the construction of a complex object from its representation so that the same construction process can create different representations.
The Factory Method (Creational Pattern)Defines an interface for creating an object, but let subclasses decide which class to instantiate. A class can defer instantiation to subclasses. Prototype (Creational Pattern)Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.
Singleton (Creational Pattern)Ensures a class only has one instance and provides a global point of access to it. Adapter (Structural Pattern)Converts the interface of a class into another interface that the clients expect and lets classes work together which otherwise couldn’t because of incompatible interfaces.
Bridge (Structural Pattern) Decouples an abstraction from its implementation so that the two can vary independently. Composite (Structural Pattern)Composes objects into tree structures to represent part-whole hierarchies and lets clients treat individual objects and compositions of objects uniformly.
Decorator (Structural Pattern)Attaches additional responsibilities to an object dynamically and provide a flexible alternative to subclassing for extending functionality. Facade (Structural Pattern)Provides a unified interface to a set of interfaces in a subsystem and defines a higher-level interface that makes the subsystem easier to use.
Flyweight (Structural Pattern)Uses sharing to support large numbers of fine-grained objects efficiently. Proxy (Structural Pattern)Provides a surrogate or placeholder for another object to control access to it.
Chain of Responsibility (Behavioural Pattern)Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. It chains the receiving objects and passes the request along the chain until an object handles it. Command (Behavioural Pattern)Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Interpreter (Behavioural Pattern)Defines a representation of its grammar for a given language along with an interpreter that uses the representation to interpret sentences in the language. Iterator (Behavioural Pattern)Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Mediator (Behavioural Pattern)Defines an object that encapsulates how a set of objects interact and promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Memento (Behavioural Pattern)Captures and externalize an object’s internal state so that the object can be restored to this state later without violating encapsulation.
Observer (Behavioural Pattern)Defines a one-to-many dependency between objects so when one object changes state, all its dependents are notified and updated automatically. State (Behavioural Pattern)Allows an object to alter its behaviour when it’s internal state changes. The object will appear to change its class.
Strategy (Behavioural Pattern)Defines a family of algorithms, encapsulate each one, and make them interchangeable. An algorithm can vary independently from clients that use it. Template Method (Behavioural Pattern)Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Subclasses can redefine certain steps of an algorithm without changing the algorithm’s structure.
Visitor (Behavioural Pattern)Represents an operation to be performed on the elements of an object structure. A new operation can be defined without changing the class of the elements on which it operates.

If design patterns are properly used and implemented in a project, it becomes easier for any new developer joining in the midst of development to quickly get up to speed. Thus, design patterns help avoid possibilities of bloating, bad performance, inflexibility and a variety of errors that are hard to fix.

Happy coding!

Share

Comments

comments

Recent Awards & Certifications

  • Clutch Global
  • Clutch Champion