Bridge Design Pattern – Design Patterns

Bridge Design Pattern – Design Patterns

Bridge Design Pattern lets you build the abstraction separate from the implementation. Hence, this is very handy when one product is developed for more than one customer. Let’s learn how the pattern is implemented.

Benefits

  1. Lets you to build the abstraction part and the implementation part separately.
  2. The abstraction keeps a reference to the implementation.
  3. Both the abstraction and the implementation are abstract.
  4. You can generate different versions of the abstraction and the implementation part.
  5. The degree of the coupling decreases.
  6. The design is extensible.

Bridge Design Pattern Structure

Below class-diagram describes well the structure of the design pattern.

Bridge Design Pattern
Bridge Design Pattern
  • Abstraction: Base abstraction class. Keeps a reference to the Implementor.
  • Implementor: Implementation class which is also abstract.
  • Refined Abstraction: A desired version of the Abstraction. Keeps a reference to the ConcreteImplementor.
  • Concrete Implementor: A desired version of the Implementor.

The Scenario

For example, I am a software producer for the clothing manufacturer fabricates. I have a product: “ClotheManufacturer”. I have two customers: “BlueClothings” and “RedClothings”. BlueClothings company produces shirts, RedClothings company produces shorts. Here is my code for my product:

Abstraction

To begin with, the below code shows the abstraction part. First, the abstractions are shown, then the concrete classes will be demonstrated.


1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class ClotheManufacturer {
    protected final ClotheTypeProducer clotheTypeProducer;
    private final String producingClotheText;

    protected ClotheManufacturer(ClotheTypeProducer clotheTypeProducer, String producingClotheText) {
        this.clotheTypeProducer = clotheTypeProducer;
        this.producingClotheText = producingClotheText;
    }

    public void manufacture() {
        System.out.println(this.producingClotheText + this.clotheTypeProducer.getClotheType());
    }
}

Implementor

Second, the implementor part comes. The implementor class is also an abstract class. It provides the independent updates.


1
2
3
public abstract class ClotheTypeProducer {
    public abstract String getClotheType();
}

Concrete classes for BlueClothing

In the meantime, we build the concrete parts for BlueClothing.


1
2
3
4
5
6
7
public class BlueClotheManufacturer extends ClotheManufacturer {

    public BlueClotheManufacturer(ClotheTypeProducer clotheTypeProducer) {
        super(clotheTypeProducer, "Producing blue clothes with type: ");
    }

}

1
2
3
4
5
6
public class ShirtsProducer extends ClotheTypeProducer {
    @Override
    public String getClotheType() {
        return "shirts";
    }
}

Concrete classes for RedClothing

Without delay, RedClothing kicks in.


1
2
3
4
5
public class RedClotheManufacturer extends ClotheManufacturer {
    public RedClotheManufacturer(ClotheTypeProducer clotheTypeProducer) {
        super(clotheTypeProducer, "Producing red clothes with type: ");
    }
}

1
2
3
4
5
6
public class ShortsProducer extends ClotheTypeProducer {
    @Override
    public String getClotheType() {
        return "shorts";
    }
}

Our sample main class

To sum up, we use our base and concrete classes in our main class.


1
2
3
4
5
6
7
8
9
10
11
12
13
public class BridgePatternMainClass {
    public static void main(String[] args) {
        //Company BlueClothings
        ClotheTypeProducer shirtsProducer = new ShirtsProducer();
        ClotheManufacturer blueClotheManufacturer = new BlueClotheManufacturer(shirtsProducer);
        blueClotheManufacturer.manufacture();

        //Company RedClothings
        ClotheTypeProducer shortsProducer = new ShortsProducer();
        ClotheManufacturer redClotheManufacturer = new RedClotheManufacturer(shortsProducer);
        redClotheManufacturer.manufacture();
    }
}

The output

The output shows how your structure works correctly. Altogether, we will see how it is easy to change components independently.


1
2
Producing blue clothes with type: shirts
Producing red clothes with type: shorts

The Ease of Change

As a matter of fact, with the benefits of this pattern, we can easily change our components. Let’s say we want to change the base behavior and the RedClothing clothe type. We will just update two classes as shown below:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public abstract class ClotheManufacturer {
    protected final ClotheTypeProducer clotheTypeProducer;
    private final String producingClotheText;

    protected ClotheManufacturer(ClotheTypeProducer clotheTypeProducer, String producingClotheText) {
        this.clotheTypeProducer = clotheTypeProducer;
        this.producingClotheText = producingClotheText;
    }

    public void manufacture() {
        System.out.println("Manufacturing begins..");
        System.out.println(this.producingClotheText + this.clotheTypeProducer.getClotheType());
        System.out.println("Manufacturing completed successfully!");
        System.out.println();
    }
}

public class ShortsProducer extends ClotheTypeProducer {
    @Override
    public String getClotheType() {
        return "striped shorts";
    }
}

As a result of this, you can see the updated output below.


1
2
3
4
5
6
7
Manufacturing begins..
Producing blue clothes with type: shirts
Manufacturing completed successfully!

Manufacturing begins..
Producing red clothes with type: striped shorts
Manufacturing completed successfully!

Also, it is very easy to add a new part for a new customer. Thus, this pattern is also extensible.

Conclusion

In brief, the Bridge design pattern is one of the most important patterns. It is really handy in lots of situations. If you have any questions, please leave in the comments section. Don’t forget to be subscribed!

Also, you can read my previous post about the Adapter pattern.

For more information about design patterns, follow this link.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *