Bridge Design Pattern

The Bridge Design Pattern is one of the Structural Design Pattern its also known as Handle-Body Design Pattern.

some times 2 independent entities work together, and both have their own inheritance hierarchy. Bridge Design Pattern provides a relation or bride structure between both entities and is used when both entities are dependent on each other but the implementation needs to be abstracted. it decouples the functional abstraction from implementation and hides the details. both entities can grow independently and open for extensions but are restricted from modification, hence following the Open Close Principle.
both entities are connected via composition hence any modification done will not affect the implementation each entity has a Single Responsibility. it prefers Composition over Inheritance.

Bridge Design Pattern Decouple an abstraction from its implementation so that both can vary independently.

some examples of Bridge Design Pattern are
a shop can have multiple items which will be fulfilled by different vendors, both shop and vendor are independent entities but are dependent on each other. both will have their own hierarchy and will grow independently. a customer doesn't need to know which vendor is fulfilling the order.
 

package org.wesome.design.patterns;

public interface Vendor {
    String placeOrder();
}
package org.wesome.design.patterns;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public abstract class Shop {
    protected Vendor vendor;
    abstract public String fullFillOrder();
}
package org.wesome.design.patterns;
public class ShopA extends Shop {
    public ShopA(Vendor vendor) {
        super(vendor);
    }
    @Override
    public String fullFillOrder() {
        return "Apple brought from Shop A " + vendor.placeOrder();
    }
}
package org.wesome.design.patterns;
public class ShopB extends Shop {
    public ShopB(Vendor vendor) {
        super(vendor);
    }
    @Override
    public String fullFillOrder() {
        return "Apple brought from Shop B " + vendor.placeOrder();
    }
}
package org.wesome.design.patterns;
public class VendorA implements Vendor {
    @Override
    public String placeOrder() {
        return "Order fullFill by Vendor A";
    }
}
package org.wesome.design.patterns;

public class VendorB implements Vendor {
    @Override
    public String placeOrder() {
        return "Order fullFill by Vendor B";
    }
}
package org.wesome.design.patterns;
public class Bridge {
    public static void main(String[] args) {
        Shop square = new ShopA(new VendorA());
        System.out.println("Bridge.main" + square.fullFillOrder());
        Shop square1 = new ShopA(new VendorB());
        System.out.println("Bridge.main" + square1.fullFillOrder());
        Shop square2 = new ShopB(new VendorA());
        System.out.println("Bridge.main" + square2.fullFillOrder());
        square = new ShopB(new VendorB());
        System.out.println("Bridge.main" + square.fullFillOrder());
    }
}

package org.wesome.design.patterns;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class BridgeTest {
    @Test
    void testShopAVendorA() {
        Shop square = new ShopA(new VendorA());
        Assertions.assertEquals(square.fullFillOrder(), "Apple brought from Shop A Order fullFill by Vendor A");
    }

    @Test
    void testShopAVendorB() {
        Shop square = new ShopA(new VendorB());
        Assertions.assertEquals(square.fullFillOrder(), "Apple brought from Shop A Order fullFill by Vendor B");
    }

    @Test
    void testShopBVendorA() {
        Shop square = new ShopB(new VendorA());
        Assertions.assertEquals(square.fullFillOrder(), "Apple brought from Shop B Order fullFill by Vendor A");
    }

    @Test
    void testShopBVendorB() {
        Shop square = new ShopB(new VendorB());
        Assertions.assertEquals(square.fullFillOrder(), "Apple brought from Shop B Order fullFill by Vendor B");
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on