<p><strong>Bridge Design Pattern</strong> is used to decouples an abstraction used the client code from its implementation that means it separates the abstraction and its implementation in separate class hierarchies. And also Bridge pattern prefers the composition over the inheritance because inheritance isn’t always flexible and it breaks the encapsulation, so any change made in the implementor that affects the abstraction used by client code.</p>
<p>In software engineering, one of popular notion is <em>&#8220;<strong>prefer composition over inheritance</strong>&#8220;</em>. Bridge design pattern promotes this popular notion. Similar to the other pattern, this pattern also comes under the <strong><a href="https://dineshonjava.com/structural-design-patterns/">structural design pattern</a></strong> family of GoF Design pattern.</p>
<h2>Bridge Design Pattern</h2>
<div style="text-align: left;">
<blockquote><p><strong>According to the Gang of Four:</strong></p>
<div style="text-align: left;">Decouple an abstraction from its implementation so that the two can vary independently.</div>
</blockquote>
</div>
<p>Bridge pattern uses an interface as bridge between the concrete classes of an abstract class and implementing classes of that interface. You can make changes in both types of classes without any impact on the client code.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<h2>Applicability</h2>
<p>There are following common problems solved by the <strong>Bridge Design Pattern.</strong></p>
<ul>
<li>Remove a permanent binding between the functional abstraction and its implementation.</li>
<li>You able to make changes into implementing classes without affecting the its abstraction and client code.</li>
<li>You can extends the abstraction and its implementation using sub classes.</li>
</ul>
<div style="text-align: left;">
<blockquote><p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/adapter-design-pattern/" href="https://dineshonjava.com/adapter-design-pattern/">Adapter Design Pattern</a></li>
<li><a title="https://dineshonjava.com/composite-pattern-design-patterns-java/" href="https://dineshonjava.com/composite-pattern-design-patterns-java/">Composite Design Pattern</a></li>
<li><a title="https://dineshonjava.com/decorator-design-pattern/" href="https://dineshonjava.com/decorator-design-pattern/">Decorator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/proxy-pattern-design-patterns-java/" href="https://dineshonjava.com/proxy-pattern-design-patterns-java/">Proxy Design Pattern</a></li>
<li><a title="https://dineshonjava.com/builder-design-pattern/" href="https://dineshonjava.com/builder-design-pattern/">Builder Design Pattern</a></li>
<li><a title="https://dineshonjava.com/creational-design-patterns/" href="https://dineshonjava.com/creational-design-patterns//">Creational GoF Design Patterns</a></li>
<li><a title="https://dineshonjava.com/behavioral-design-pattern/" href="https://dineshonjava.com/behavioral-design-pattern/">Behavioral GoF Design Patterns</a></li>
</ul>
</blockquote>
</div>
<h2>UML class diagram of the Bridge Pattern</h2>
<p>Let&#8217;s see the following class diagram. It illustrates about the component classes and interfaces.</p>
<p><img class="aligncenter size-full wp-image-3687" src="https://dineshonjava.com/wp-content/uploads/2017/12/bridge.gif" alt="Bridge Design Pattern" width="448" height="293" /></p>
<p>The classes and objects participating in this pattern are:<br />
<strong>Abstraction (Bank)</strong></p>
<ul>
<li>It defines the abstraction&#8217;s interface.</li>
<li>It maintains a reference to an object of type Implementor.</li>
</ul>
<p><strong>RefinedAbstraction (IciciBank, HdfcBank)</strong></p>
<ul>
<li>It extends the interface defined by Abstraction.</li>
</ul>
<p><strong>Implementor (Account)</strong></p>
<ul>
<li>It defines the interface for implementation classes. This interface doesn&#8217;t have to correspond exactly to Abstraction&#8217;s interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.</li>
</ul>
<p><strong>ConcreteImplementor (CurrentAccount, SavingAccount)</strong></p>
<ul>
<li>It implements the Implementor interface and defines its concrete implementation.</li>
</ul>
<h2>Pros of the Bridge Pattern</h2>
<p>There are following pros of the Bridge Design Pattern.</p>
<ul>
<li>This design pattern allows you to make separation between the implementation and the abstraction.</li>
<li>Bridge design pattern provides flexibility to change in both types of classes without side effect into client code.</li>
<li>This pattern allows the hiding of actual implementation details from the client by using abstraction between them.</li>
</ul>
<h2>Bridge Vs Adapter Design Pattern</h2>
<p>The <strong><a href="https://dineshonjava.com/adapter-design-pattern/">adapter design pattern</a></strong> helps it two incompatible classes to work together. But, <strong>bridge design pattern</strong> decouples the abstraction and implementation by creating two different hierarchies.</p>
<h2>Sample Implementation of Bridge Design Pattern</h2>
<p>You look in the following example where I am going to demonstrate use of Bridge design pattern. Here I am taking two accounts of any <strong>Bank</strong>, you can open two types of accounts one is <em><strong>SavingAccount</strong> </em>and other is <em><strong>CurrentAccount</strong></em>. Let&#8217;s see the relationship between the abstraction of a <em><strong>Bank</strong> </em>and <em><strong>Account</strong></em>.</p>
<h3>Relationship without using Bridge Design Pattern</h3>
<p>Let&#8217;s start to create a design without using Bridge Design Pattern. First create an interface or an abstract class, <em><strong>Bank</strong></em>. And then I create its derived classes: <em><strong>IciciBank</strong> </em>and <em><strong>HdfcBank</strong></em>. To open an account in bank, first decide type of accounts classes: <em><strong>SavingAccount</strong> </em>and <em><strong>CurrentAccount</strong></em>, these classes extends the specific banks classes (<em><strong>HdfcBank</strong> </em>and <em><strong>IciciBank</strong></em>). This simple deep inheritance hierarchy in this application.</p>
<p>So what is wrong with this design as below figure. You will notice in this design, there are two parts, one is the abstraction part and other is the implementation part. Client code interacts with the abstraction part. Client code can only access new change or new functionality of the implementation part when you will update the abstraction part, it means both parts the abstraction and implementation are tightly coupled with each-other.</p>
<p><img class="aligncenter size-full wp-image-3690" src="https://dineshonjava.com/wp-content/uploads/2017/12/without-bridge-pattern.png" alt="without bridge pattern" width="715" height="249" /></p>
<h3>Relationship using Bridge Design Pattern</h3>
<p>Let&#8217;s look into the following figure, how Bridge design pattern solve these design issues as noticed in the without using bridge pattern example. Bridge pattern is separating the abstraction and implementation into two class hierarchies.</p>
<p><img class="aligncenter size-full wp-image-3691" src="https://dineshonjava.com/wp-content/uploads/2017/12/with-bridge-pattern.png" alt="with bridge pattern" width="793" height="194" /></p>
<p>We have a <strong>Account </strong>interface which is acting as a bridge implementer and concrete classes <strong>SavingAccount</strong>, <strong>CurrentAccount </strong>implementing the Account interface. <strong>Bank </strong>is an abstract class and will use object of <strong>Account</strong>.</p>
<h3>Step 1: Let&#8217;s create bridge implementer interface.</h3>
<p><strong>Account.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 *	Implementor for bridge pattern 
 */ 
