Bridge Design Pattern

What is Bridge Design Pattern?

Generally, a bridge is something that is used to overcome hurdles.

For example, consider there is a lake with a bridge. For crossing the lake, you use the bridge. 

You don’t want to know about the architecture of that bridge or about the raw materials that are used to construct that bridge.

This is called abstraction which means “ignoring the internal details about the implementations“. 

  • The Bridge Design Pattern comes under Structural Design Pattern
  • The primary motive of the Bridge Design Pattern is to allow the Abstraction and Implementation to act independently.

Real Life Examples of Bridge Design Pattern

Example 1:

file photoIf you are a software developer and if you are using some software for writing some data, you could just call an API for doing the job. Here, initially, they have used a file to store the data and then after two years, they updated the file mechanism to the database mechanism. These details would be abstracted to the client. If a client calls save data, the inner technical details would be handled by the implemented classes. You can replace any implementation at any time without affecting the abstraction layer. If after some time a new technology arrives, then that implementation gets updated to the new implementation, replacing your database with other technology. Here the client doesn’t need to worry about those things. If the client asks to get the object, if the object were stored in the file, then the object would be returned from the file. If the object were stored in the database, then from that database the object would be served.

Example 2:

iphone sample photoLet’s suppose if you are a retail shop owner and you send a message to the customers regarding the new products whenever they arrive at the shop. So, you need to intimate the customers via SMS, Email, Facebook, Messenger or Twitter. You will use some software for doing that job. You click on send the message with the help of that software.<br />Here, the inner technical details are abstracted from the implementation. Let’s suppose if a customer using mobile then when you click on send the message, then SMS will be sent to the client. Let’s assume that the customer uses email then triggering that button would send the email to the client. So here the technical information would be hidden from for the client. Thus, the client can call some methods without knowing the technical information.

Bridge Design Pattern UML Class Diagram

Bridge Design Pattern Java Implementation

Step 1:

public interface FileDownloaderInterface
{
    public Object download(String path);

    public boolean store(Object object);
}

Step 2:

public class FileDownloaderAbstractImpl implements FileDownloaderInterface
{

    private FileDownloadImplementor fileDownloader = null;

    public FileDownloaderAbstractImpl(FileDownloadImplementor provider)
    {
        super();
        this.fileDownloader = provider;
    }

    @Override
    public Object download(String path)
    {
        return fileDownloader.downloadFile(path);
    }

    @Override
    public boolean store(Object object)
    {
        return fileDownloader.storeFile(object);
    }
}

Step 3:

public interface FileDownloadImplementor
{
    public Object downloadFile(String path);
     
    public boolean storeFile(Object object);
}

Step 4:

public class WindowsFileDownloadImplementor implements FileDownloadImplementor
{
    @Override
    public Object downloadFile(String path) {
        return new Object();
    }
 
    @Override
    public boolean storeFile(Object object) {
        System.out.println("File stored successfully in WINDOWS machine");
        return true;
    }
}

Step 5:

public class LinuxFileDownloadImplementor implements FileDownloadImplementor
{
    @Override
    public Object downloadFile(String path) {
        return new Object();
    }
 
    @Override
    public boolean storeFile(Object object) {
        System.out.println("File stored successfully in LINUX machine");
        return true;
    }
}

Step 6:

public class BridgeEx
{

    public static void main(String[] args)
    {
        String os = "linux";
        FileDownloaderInterface downloader = null;

        switch (os)
        {
            case "windows":
                downloader = new FileDownloaderAbstractImpl(new WindowsFileDownloadImplementor());
                break;

            case "linux":
                downloader = new FileDownloaderAbstractImpl(new LinuxFileDownloadImplementor());
                break;

            default:
                System.out.println("OS not supported !!");
        }

        Object fileContent = downloader.download("some path");
        downloader.store(fileContent);
    }
}

Tutorial Videos of Bridge Design Pattern

Play Video
Play Video