Bridge Method

Bridge Method

Bridge Method

A bridge method is a structural design pattern that permits us to separate big classes(large classes) into two separate classes (hierarchies). The implementation can be improved autonomously, it extends to a big hierarchy of classes, which separates them into two separate but interdependent hierarchies. The design pattern has to do with two major things and they are:
  • Abstraction (interface)
    • it defines the interface for the control part of the two-class hierarchies to which it delegates all the work to an object of the implementation hierarchy.
  • Implementation(platform)
    • it defines the interfaces for all implementation classes.
Implementation

The bridge design pattern is actualized using the following steps below:

  • First, identify the orthogonal dimensions(concepts) in your classes. These concepts could be in formats like front-end/back-end, abstraction/platform, domain/infrastructure or interface/implementation.
  •  Check the operations the client needs and add them to the base abstraction class.
  • Discover the operations available on all platforms and states the one abstraction needs in the general implementation interface.
  • Create a concrete implementation class for all platform in your domain but be sure they follow the implementation interface.
  • Add a reference field inside the abstraction class for the implementation type. The abstraction represents most of the work to the implementation object referenced in that field.
  • Extend the bass abstraction class and create a refined abstraction for each variant when you have several variants of high-level logic.
  • The user code should pass an implementation object to the abstraction’s constructor to associate one with the other.
Code Implementation
from __future__ import annotations
from abc import ABC, abstractmethod

class RemoteControl:
    """
    The RemoteControl defines the interface for the "control" part of the two
    class hierarchies. It maintains a reference to an object of the
    Implementation hierarchy and delegates all of the real work to this object.
    """
    def __init__(self, implementation: Device):
        self.implementation = implementation
    def operation(self):
        return (f"Remote Control: Base operation with:\n"
                f"{self.implementation.operation_implementation()}")

class AdvancedRemoteControl(RemoteControl):
    """
    You can extend the RemoteControl without changing the Implementation classes.
    """
    def operation(self):
        return (f"Advanced Remote Control: Extended operation with:\n"
                f"{self.implementation.operation_implementation()}")

class Device(ABC):
    """
    The Implementation defines the interface for all implementation classes. It
    doesn't have to match the RemoteControl's interface. In fact, the two
    interfaces can be entirely different. Typically the Implementation interface
    provides only primitive operations, while the RemoteControl defines higher-
    level operations based on those primitives.
    """

    @abstractmethod
    def operation_implementation(self):
        pass

class Tv(Device):
    def operation_implementation(self):
        return "Tv Device: Here's the result on the Tv set function."
class Radio(Device):
    def operation_implementation(self):
        return "Radio device: Here's the result on the Radio set function."

def client_code(abstraction: RemoteControl):
    """
    Except for the initialization phase, where an RemoteControl object gets linked
    with a specific Implementation object, the client code should only depend on
    the RemoteControl class. This way the client code can support any abstraction-
    implementation combination.
    """
    # ...
    print(abstraction.operation(), end="")

    # ...

if __name__ == "__main__":

   """
    The client code should be able to work with any pre-configured abstraction-
    implementation combination.
    """
    Tvset = Tv()
    remote = RemoteControl(Tvset)
    client_code(remote)

    print("\n")

    radio = Radio()
    Adremote = AdvancedRemoteControl(radio)
    client_code(Adremote)

The above program generates the following output −

ProBook:~/InfosecAddicts/python design pattern$ python bridge.py
Remote Control: Base operation with:
Tv Device: Here's the result on the Tv set function.

Advanced Remote Control: Extended operation with:

The code includes illustrations on the structure of the Bridge design pattern using the remote control scenario.

Pros and Cons

Pros
  • Single Responsibility Principle
  • Open/Closed Principle
  • Independent feature of the platform

 

Cons
  • Complexity
    • When you apply the pattern to a highly cohesive class you might make the code more complicated.
  • Interfaces with only single implementation
    • it is hard to manage when you have a large set of interfaces but the low number or one implementation.

 

 

Lesson Content