设计模式-桥接模式

        桥接模式:实现系统可能有多个角度分类,每一种分类都有可能变化,那么就把这种多角度分离独立让他们独立变化,减少它们之间的耦合。实现了抽象和实现部分的分离,从而极大的提供了系统的灵活性,让抽象部分和实现部分独立开来,这有助于系统进行分层设计,从而产生更好的结构化系统。对于系统的高层部分,只需要知道抽象部分和实现部分的接口就可以了,其他的都分由具体业务来完成。桥接模式替代多层继承方案,可以减少子类,降低系统的管理和维护成本。桥接模式的引入增加了系统的理解和设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计和编程。桥接模式要求正确识别出系统中两个独立变化的维度,因此其使用范围有一定的局限性,即需要有这样的应用场景。

        

package com.bridge;

public abstract class Implementot {
	public abstract void operation();
}
package com.bridge;

public class ConcreteImplementorA extends Implementot{

	@Override
	public void operation() {
		System.out.println("具体实现A的方法执行!");
	}

}
package com.bridge;

public class ConcreteImplementorB extends Implementot{

	@Override
	public void operation() {
		System.out.println("具体实现B的方法执行!");
	}

}
package com.bridge;

public abstract class Abstraction {
	protected Implementot implementot;

	public void setImplementot(Implementot implementot) {
		this.implementot = implementot;
	}
	
	public void operation() {
		implementot.operation();
	}
}
package com.bridge;

public class RefinedAbstraction extends Abstraction{
	public void operation() {
		implementot.operation();
	}
}
package com.bridge;

public class User {
	public static void main(String[] args) {
		Abstraction ab = new RefinedAbstraction();
		ab.setImplementot(new ConcreteImplementorA());
		ab.operation();
		
		ab.setImplementot(new ConcreteImplementorB());
		ab.operation();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值