Java Bridge Design Pattern Example (original) (raw)

1. Introduction

In this article, we would be talking about one of the many Java Design Patterns – The Bridge Design pattern. In a large scale Java application built for enterprises, at times it becomes difficult to manage and understand the code. With a varying profile of developers working on the same project, it is necessary that the code being developed is understandable by new developers that join the project. This is possible only when there are standards in place. These standards should be meant to structural or develop the code in a way that is already expected by the new developers. The Java community has hence specified several design patterns. In each pattern there are certain pre-defined coding standards and structures to be followed. These specific standards and the structures of code assist in development of a code that is organised and easily manageable. Moreover, it also ascertains that the code is organised in a predefined manner and thus it is easily understandable by any new developer joining the project.

In order to ensure that these coding structures are standardised, Java defines a large bunch of design patterns which revolve around the various aspects of coding like how we define a specific class, how we link the classes to use other, how we leverage the core features of Java like the inheritence and interfaces, the creation of objects and the management of object behaviour.

2. Java Design patterns

There are three primary groups of these design patterns which are briefly explained below.

2.1 Creational Pattern

This group of patterns provides five different design patterns that primarily focus on the logic of creation and destruction of objects while concealing the actual implementation of the objects. This basically defines the standards for abstraction. Thus, this pattern controls the object access for each module that exists in the application.

2.2 Structural Pattern

This group of patterns provides us with seven different patterns to help the developers organise the class structure so that the desired classes and features are exposed in a desired manner. Additionally, they offer solutions to problems like interfacing different types of objects, entities or applications. They also guide in solving the problems of separating abstract classes from the actual implementation. This is where bridge pattern basically belongs.

2.3 Behavioural Pattern

This pattern category is mainly associated with the way objects communicate with each other. This includes messaging queues, passing messages via connecting classes and others. There are nearly eleven such patterns to define the communication methodologies.

In this article, we would be discussing one of the structural design patterns – the Bridge design pattern. The article would explain the concept behind this pattern with a real-life example to help you gain an insight on how the design pattern have really helped make a better project code. The next section covers the pattern logically to explain each component of the Bridge design pattern to prepare you to understand the code that lies ahead.

The Bridge configuration design enables you to isolate the abstraction from the implementation. It is a basic plan design.

There are 2 sections in Bridge configuration design :

This is an outline system that embodies an implementation class within an interface class.

The bridge design enables the Abstraction and the Implementation to be created freely and the customer code can get to just the Abstraction part without being worried about the Implementation part.

The abstraction is an interface or dynamic class and the practitioner is additionally an interface or conceptual class.

The abstraction contains a reference to the practitioner. Offspring of the abstraction are alluded to as refined abstractions, and offspring of the practitioner are concrete practitioners. Since we can change the reference to the practitioner in the abstraction, we can change the abstraction’s practitioner at run-time. Changes to the practitioner don’t influence customer code.

It expands the free coupling between class abstraction and it’s implementation.

The figure below elaborates the concept:

Java Bridge Design Pattern

Bridge Design Pattern

Abstraction – center of the bridge configuration design and characterizes the core. Contains a reference to the implementer.

Refined Abstraction – Extends the abstraction takes the better detail one level underneath. Conceals the better components from implemetors.

Implementer – It characterizes the interface for implementation classes. This interface does not have to compare straightforwardly to abstraction interface and can be altogether different. Abstraction devil gives an implementation regarding activities given by Implementer interface.

Solid Implementation – Implements the above implementer by giving solid implementation.

4. Coding Bridge Pattern

Now that we have understood the Bridge pattern basics, let us develop the code to understand the bridge pattern. Unlike the other design patterns, it is a bit complex to understand bridge pattern. Let us develop all the relevant classes before we understand the code. The code will be explained after the classes have been created.

Vehicle.java

0102030405060708091011121314 package com.javacodegeeks;abstract class Vehicle { protected Workshop w1; protected Workshop w2; protected Vehicle(Workshop w1, Workshop w2) { this.w1 = w1; this.w2 = w2; } abstract public void manufacture(); }

Workshop.java

1234567 package com.javacodegeeks;interface Workshop { abstract public void work(); }

