Design Patterns -- Bridge

本文深入解析了桥接模式的设计意图及实现步骤,旨在使抽象与其实现能够独立变化。通过实例说明如何重构类层次结构来应用此模式,并展示了具体的应用场景。

1. The intent of the BRIDGE pattern is to decouple an abstraction (a  class that relies on a set of abstarct operations, GoF Design Patterns)  from the implementation of its abstract operation, so that the abstraction and its implementation can vary independently.

2. A Bridge structure moves the abstract operations that an abstraction relies on into a separate interface, pic:

Implementor contains the abstract operations seperated from Abstraction, and after seperation Abstraction  is concrete.

3. A common example of Bridge occurs in drivers using. An application that uses a driver is an abstraction -- the choice of driver determines what happens when the application runs.

4. Steps to refactor a hierarchy with an abstract class at lits top into a bridge:

a. Move the abstract operation in the superclass into an interface.

b.Define implementation classes that provide different implementations of the interface.

c. Redefine the remaining operations in the abstract class as operations on an instance of the new interface. (The remaining operations usually uses the functions in the interface).

5. Another example of Bridge:

ori:

sec:

after refactor:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值