Difference between Bridge pattern and Adapter pattern

后端 未结 9 1001
清歌不尽
清歌不尽 2020-11-29 19:16

What is the difference between the Bridge and Adapter patterns?

相关标签:
9条回答
  • 2020-11-29 19:57

    Bridge is improved Adapter. Bridge includes adapter and adds additional flexibility to it. Here is how elements from Ravindra's answer map between patterns:

          Adapter  |    Bridge
        -----------|---------------
        Target     | Abstraction
        -----------|---------------
                   | RefinedAbstraction
                   |
                   |   This element is Bridge specific. If there is a group of 
                   |   implementations that share the same logic, the logic can be placed here.
                   |   For example, all cars split into two large groups: manual and auto. 
                   |   So, there will be two RefinedAbstraction classes.
        -----------|--------------- 
        Adapter    | Implementor
        -----------|---------------
        Adaptee    | ConcreteImplementor
    
    0 讨论(0)
  • 2020-11-29 20:01

    Looks like shorter and clear answer to me according to another stackoverflow answer here:

    • Adapter is used when you have an abstract interface, and you want to map that interface to another object which has similar functional role, but a different interface.

    • Bridge is very similar to Adapter, but we call it Bridge when you define both the abstract interface and the underlying implementation. I.e. you're not adapting to some legacy or third-party code, you're the designer of all the code but you need to be able to swap out different implementations.

    0 讨论(0)
  • 2020-11-29 20:02

    Adapter:

    1. It is a structural pattern
    2. It is useful to work with two incompatible interfaces

    UML Diagram: from dofactory article:

    Target : defines the domain-specific interface that Client uses.

    Adapter : adapts the interface Adaptee to the Target interface.

    Adaptee : defines an existing interface that needs adapting.

    Client : collaborates with objects conforming to the Target interface.

    Example:

    Square and Rectangle are two different shapes and getting area() of each of them requires different methods. But still Square work on Rectangle interface with conversion of some of the properties.

    public class AdapterDemo{
        public static void main(String args[]){
            SquareArea s = new SquareArea(4);
            System.out.println("Square area :"+s.getArea());
        }
    }
    
    class RectangleArea {
        public int getArea(int length, int width){
            return length * width;
        }
    }
    
    class SquareArea extends RectangleArea {
    
        int length;
        public SquareArea(int length){
            this.length = length;
        }
        public int getArea(){
            return getArea(length,length);
        }
    }
    

    Bridge:

    1. It is structural pattern
    2. it decouples an abstraction from its implementation and both can vary independently
    3. It is possible because composition has been used in place of inheritance

    EDIT: ( as per @quasoft suggestion)

    You have four components in this pattern.

    1. Abstraction: It defines an interface

    2. RefinedAbstraction: It implements abstraction:

    3. Implementor: It defines an interface for implementation

    4. ConcreteImplementor: It implements Implementor interface.

    Code snippet:

    Gear gear = new ManualGear();
    Vehicle vehicle = new Car(gear);
    vehicle.addGear();
    
    gear = new AutoGear();
    vehicle = new Car(gear);
    vehicle.addGear();
    

    Related post:

    When do you use the Bridge Pattern? How is it different from Adapter pattern?

    Key differences: from sourcemaking article

    1. Adapter makes things work after they're designed; Bridge makes them work before they are.
    2. Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together.
    0 讨论(0)
提交回复
热议问题