Bridge Pattern

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

Decouple an abstraction from its implementation so that the two can vary independently.

Participants

  • Abstraction: defines the abstraction’s interface. Maintains a reference to an object of type Implementor.
  • RefinedAbstraction: extends the interface defined by Abstraction.
  • Implementor: defines the interface for implementation classes. This interface doesn’t have to correspond exactly to Abstraction’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.
  • ConcreteImplementor: implements the Implementor interface and defines its concrete implementation.

Code

public class Main {

    public static void main(String[] args) {
        Abstraction ab = new RefinedAbstraction();
        ab.implementor = new ConcreteImplementorA();
        ab.operation();
        ab.implementor = new ConcreteImplementorB();
        ab.operation();
    }
}

public class Abstraction {

    protected Implementor implementor;

    public void setImplementor(Implementor implementor) {
        this.implementor = implementor;
    }

    public void operation() {
        implementor.operation();
    }
}

public interface Implementor {

    public void operation();
}

public class RefinedAbstraction extends Abstraction {

    @Override
    public void operation() {
        implementor.operation();
    }
}

public class ConcreteImplementorA implements Implementor {

    @Override
    public void operation() {
        System.out.println("ConcreteImplementorA Operation");
    }
}

public class ConcreteImplementorB implements Implementor {

    @Override
    public void operation() {
        System.out.println("ConcreteImplementorB Operation");
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

ConcreteImplementorA Operation
ConcreteImplementorB Operation

Enter fullscreen mode Exit fullscreen mode

eidherjulian61 / design-patterns

图片[1]-Bridge Pattern - 拾光赋-拾光赋

Main Design Patterns

eidher ・ Sep 27 ’20

#designpatterns #creational #structural #behavioral

Design Patterns (24 Part Series)

1 Main Design Patterns
2 Abstract Factory Pattern
20 more parts…
3 Adapter Pattern
4 Decorator Pattern
5 Strategy Pattern
6 Observer Pattern
7 Builder Pattern
8 Factory Method Pattern
9 Prototype Pattern
10 Singleton Pattern
11 Bridge Pattern
12 Composite Pattern
13 Facade Pattern
14 Flyweight Pattern
15 Proxy Pattern
16 Chain of Responsibility Pattern
17 Command Pattern
18 Interpreter Pattern
19 Iterator Pattern
20 Mediator Pattern
21 Memento Pattern
22 State Pattern
23 Template Method Pattern
24 Visitor Pattern

原文链接:Bridge Pattern

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容