public interface Account { 
	Account openAccount(); 
	void accountType(); 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create concrete bridge implementer classes implementing the <em>Account</em> interface.</h3>
<p><strong>CurrentAccount.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 * Concrete implementation 1 for bridge pattern 
 */ 
public class CurrentAccount implements Account { 
 
	@Override 
	public Account openAccount() { 
		System.out.println("OPENED: CURRENT ACCOUNT "); 
		return new CurrentAccount(); 
	} 
	 
	@Override 
	public void accountType() { 
		System.out.println("##It is a CURRENT Account##"); 
	} 
} 
 
</pre>
<p><strong>SavingAccount.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 * Concrete implementation 2 for bridge pattern 
 */ 
public class SavingAccount implements Account { 
 
	@Override 
	public Account openAccount() { 
		System.out.println("OPENED: SAVING ACCOUNT "); 
		return new SavingAccount(); 
	} 
 
	@Override 
	public void accountType() { 
		System.out.println("##It is a SAVING Account##"); 
	} 
 
} 
 
</pre>
<h3>Step 3: Let&#8217;s create an abstract class <em>Bank</em> using the <em>Account</em> interface.</h3>
<p><strong>Bank.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 *	Abstraction in bridge pattern 
 */ 
public abstract class Bank { 
	//Composition with implementor 
	protected Account account; 
	 
	public Bank(Account account){ 
		this.account = account; 
	} 
	 
	abstract Account openAccount(); 
} 
 
</pre>
<h3>Step 4: Let&#8217;s create concrete class implementing the <em>Bank</em> interface.</h3>
<p><strong>IciciBank.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 * Refine abstraction 1 in bridge pattern 
 */ 
public class IciciBank extends Bank { 
 
	public IciciBank(Account account) { 
		super(account); 
	} 
 
	@Override 
	Account openAccount() { 
		System.out.print("Open your account with ICICI Bank"); 
		return account; 
	} 
 
} 
 
</pre>
<p><strong>HdfcBank.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 * Refine abstraction 2 in bridge pattern 
 */ 
public class HdfcBank extends Bank { 
 
	public HdfcBank(Account account) { 
		super(account); 
	} 
	@Override 
	Account openAccount() { 
		System.out.print("Open your account with HDFC Bank"); 
		return account; 
	} 
 
} 
 
</pre>
<h3>Step 5: Let&#8217;s create a demo class and use the <em>Bank</em> and <em>Account</em> classes to open different types of accounts Saving and Current Account.</h3>
<p><strong>BridgePatternMain.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.bridge; 
 
/** 
 * @author Dinesh.Rajput 
 * Demonstration of bridge design pattern 
 */ 
public class BridgePatternMain { 
 
	public static void main(String[] args) { 
		Bank icici = new IciciBank(new CurrentAccount()); 
		Account current = icici.openAccount(); 
		current.accountType(); 
		 
		Bank hdfc = new HdfcBank(new SavingAccount()); 
		Account saving = hdfc.openAccount(); 
		saving.accountType(); 
	} 
 
} 
 
</pre>
<h3>Step 6: Let&#8217;s run the above demo class and verify the output.</h3>
<pre class="highlight">Open your account with ICICI Bank##It is a CURRENT Account## 
Open your account with HDFC Bank##It is a SAVING Account## 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/adapter-design-pattern/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/decorator-design-pattern/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3686'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…