设计模式[4] Bridge Pattern 桥接模式

decouple an abstraction from its implementation so that the two can vary independently" (Gamma et al.)
接口和实现分离,接口和实现的修改可以互不干扰。

参与者:
  • Abstraction   (IShape) 
    • defines the abstraction's interface.
    • maintains a reference to an object of type Implementor. 主要就是这个reference。
  • RefinedAbstraction   (CircleShape)
    • extends the interface defined by Abstraction.
  • Implementor   (IDrawingAPI)
    • defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface; in fact the two interfaces can be quite different. Typically the Implementation interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives. 和Abstraction的接口没啥关系。
  • ConcreteImplementor   (DrawingAPI1, DrawingAPI2)
    • implements the Implementor interface and defines its concrete implementation.

using  System;
 
 
/** "Implementor" */
 
interface  IDrawingAPI  {
    
void DrawCircle(double x, double y, double radius);
 }

 
 
/** "ConcreteImplementor" 1/2 */
 
class  DrawingAPI1 : IDrawingAPI  {
    
public void DrawCircle(double x, double y, double radius) 
    
{
        System.Console.WriteLine(
"API1.circle at {0}:{1} radius {2}", x, y, radius); 
    }

 }

 
 
/** "ConcreteImplementor" 2/2 */
 
class  DrawingAPI2 : IDrawingAPI 
 
{
    
public void DrawCircle(double x, double y, double radius) 
    

        System.Console.WriteLine(
"API2.circle at {0}:{1} radius {2}", x, y, radius); 
    }

 }

 
 
/** "Abstraction" */
 
interface  IShape  {
    
void Draw();                             // low-level (i.e. Implementation-specific)
    void ResizeByPercentage(double pct);     // high-level (i.e. Abstraction-specific)
 }

 
 
/** "Refined Abstraction" */
 
class  CircleShape : IShape  {
    
private double x, y, radius;
    
private IDrawingAPI drawingAPI;
    
public CircleShape(double x, double y, double radius, IDrawingAPI drawingAPI) 
    
{
        
this.x = x;  this.y = y;  this.radius = radius; 
        
this.drawingAPI = drawingAPI;
    }

    
// low-level (i.e. Implementation-specific)
    public void Draw() { drawingAPI.DrawCircle(x, y, radius); }
    
// high-level (i.e. Abstraction-specific)
    public void ResizeByPercentage(double pct) { radius *= pct; }
 }

 
 
/** "Client" */
 
class  BridgePattern  {
    
public static void Main(string[] args) {
        IShape[] shapes 
= new IShape[2];
        shapes[
0= new CircleShape(123new DrawingAPI1());
        shapes[
1= new CircleShape(5711new DrawingAPI2());
 
        
foreach (IShape shape in shapes) {
            shape.ResizeByPercentage(
2.5);
            shape.Draw();
        }

    }

 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值