OOP Design Patterns

Categories

Component ID

2614540

Component name

OOP Design Patterns

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

1094

Component created

Component changed

Component body

The project is introducing some Demo implementations for OOP PHP Design Patterns in the Drupal 7 and Drupal 8 environments. With the advent of PHP 5.6 and PHP 7 to the Drupal, some advanced OOP concepts become available (See OOP Examples.)

A design pattern is, simply put, a recommended best practice for solving a particular problem. We will walk you through commonly used one, but with a Drupal 'spice'. If you are trying to figure out how to implement such-and-such functionality, then use this design pattern as your approach.

There are 23 classic software patterns published in 1994 by the so-called "Gang of Four" (Gamma et al.), which is frequently abbreviated as "GoF". We will also include some useful patterns that originate in the General Responsibility Assignment Software Patterns (GRASP) approach.

Both GOF and GRASP approaches will easy some common software development challenges:

  • Building new applications while leveraging existing codebases
  • Building/enhancing applications securely, quickly and effectively
  • Integrating new and existing applications
  • Multiple developers programming the same product
  • Few developers programming many products
  • Varying, sometimes unknown, data sources, OS, Setups

But both are not:

  • They are not the end to all of your coding challenges.
  • They are not a substitute for application code and logic.
  • They are not always the best solution for the job.
  • They are not always the easiest concept to grasp and/or use.

Demo patterns available:

The Adapter pattern.

Purpose: allows the interface of an existing class to be used from another interface. It is often used to make existing classes work with others without modifying their source code.

Situations where Adapter Pattern may help:

  • I want to add search capabilities to my application and support multiple search engines such as Google, Yahoo, Bing…
  • Have an image loader for different kinds of images JPG, GIF, PNG…
  • Use different Email providers: Gmail, Hotmail, YahooMail…
  • Use different data sources: Db, XML, TXT, CSV…
  • Connect to different Db: MySQL, PostgreSQL, MSSQL, Oracle
  • Have multiple downloads using different protocols: HTTP, HTTPS, FTP…
  • Use different payment providers: Moneris, AuthorizeNet, Skrill…

Also there are many real-life production Adapter examples in DB Maintenance module.

For example:

  • DbServerHandlerInterface wraps differences between MySQL and PgSQL.
  • ConfigHandler wraps differences between Drupal 7 variables and Drupal 8 Drupal::config.
  • WatchdogAdapter wraps differences between Drupal 7 watchdog and Drupal 8 Drupal::logger.

All differences are isolated in adapters/wrappers and do not affect main module's workflow.