bridge design pattern in php

Describing PHP Bridge Design Pattern With an Example

Another structural design pattern is the Bridge design pattern which represent two collection of strategies where each one of the first collection can make use of each one in the second collection.

 

 

When talking about the Bridge design pattern we remember the Strategy pattern although the strategy pattern is a behavioral pattern and the Bridge pattern is a structural pattern but they have some similarities. In the strategy pattern you chose between two strategies. In the Bridge pattern you have two collection of strategies and each strategy in the first collection can make use of each strategy in the second collection. So in other words the Bridge pattern is a strategy pattern but in large scale.

The first collection in the Bridge pattern is the abstractions while the second collection is the implementations. The abstractions doesn’t need to know how the implementation work. Just all it need it know is how to call the implementation.

 

php bridge design pattern diagram

Terminology

 

Abstractions

Implementations



Example

Let’s see an example to clarify the idea, suppose we have three types of applications as follows: Web application, Desktop application, and Mobile Application. These three different applications needs to access and manipulate database where the database driver may be Mysql database or MsSqlServer database or Oracle database.



As you see in the above code i have added the interfaces and classes that represent the basic components of the Bridge pattern. As we mentioned earlier that the bridge pattern have two main components.

The first component is the abstraction collection of strategies and in this example the abstraction component represent the application type. The abstraction component here have no implementations in it’s own but call the other collections implementations based on database driver type.

At first i have added the interface for the first collection IOApplication inside this interface an important function called setDatabaseDriver(IODatabaseDriver $dbDriver). This function is to set a reference of the second component in first component to be able to call the second component handleQuery($query) method, so i have added another abstract class to handle the setDatabaseDriver() method like this:

I have added this abstract class so as to override this method once and not on all classes.

Then i added the abstraction classes which are WebApp, DesktopApp, and MobileApp. the abstraction classes must extend from base class Application and must override the function query() for example in the WepApp class:

The most important part here is calling the handleQuery() method from IODatabaseDriver interface and this achieves the area of responsibility principal. This also achieves the main principal of this pattern is the first collection of strategies makes use of the second collection of strategies.

The second collection must contain the real implementations of the Api, in this case i just printed some text but in real world scenario the Database driver must handle querying the database.

 

 

Share this: