Blog Infos
Author
Published
Topics
, , ,
Published
Why should you use Bridge?

The primary purpose of this pattern is to split a class into two abstract hierarchies of classes so that they can grow independently. It separates the responsibilities of a single class into multiple classes with a single responsibility.

Consider using this pattern if your class is becoming too big or too complex.

Advantages
  • Splitted Responsibility, thus making it easier to find a fragment of code.
  • Single Responsibility. Each class has its responsibility.
  • Open/Closed Principle. You can introduce new hierarchies without changing the previous ones.
  • Codebase grows linearly instead of exponentially.
  • Code Encapsulation. You’re working with abstractions instead of concrete classes.
Disadvantages
  • The classes might become too cohesive on each other, making them unreusable.
  • You’re introducing additional layers of abstraction into your code, which could make your codebase more complex instead of easier to maintain.
Example

You’re working on a project, and your task is to implement a Table and Chair production feature. Both of them can be created from Wood and Metal. You’ve also heard that they’ll likely add more types of Furniture and Materials.

That means adding new Material shouldn’t interfere with Furniture and the other way around.

Here’s a class diagram of how we’ll structure our code:

Bridge diagram

 

We could also keep adding WoodenChair , MetalChair etc., but then we’ll need Materials * Furniture number of classes instead of Materials + Furniture number of classes. Our code will grow linearly instead of exponentially.

You could also make the Furniture have a Material instead of making the concrete classes do that. Then the diagram would look like this:

Bridge with interface aggregation

 

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

I prefer the first one because it’s better when concrete classes depend on others, not abstract ones. Let’s start by coding Material:

interface Material {
    fun collect(): String
}

class Wood : Material {
    override fun collect() = "wood"
}

class Metal : Material {
    override fun collect() = "metal"
}

Now that we have all the required Material classes, let’s create Furniture :

interface Furniture {
    fun build()
}

class Chair(
    private val material: Material,
) : Furniture {
    override fun build() {
        println("Building Chair from " + material.collect())
    }
}

class Table(
    private val material: Material,
) : Furniture {
    override fun build() {
        println("Building Table from " + material.collect())
    }
}

// Usage
fun main() {
    val woodenTable: Furniture = Table(material = Wood())
    val metalTable: Furniture = Table(material = Metal())
    woodenTable.build() // Building Table from wood
    metalTable.build() // Building Table from metal

    val woodenChair: Furniture = Chair(material = Wood())
    val metalChair: Furniture = Chair(material = Metal())
    woodenChair.build() // Building Chair from wood
    metalChair.build() // Building Chair from metal
}

Now, it’s just an example, so I’m keeping things simple, but Table , Chair might have more details same goes for Metal and Wood . In that case, it’ll also help separate some logic.

Thanks for reading! I hope you’ve learned something new. Please clap and follow me for more!

More design patterns

https://medium.com/@michalankiersztajn/list/design-patterns-in-kotlin-12e52466affe?source=post_page—–63d767eedb2a——————————–

Based on the book:

“Wzorce projektowe : elementy oprogramowania obiektowego wielokrotnego użytku” — Erich Gamma Autor; Janusz Jabłonowski (Translator); Grady Booch (Introduction author); Richard Helm (Author); Ralph Johnson (Author); John M Vlissides (Author)

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu