桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。

Implementor类
public abstract class Implementor {
public abstract void Operation();
}
ConcreteImplementor派生类
public class ConcreteImplmentorA extends Implementor {
@Override
public void Operation() {
// TODO Auto-generated method stub
System.out.println("具体实现A的方法执行");
}
}
public class ConcreteImplmentorB extends Implementor {
@Override
public void Operation() {
// TODO Auto-generated method stub
System.out.println("具体实现B的方法执行");
}
}
Abstraction类
public class Abstraction {
protected Implementor implementor;
public void setImplementor(Implementor implementor) {
this.implementor = implementor;
}
public void Operation() {
implementor.Operation();
}
}
RefinedAbstraction类
public class RefinedAbstraction extends Abstraction {
public void Operation() {
implementor.Operation();
}
}
客户端代码
public static void main(String[] args) {
// TODO Auto-generated method stub
Abstraction abstraction = new RefinedAbstraction();
abstraction.setImplementor(new ConcreteImplmentorA());
abstraction.Operation();
abstraction.setImplementor(new ConcreteImplmentorB());
abstraction.Operation();
}
实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让它们独立变化,减少它们之间的耦合。
本文深入探讨了桥接模式,一种将抽象与其实现分离的设计模式,使得两者可以独立变化。通过具体的Java代码示例,展示了如何使用桥接模式来减少系统中不同分类之间的耦合,提高系统的灵活性和可扩展性。
2万+

被折叠的 条评论
为什么被折叠?



