桥接模式说, 只是“将功能抽象与实现分离开, 以便两者可以独立变化”。
【桥接模式】桥接模式也称为句柄或主体。
桥接模式的优势
- 它使实现与接口分离。
- 它提高了可扩展性。
- 它允许向客户端隐藏实施细节。
- 当你不希望在函数抽象及其实现之间建立永久绑定时。
- 当功能抽象及其实现都需要使用子类扩展时。
- 它主要用于那些对实现进行更改不会影响客户端的地方。
下面给出的UML描述了桥接模式的示例。
用于桥接模式的UML:
文章图片
以上UML的实现: 步骤1 创建一个问题界面, 该界面提供从一个问题到另一个问题的导航, 反之亦然。
// this is the Question interface.
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();
}
// End of the Question interface.
第2步 创建一个将实现Question接口的JavaQuestions实现类。
// this is the JavaQuestions class.
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 exceptionhandling are there in java? ");
questions.add("Define the keyword final forvariable, 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);
}
}
}// End of the JavaQuestions class.
第三步 创建一个QuestionManager类, 它将使用Question接口作为桥梁。
// this is the QuestionManager class.
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();
}
}// End of the QuestionManager class.
步骤4 创建一个QuestionFormat类, 它将扩展QuestionManager类
// this is the QuestionFormat class.
public class QuestionFormat extends QuestionManager {
public QuestionFormat(String catalog){
super(catalog);
}
public void displayAll() {
System.out.println("\n---------------------------------------------------------");
super.displayAll();
System.out.println("-----------------------------------------------------------");
}
}// End of the QuestionFormat class.
第5步 创建一个BridgePatternDemo类。
// this is the BridgePatternDemo class.
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();
}
}// End of the BridgePatternDemo class.
输出量
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 exceptionhandling are there in java?
Define the keyword final forvariable, 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?
-----------------------------------------------------------------------
推荐阅读
- 建造者设计模式
- 行为设计模式
- 适配器模式
- 什么是.DAT文件以及如何在Windows 10中打开DAT文件()
- Android和iOS设备的12大最佳写作应用软件合集推荐
- Windows 10如何从PDF文件中提取图像(有哪些方法?)
- 供作者使用的12大最佳图书写作软件推荐合集(哪个最好用())
- Win8怎样禁用Windows徽标键?
- Windows8龙之谷玩不了怎样办?