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

设计模式笔记————桥接模式

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();
	}

实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让它们独立变化,减少它们之间的耦合。

相关文章:

  • 2021-09-11
  • 2021-12-07
  • 2021-06-22
  • 2022-12-23
  • 2021-12-10
  • 2021-10-18
  • 2021-10-18
  • 2021-06-11
猜你喜欢
  • 2021-09-05
  • 2022-01-21
  • 2021-08-04
  • 2022-03-09
  • 2021-07-12
  • 2022-12-23
  • 2021-11-12
相关资源
相似解决方案