Design Patterns: Bridge
Tags: computerscience, beginners, programming, architecture
Published: 2021-08-23 05:54:27

<!-- wp:paragraph -->
<p>As the series goes on, the weirder the names get?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Bridge pattern?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We ain't literally constructing a bridge it's more of a metaphor.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Today you will learn:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Core concepts behind the bridge pattern</li><li>How to implement the bridge pattern</li><li>How to recognize opportunities the bridge pattern</li><li>The benefits and downsides of the bridge pattern</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Definition</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The bridge pattern states that you have to build an ASCII bridge between your modules, like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":679,"sizeSlug":"full","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/2-1.png" alt="" class="wp-image-679"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Just kidding!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In all seriousness, the bridge pattern advocates for the <strong>separation</strong> of <strong>abstraction</strong> and <strong>implementation</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But what do you mean by abstraction and implementation?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Well, to make things simpler let's go over an example.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Problem</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Imagine your amazon in its early days.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Back when they only sold hardcover books.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Their classes might look something like this:</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":678,"sizeSlug":"full","linkDestination":"none"} -->
<div class="wp-block-image"><figure class="aligncenter size-full"><img src="https://softwareadventuring.com/wp-content/uploads/2021/08/1-1.png" alt="" class="wp-image-678"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This most definitely works.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But it looks a bit iffy.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If we wanted to create a new book called <code>ActionBook</code> and want to publish it both as a printed book and as an audiobook.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We would be forced to create two new classes:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><code>ActionPrintBook</code></li><li><code>ActionAudioBook</code></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>But what if we also want to publish a digital version of the book?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once again, we will be forced to create a new <code>ActionDigitalBook</code> class.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In a nutshell for every new book, we have to create up to 3 additional classes for each version.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, if you want to do any changes to the book, you will have to do them in multiple places, hence breaking the DRY (Don't Repeat Yourself) principle.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Over time, this gets messy very fast.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But no worries, this is where the bridge pattern shines.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Solution</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If we examine the classes closely, what's the difference between the audio book and the print book?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You will obviously say the content, the print book outputs a bunch of text, while the audio book outputs audio.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Our first step would be to decouple the content from the book classes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's create a content provider interface:</p>
<!-- /wp:paragraph -->
````Java
interface IBookContentProvider {
	public String[] getContent();
} 

class AutobiographyBookContentProvider implements IBookContentProvider {

	public String[] getContent(){
		return ["I was a boy", "now I'm not a boy"];
	}
}

class AutobiographyAudioBook implements AudioBook {
		IBookContentProvider contentProvider;

		public AutobiographyAudioBook(IBookContentProvider contentProvider){
			self.contentProvider = contentProvider;
		}
		
		public void publish(){
			String[] content = self.contentProvider.getContent()

			System.out.println("Published Book: " + content)
		}
}
```

<!-- wp:paragraph -->
<p>Let's recap what we did over here:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>First, we created an <code>IBookContentProvider</code> interface, that declares the method <code>getContent</code> that will essentially get our content.</li><li>Then we created our first content provider called <code>AutobiographyBookContentProvider</code> that implements our <code>IBookContentProvider</code> interface.</li><li>Finally we connected our <code>AutobiographyBookContentProvider</code> to our <code>AutobiographyAudioBook</code> through the constructor.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Right, now if we ever wanted to create a new book, we would simply create a new book content provider and simply connect it to our book types (print, audiobook, digital, etc...)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is the basic premise of the bridge pattern.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It advocates for composition over inheritance.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>When to use this pattern?</h2>
<!-- /wp:heading -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Use the bridge pattern, when you want to decouple a monolithic class that has several variants of some functionality.</strong> The bigger the class becomes, the more complicated it gets. It get's harder to make changes, and there's is a high possibility of things simply breaking out. This is where the bridge pattern comes, and divides this huge monolithic class into several class hierarchies. You can change code in each class hierarchy independently, hence making it much more easier and maintainable to work with.</li><li><strong>Use the bridge pattern, when you want to extend a class into several independent variants.</strong> The bridge pattern advocates to separate classes into hierarchies. You essentially create building blocks which all work together that you can mix and match.</li><li><strong>Use the Bridge if you need to be able to switch implementations at runtime.</strong> If you have implemented the bridge pattern, it's very easy to change implementations. You simply have to change the classes that your passing.</li></ol>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Benefits and Downsides</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It's not all sunshine and rainbows, let's go over the benefits and downsides of the bridge pattern.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Benefits</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>You create platform independent classes and apps.</li><li>The client doesn't know the nitty gritty details, it only deals with abstractions.</li><li>You can introduce new abstractions and implementations independently.</li><li>You focus on the high level logic in abstraction, and details in implementations.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Downsides</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>The program becomes more complicated due to the addition of many different classes and interfaces.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The bridge pattern is one of the most useful design patterns, I'm sure you will find some use for it at both work and pet projects. I would like to end it with a question, have you ever used the bridge pattern? if so how was it? did you regret it?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Thanks for reading!</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Further Readings</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you want to learn more about the design patterns, I would recommend&nbsp;<a href="https://refactoring.guru/design-patterns">Diving into Design Patterns</a>. It explains all 23 design patterns found in the GoF book, in a fun and engaging manner.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Another book that I recommend is&nbsp;<a href="https://www.amazon.com/Head-First-Design-Patterns-Brain-Friendly/dp/0596007124">Heads First Design Patterns: A Brain-Friendly Guide</a>, which has fun and easy-to-read explanations.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The Plug</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Make sure to follow me on Twitter <a href="https://twitter.com/tamerlan_dev">@tamerlan_dev</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Also, check out my blog for more content like this:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://softwareadventuring.com/">https://softwareadventuring.com/</a></p>
<!-- /wp:paragraph -->