设计模式:adapter适配器
2016-08-05 15:38
267 查看
1.动机
client想要用adaptee的功能,但是client的调用的接口跟adaptee提供的接口不匹配,这个时候需要adapter做适配。
2.实现方式
2.1 类适配
adapter2公共继承Target,私有继承Adaptee2,并将target的request()指向Adaptee2的specificRequest().
优点:
由于Adapter2是Adaptee2的子类,可以置换或丰富Adaptee2的方法,灵活性更强;
缺点:
在java等语言中不支持多重继承。
2.2 对象适配
adapter继承Target,并实例化Adaptee对象
优点:
可以实例化多个adaptee对象,一个adapter可以Adaptee类和它的子类都适配到Target接口;
缺点:
不能灵活修改功能
3.实例
/*
*/
#include "Geom.H"
// Compilation Instructions
// With ClassAdapter defined and not defined
#define ClassAdapter 0
/*
*/
class Manipulator;
/*
*/
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;
};
/*
*/
#ifdef ClassAdapter
/*
*/
class TextShape : public Shape, private TextView {
public:
TextShape();
virtual void BoundingBox(
Point& bottomLeft, Point& topRight
) const;
virtual bool IsEmpty() const;
virtual Manipulator* CreateManipulator() const;
};
/*
*/
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);
}
/*
*/
bool TextShape::IsEmpty () const {
return TextView::IsEmpty();
}
/*
*/
class Manipulator {
};
class TextManipulator : public Manipulator {
public:
TextManipulator(const TextShape*);
};
/*
*/
Manipulator* TextShape::CreateManipulator () const {
return new TextManipulator(this);
}
/*
*/
#endif
#ifndef ClassAdapter
class TextView;
class Manipulator {
};
class TextManipulator : public Manipulator {
public:
TextManipulator();
};
/*
*/
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);
}
/*
*/
#endif
/*
*/
client想要用adaptee的功能,但是client的调用的接口跟adaptee提供的接口不匹配,这个时候需要adapter做适配。
2.实现方式
2.1 类适配
adapter2公共继承Target,私有继承Adaptee2,并将target的request()指向Adaptee2的specificRequest().
优点:
由于Adapter2是Adaptee2的子类,可以置换或丰富Adaptee2的方法,灵活性更强;
缺点:
在java等语言中不支持多重继承。
2.2 对象适配
adapter继承Target,并实例化Adaptee对象
优点:
可以实例化多个adaptee对象,一个adapter可以Adaptee类和它的子类都适配到Target接口;
缺点:
不能灵活修改功能
3.实例
/*
*/
#include "Geom.H"
// Compilation Instructions
// With ClassAdapter defined and not defined
#define ClassAdapter 0
/*
*/
class Manipulator;
/*
*/
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;
};
/*
*/
#ifdef ClassAdapter
/*
*/
class TextShape : public Shape, private TextView {
public:
TextShape();
virtual void BoundingBox(
Point& bottomLeft, Point& topRight
) const;
virtual bool IsEmpty() const;
virtual Manipulator* CreateManipulator() const;
};
/*
*/
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);
}
/*
*/
bool TextShape::IsEmpty () const {
return TextView::IsEmpty();
}
/*
*/
class Manipulator {
};
class TextManipulator : public Manipulator {
public:
TextManipulator(const TextShape*);
};
/*
*/
Manipulator* TextShape::CreateManipulator () const {
return new TextManipulator(this);
}
/*
*/
#endif
#ifndef ClassAdapter
class TextView;
class Manipulator {
};
class TextManipulator : public Manipulator {
public:
TextManipulator();
};
/*
*/
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);
}
/*
*/
#endif
/*
*/
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- 设计模式---状态模式在web前端中的应用
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C# 设计模式系列教程-建造者模式
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用