笔记:Gof设计模式--Adapter

1、意图 

    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

 

2、适应性

 Use the Adapter pattern when
    •  you want to use an existing class, and its interface does not match the one you need.

    •  you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.

    •  (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

 

3、结构

 

4、示例代码

  First we'll define factory methods in MazeGame for creating the maze, room, wall, and door objects:

 class Shape { 
    public: 
        Shape(); 
        virtual void BoundingBox( 
            Point& bottomLeft, Point& topRight 
        ) const; 
        virtual Manipulator* CreateManipulator() const; 
    }; 
     
    
 
 class TextView { 
    public: 
        TextView(); 
        void GetOrigin(Coord& x, Coord& y) const; 
        void GetExtent(Coord& width, Coord& height) const; 
        virtual bool IsEmpty() const; 
    };


 


  A class adapter uses multiple inheritance to adapt interfaces. The key to class adapters is to use one inheritance branch to inherit the interface and another branch to inherit the implementation. The usual way to make this distinction in C++ is to inherit the interface publicly and inherit the implementation privately. We'll use this convention to define the TextShape adapter.

 
 class TextShape : public Shape, private TextView { 
    public: 
        TextShape(); 
     
        virtual void BoundingBox( 
            Point& bottomLeft, Point& topRight 
        ) const; 
        virtual bool IsEmpty() const; 
        virtual Manipulator* CreateManipulator() const; 
    }; 


  The BoundingBox operation converts TextView's interface to conform to Shape's.

 void TextShape::BoundingBox ( 
        Point& bottomLeft, Point& topRight 
    ) const { 
        Coord bottom, left, width, height; 
     
        GetOrigin(bottom, left); 
        GetExtent(width, height); 
     
        bottomLeft = Point(bottom, left); 
        topRight = Point(bottom + height, left + width); 
    } 


  The IsEmpty operation demonstrates the direct forwarding of requests common in adapter implementations:

 bool TextShape::IsEmpty () const { 
        return TextView::IsEmpty(); 
    } 


  Finally, we define CreateManipulator (which isn't supported by TextView) from scratch. Assume we've already implemented a TextManipulator class that supports manipulation of a TextShape.

 Manipulator* TextShape::CreateManipulator () const { 
        return new TextManipulator(this); 
    } 


  The object adapter uses object composition to combine classes with different interfaces. In this approach, the adapter TextShape maintains a pointer to TextView.

class TextShape : public Shape { 
    public: 
        TextShape(TextView*); 
     
        virtual void BoundingBox( 
            Point& bottomLeft, Point& topRight 
        ) const; 
        virtual bool IsEmpty() const; 
        virtual Manipulator* CreateManipulator() const; 
    private: 
        TextView* _text; 
};

 TextShape::TextShape (TextView* t) { 
        _text = t; 
    }
 void TextShape::BoundingBox ( 
        Point& bottomLeft, Point& topRight 
    ) const { 
        Coord bottom, left, width, height; 
     
        _text->GetOrigin(bottom, left); 
        _text->GetExtent(width, height); 
     
        bottomLeft = Point(bottom, left); 
        topRight = Point(bottom + height, left + width); 
    } 
     
    
 bool TextShape::IsEmpty () const { 
        return _text->IsEmpty(); 
    } 

 Manipulator* TextShape::CreateManipulator () const { 
        return new TextManipulator(this); 
    } 


 Compare this code to the class adapter case. The object adapter requires a little more effort to write, but it's more flexible. For example, the object adapter version of TextShape will work equally well with subclasses of TextView—the client simply passes an instance of a TextView subclass to the TextShape constructor.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值