设计模式-桥接模式


将抽象和实现解耦,使得两者可以独立地变化。
桥接模式使用了类间的聚合关系、集成、覆写等常用功能,但是它是一个清晰稳定的架构。



1. 不希望或者不适用使用继承的场景
2. 接口或抽象类不稳定的场景
3. 重用性要求较高的场景


设计模式-桥接模式

public interface Implementor {
	public void doSomething();
	public void doAnything();
}

public class ConcreteImplmentor1 implements Implementor {

	@Override
	public void doSomething() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doAnything() {
		// TODO Auto-generated method stub

	}

}


public class ConcreteImplementor2 implements Implementor {

	@Override
	public void doSomething() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doAnything() {
		// TODO Auto-generated method stub

	}

}

public abstract class Abstraction {
	private Implementor implementor;
	
	public Abstraction(Implementor implementor) {
		this.implementor = implementor;
	}
	
	// Self action and attribute
	public void request() {
		this.implementor.doSomething();
	}
	
	// get implements role
	public Implementor getImplementor() {
		return this.implementor;
	}
}

public class RefinedAbstraction extends Abstraction {

	public RefinedAbstraction(Implementor implementor) {
		super(implementor);
	}

	@Override
	public void request() {
		super.request();
		super.getImplementor().doAnything();
	}
	
}

public class Client {
	
	public static void main(String[] args) {
		
		Implementor implementor = new ConcreteImplmentor1();
		Abstraction abstraction = new RefinedAbstraction(implementor);
		abstraction.request();
		
	}
}

你可能感兴趣的:(设计模式)