/** * 主类抽象类 */ public abstract class Abstraction { /** * 桥接组合对象 */ protected Implementor implementor; public Abstraction(Implementor implementor) { this.implementor = implementor; } /** * 操做类 */ public abstract void operation(); } /** * 抽象接口 */ public interface Implementor { void operationImpl(); } /** * 实现类 */ public class RefinedAbstraction extends Abstraction { public RefinedAbstraction (Implementor implementor) { super(implementor); } @Override public void operation() { System.out.println("操做"); implementor.operationImpl(); } } /** * 接口抽象实现1 */ public class ConcreteImplementorA implements Implementor { @Override public void operationImpl() { System.out.println("桥接A"); } } /** * 接口抽象实现2 */ public class ConcreteImplementorB implements Implementor { @Override public void operationImpl() { System.out.println("桥接B"); } }
/** * 测试与应用 */ public class Test { public static void main(String[] args) { Abstraction abstraction1 = new RefinedAbstraction(new ConcreteImplementorA()); Abstraction abstraction2 = new RefinedAbstraction(new ConcreteImplementorB()); abstraction1.operation(); abstraction2.operation(); } }
操做 桥接A 操做 桥接B
角色介绍html
银行有农业银行和工商银行等等,而帐户有活期帐户和死期帐户,两个维度很适合使用桥接模式,下面为具体实现:
/** * 银行抽象类 */ public abstract class Bank { protected Account account; public Bank(Account account) { this.account = account; } /** * 不限制方法名,但由于委派因此起的同样 * 不要本身都实现了,要尽可能把行为委托给组合的类 * @return */ abstract Account openAccount(); } /** * 农业银行实现类 */ public class ABCBank extends Bank { public ABCBank (Account account) { super(account); } @Override Account openAccount() { System.out.println("打开中国农业银行帐号"); account.openAccount(); return account; } } /** * 工商银行实现类 */ public class ICBCBank extends Bank { public ICBCBank(Account account) { super(account); } @Override Account openAccount() { System.out.println("打开中国工商银行帐号"); account.openAccount(); return account; } } /** * 银行帐号, 桥的实现接口 */ public interface Account { /** * 打开帐号 * @return */ Account openAccount(); /** * 查看帐号类型 */ void showAccountType(); } /** * 按期帐户实现类 */ public class DepositAccount implements Account { @Override public DepositAccount openAccount() { System.out.println("打开按期帐号"); return new DepositAccount(); } @Override public void showAccountType() { System.out.println("这是按期帐号"); } } /** * 活期帐户实现类 */ public class SavingAccount implements Account { @Override public SavingAccount openAccount() { System.out.println("打开活期帐号"); return new SavingAccount(); } @Override public void showAccountType() { System.out.println("这是活期帐号"); } }
/** * 测试与应用 */ public class Test { public static void main(String[] args) { Bank icbcBank = new ICBCBank(new DepositAccount()); Account icbcAccount = icbcBank.openAccount(); icbcAccount.showAccountType(); Bank abcBank = new ABCBank(new SavingAccount()); Account abcAccount = abcBank.openAccount(); abcAccount.showAccountType(); } }
打开中国工商银行帐号 打开按期帐号 这是按期帐号 打开中国农业银行帐号 打开活期帐号 这是活期帐号
桥接模式和组合模式java
桥接模式和适配器模式git
慕课网设计模式精讲
: https://coding.imooc.com/class/270.html 设计模式读书笔记-----桥接模式
: http://www.javashuo.com/article/p-njknvetl-bm.html