//&的使用的方法,引用别名
//const这个成员对象什么意思,有什么属性,一定要加const吗,优缺点。承诺不改对象,能被 const 对象调用,不改状态就该加
//继承可以多重继承吗,这里面public和private又有什么区别,前者是一个东西后者是实现
//类适配器里面成员函数返回的是对象吗还是函数呢那个getextent
//explicit关键字干嘛的。是隐士实现
//explicit ObjectTextShape(TextView* t) : tv(t) {}这个可以写成什么格式
// explicit ObjectTextShape (TextView*t)
// {
// tv = t;
// }
#include <iostream>
struct Point {
int x = 0, y = 0; };
// ========== Target ==========
class Shape {
public:
virtual void boundingBox(Point& bl, Point& tr) const = 0;
virtual bool isEmpty() const = 0;
virtual void createManipulator() const = 0;
virtual ~Shape() = default;
};
// ========== Adaptee ==========
class TextView {
public:
void getExtent(Point& bl, Point& tr) const {
bl = {
0, 0}; tr = {
100, 50};
std::cout << " TextView::getExtent\n";
}
bool isEmpty() const {
std::cout << " TextView::isEmpty\n";
return false;
}
};
// ========== 类适配器 ==========
class ClassTextShape : public Shape, private TextView {
public:
void boundingBox(Point& bl, Point& tr) const override {
getExtent(bl, tr);
}
bool isEmpty() const override {
return TextView::isEmpty();
}
void createManipulator() const override {
std::cout << " ClassTextShape::createManipulator\n";
}
};
// ========== 对象适配器 ==========
class ObjectTextShape : public Shape {
TextView* tv;
public:
explicit ObjectTextShape(TextView* t) : tv(t) {
}
void boundingBox(Point& bl, Point& tr) const override {
tv->getExtent(bl, tr);
}
bool isEmpty() const override {
return tv->isEmpty();
}
void createManipulator() const override {
std::cout << " ObjectTextShape::createManipulator\n";
}
};
// ========== Client ==========
void drawShape(const Shape& s) {
Point bl, tr;
s.boundingBox(bl, tr);
std::cout << " boundingBox: (" << bl.x << "," << bl.y << ") - ("
<< tr.x << "," << tr.y << ")\n";
s.isEmpty();
s.createManipulator
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/m0_63774801/article/details/166253247



