PHP Design Patterns with Drupal 8

+

What are Design patterns ?

Description of Communicating Objects and classes that are customised to solve a general problem in a particular context.

As per the official Definition...

In Simple Words..

They are patterns or templates , that can be implemented to solve a problem in different particular solutions...

Design Patterns Superpowers (Why are they required ?)

  • Design patterns help to speed up the development.
     
  • The templates are proven and from the developer’s position, only implementation is required.
     
  • Encapsulates big ideas in a simpler way
     
  • Each pattern describes a problem which occurs over and over again

Design Patterns enforces SOLID principles

SOLID is a set of object oriented design principles aimed at making code more maintainable and flexible

  • Single responsibility principle - a class should have a single responsibility, but more than that, a class should only have one reason to change
  • Open/closed principle - classes should be open for extension, but closed for modification
  • Liskov substitution principle - objects should be replaceable by their subtypes without altering how the program works
  • Interface segregation principle - classes should not be forced to implement interfaces they do not use
  • Dependency inversion principle - states that classes should depend upon abstractions, not concretions

Categorisation of GoF (Gang of Four Design Patterns)

  • Creational - Used to construct objects such that they can be decoupled from their implementing System.
     
  • Structural - Used to form large object structures between many disparate objects
     
  • Behavioural - Used to manage algorithms, relationships and responsibilities between objects.

Any Idea why are they called GoF (Gang Of Four Design Patterns)?

Design Patterns with Drupal 8

  • Drupal 8 is all about Object Oriented PHP and contains concepts like
    • Dependency Injection,
    • Service containers,
    • Factory Methods,
    • PHP traits,
    • Discovery Methods,
    • Strategy
    • Singleton
       
  • Symfony components adopted by Drupal 8
     
  • If you know these concepts, it will be easy for you to understand Drupal 8 better and move to the next level of development in Drupal 8.

We will cover ...

  • Singleton Design Pattern and it's implementation
     
  • Factory Pattern and it's implementation
     
  • Mediator Design pattern and it's implementation
     
  • Dependency Injection (If time allows)

Singleton Design Pattern (The Creational Pattern)

Singleton pattern which doesn’t allow system to create more than one instance of the class.

Use Singleton Pattern and You will get ...

  • Boosts performance by not allowing creation of multiple objects to avoid consumption of more memory by creating multiple objects.
     

  • Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.
     

  • Ease of implementation.

Factory Design Pattern (The Creational Pattern)

Factory Method defines a method, which should be used for creating objects instead of direct constructor call. Subclasses can override this method to change the class of objects that will be created.

UML Explanation

Mediator Design Pattern (The behavioural Pattern)

Mediator is a behavioural design pattern that reduces coupling between components of a program by making them communicate indirectly, through a special mediator object.

UML Explanation

Services and Dependency Injection in Drupal 8

  • Drupal 8 introduces the concept of services to decouple reusable functionality and makes these services pluggable and replaceable by registering them with a service container.
     
  • Services are used to perform operations like accessing the database or sending an e-mail.

     
  • Instead of Using PHPs Native functions and mysql we use services to establish connections and fire queries.

References

Made with Slides.com