Produce.java

01020304050607080910 package com.javacodegeeks;class Produce implements Workshop { @Override public void work() { System.out.print("Producing vehicle"); } }

Assemble.java

0102030405060708091011 package com.javacodegeeks;class Assemble implements Workshop { @Override public void work() { System.out.println("Assembing Vehicle."); } }

Bike.java

0102030405060708091011121314151617 package com.javacodegeeks;import com.javacodegeeks.Vehicle;class Bike extends Vehicle { public Bike(Workshop w1, Workshop w2) { super(w1, w2); } @Override public void manufacture() { System.out.print("Bike "); w1.work(); w2.work(); } }

Car.java

01020304050607080910111213141516 package com.javacodegeeks;class Car extends Vehicle { public Car(Workshop w1, Workshop w2) { super(w1, w2); } @Override public void manufacture() { System.out.print("Car "); w1.work(); w2.work(); } }

So far, we have created all the classes that depict the Bridge pattern. Each sub-component has two different classes with similar code. Now, the next class that we would develop is the actual main class that utilises this pattern to perform action.

BridgePattern.java

010203040506070809101112 package com.javacodegeeks;class BridgePattern { public static void main(String[] args) { Vehicle vehicle1 = new Car(new Produce(), new Assemble()); vehicle1.manufacture(); Vehicle vehicle2 = new Bike(new Produce(), new Assemble()); vehicle2.manufacture(); } }

This is the final code. On execution of the above code, you get the following output.

12 Assembling Vehicle Building Vehicle

In the above code, we created two implementations of a bridge class. These implementors assist in doing the tasks that the bridge class was created for. Thus, they utilise the abstract class to further advance the code and provides specific functionalities to the caller class. Although the code is self-explanatory, we would try and understand the same using the below images. Let us look at the below images. These images explain how the implementation would have been in absence of bridge pattern and how it changed by implementing using the bridge design pattern.

Java Bridge Design Pattern - Without Bridge Design Pattern

Without Bridge Design Pattern

Notice in the above image that if the bridge pattern would not have been implemented, there would have been unnecessary duplication of the production and assembly classes. Now let us see how this was changed in a Bridge design pattern.

Java Bridge Design Pattern - With Bridge Design Pattern

With Bridge Design Pattern

As it can be seen clearly, there is a direct reduction of redundant code due to a bridge between Vehicle and Workshop. Thus, Bridge pattern helps in highly simplifying the code. Now that we have clearly understood what bridge pattern is all about, let us look at its advantages and feasibility of its use.

5. Advantages of bridge design pattern

6. Drawbacks of bridge design pattern

7. Using Bridge Design Pattern

There are certain specific cases when using bridge design pattern actually makes sense. They are specified below:

8. A brief comparison of Bridge Design pattern with others

Comparison always provides a better insight on any item. In this section, we contrast Bridge design pattern with others and try to understand how bridge design pattern differs from others.

9. Java Bridge Design Pattern – Conclusion

The Bridge pattern certainly assists in reducing the replication of code for multiple classes. The bridge pattern helps in organising the code better. However, it also adds to the complexity as there is inter-linking of classes involves which increases the difficulty level for understanding the code. Occasionally, it might also make it difficult to trace due to concept of abstraction being used in this design pattern.

Bridge pattern mandates that any new person joining an enterprise scale project using this pattern, must have in-depth knowledge about this pattern. In absence of which, it would create an entry barrier for the user. In cases where the bridge classes increase or the inter-linking basically increases, it is better to avoid the design pattern rather than trying to use it as a standard.

10. Download the Source Code

That was an Example of Java Bridge Design Pattern. Below you can find the Eclipse project containing the source code of the example.

Download
You can download the full source code of this example here: Bridge Pattern.zip

Photo of Abhishek Kothari

Abhishek is a Web Developer with diverse skills across multiple Web development technologies. During his professional career, he has worked on numerous enterprise level applications and understood the technological architecture and complexities involved in making an exceptional project. His passion to share knowledge among the community through various mediums has led him towards being a Professional Online Trainer, Youtuber as well as Technical Content Writer.