适配器模式(类对象结构型模式)

 意图:将一个类的借口转换成客户希望的另外一个接口。使得原来由于接口不兼容而不能一起工作的那些类可以一起工作。

适用性:

1 想使用一个已经存在的类,而它的借口不符合你的需求

2 创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作

3 (仅适用于对象Adapter)使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。

结构:

类适配器使用多重继承对一个接口与另一个接口进行匹配。用C++实现时,Adapter应该采用公共方式继承Target类,并且用私有方式继承Adaptee。所以,Adapter类应该是Target的子类型,但不是Adaptee的子类型。

对象适配器依赖于对象组合,将Adaptee的一个实例作为Adapter类中一个成员,从而对Adapter的操作,转而调用Adaptee类的操作。

这种模式也可以提供那些被匹配的泪所没有提供的功能。

以下为书上98页例子:将TextView适配Shape的接口,有两种实现,一种构造一个类适配器,一种是对象适配器

#include <iostream>
using namespace std;

//点结构体
struct Point
{
    Point(int x = 0, int y = 0) : _x(x), _y(y)
    {}
private:
    int _x;
    int _y;
};

class Shape;
class TextShape;

//控制器,可以对Shape进行操控
class Manipulator
{
public:
	Manipulator()
	{
		cout << "构造一个Manipulator" << endl;
	}
};

class TextManipulator : public Manipulator
{
public:
	TextManipulator()
	{
		cout << "构造一个TextManipulator" << endl;
	}
};

//接口
class Shape
{
public:
    Shape()
    {
        cout << "创建一个Shape..." << endl;
    }

    virtual void BoundingBox(Point& bottomLeft, Point& topRight) const
    {

    }

    virtual Manipulator* CreateManipulator() const
    {
        cout << "创建了一个CreateMainputor" << endl;
        return new Manipulator();
    }
};

//adaptee,不兼容的类,通过TextShape进行适配
class TextView
{
public:
    TextView()
    {
        cout << "构造一个TextView" << endl;
    }

    void GetOrigin(int x, int y) const
    {
        x = 0;
        y = 0;
    }

    void GetExtent(int width, int height) const
    {
        width = 10;
        height = 10;
    }

    virtual bool IsEmpty() const
    {
        return false;
    }
};

//Adapter,使TextView适合Shape的接口 ,这是个类适配器,
// 通过public继承接口,private继承实现
class TextShape : public Shape, private TextView
{
public:
    TextShape()
    {
        cout << "构造一个TextShape" << endl;
    }

    virtual void BoundingBox(Point& bottomLeft, Point& topRight) const
    {
        int bottom, left, width, height;

        GetOrigin(bottom, left);
        GetExtent(width, height);

        bottomLeft = Point(bottom, left);
        topRight = Point(bottom + height, left + width);
    }

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

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



//对象适配器,包含了一个要适配的对象
class TextShape1 : public Shape
{
public:
    TextShape1(TextView*)
    {
        cout << "构造一个TextShape1对象" << endl;
    }

    virtual void BoundingBox(Point& bottomLeft, Point& topRight) const
    {
        int bottom, left, width, height;

        _text->GetOrigin(bottom, left);
        _text->GetExtent(width, height);

        bottomLeft = Point(bottom, left);
        topRight = Point(bottom + height, left + width);
    }

    bool IsEmpty() const
    {
        return _text->IsEmpty();
    }

    Manipulator* CreateManipulator() const
    {
        return new TextManipulator();
    }
private:
    TextView* _text;
};



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值