桥接模式表示 "将功能的抽象化与其实现分离,使两者可以独立变化"

桥接模式也称为 Handle 或 Body

桥接模式的优点

  • 它实现了接口与实现部分的分离。
  • 提高了系统的扩展性。
  • 允许隐藏实现细节,使客户端不受影响。

桥接模式的使用

  • 当你不希望在功能抽象和其实现部分之间有永久绑定时。
  • 当功能抽象和实现部分都需要通过子类进行扩展时。
  • 多用于实现的修改不影响客户端的场合。

桥接模式示例

下面的 UML 描述了桥接模式的示例。

桥接模式的 UML:

2-1.jpg

实现上述 UML:

步骤 1

创建一个 Question 接口,提供从一个问题导航到另一个问题的功能。

public interface Question {
    public void nextQuestion();
    public void previousQuestion();
    public void newQuestion(String q);
    public void deleteQuestion(String q);
    public void displayQuestion();
    public void displayAllQuestions();
}

步骤 2

创建一个实现 Question 接口的 JavaQuestions 类。

import java.util.ArrayList;
import java.util.List;

public class JavaQuestions implements Question {
    private List<String> questions = new ArrayList<String>();
    private int current = 0;

    public JavaQuestions() {
        questions.add("What is class?");
        questions.add("What is interface?");
        questions.add("What is abstraction?");
        questions.add("How multiple polymorphism is achieved in java?");
        questions.add("How many types of exception handling are there in java?");
        questions.add("Define the keyword final for variable, method, and class in java?");
        questions.add("What is abstract class?");
        questions.add("What is multi-threading?");
    }

    public void nextQuestion() {
        if (current <= questions.size() - 1)
            current++;
        System.out.print(current);
    }

    public void previousQuestion() {
        if (current > 0)
            current--;
    }

    public void newQuestion(String quest) {
        questions.add(quest);
    }

    public void deleteQuestion(String quest) {
        questions.remove(quest);
    }

    public void displayQuestion() {
        System.out.println(questions.get(current));
    }

    public void displayAllQuestions() {
        for (String quest : questions) {
            System.out.println(quest);
        }
    }
}

步骤 3

创建一个使用 Question 接口的 QuestionManager 类,该类将作为桥梁。

public class QuestionManager {
    protected Question q;
    public String catalog;

    public QuestionManager(String catalog) {
        this.catalog = catalog;
    }

    public void next() {
        q.nextQuestion();
    }

    public void previous() {
        q.previousQuestion();
    }

    public void newOne(String quest) {
        q.newQuestion(quest);
    }

    public void delete(String quest) {
        q.deleteQuestion(quest);
    }

    public void display() {
        q.displayQuestion();
    }

    public void displayAll() {
        System.out.println("Question Paper: " + catalog);
        q.displayAllQuestions();
    }
}

步骤 4

创建一个扩展 QuestionManager 类的 QuestionFormat 类。

public class QuestionFormat extends QuestionManager {
    public QuestionFormat(String catalog) {
        super(catalog);
    }

    public void displayAll() {
        System.out.println("\n---------------------------------------------------------");
        super.displayAll();
        System.out.println("-----------------------------------------------------------");
    }
}

步骤 5

创建一个 BridgePatternDemo 类。

public class BridgePatternDemo {
    public static void main(String[] args) {
        QuestionFormat questions = new QuestionFormat("Java Programming Language");
        questions.q = new JavaQuestions();
        questions.delete("What is class?");
        questions.display();
        questions.newOne("What is inheritance?");
        questions.newOne("How many types of inheritance are there in java?");
        questions.displayAll();
    }
}

输出

What is interface?

---------------------------------------------------------
Question Paper: Java Programming Language
What is class?
What is interface?
What is abstraction?
How multiple polymorphism is achieved in java?
How many types of exception handling are there in java?
Define the keyword final for variable, method, and class in java?
What is abstract class?
What is multi-threading?
What is inheritance?
How many types of inheritance are there in java?
-----------------------------------------------------------

标签: Design Patterns, Design Patterns教程, 设计模式, 软件设计, 结构型模式, 行为型模式, 单例模式, 工厂模式, 观察者模式, 中介者模式, 访问者模式