别人写好的代码时经过验证过的,所以不需要修改,在使用别人代码时或者对别人写的类库进行封装时,就用到了适配器模式。
多重继承实现
适配器继承原来的代码类和一个通用接口类,在适配器中实现通用接口类的函数,该函数中调用原来原来代码的方法(原来代码就是类库中或者别人代码)。
使用 一个矩形打印说明一下
LegacyRectangle.h是别人的旧类(别人的类,不动)Rectangle.h接口RectangleAdapter.h适配器
Rectangle.h
//
// Created by wenfan on 2021/5/31.
//
#ifndef LEARN_CPP_RECTANGLE_H
#define LEARN_CPP_RECTANGLE_H
class Rectangle{
public:
virtual void Draw() = 0;
};
#endif //LEARN_CPP_RECTANGLE_H
LegacyRectangle.h
//
// Created by wenfan on 2021/5/31.
//
#ifndef LEARN_CPP_LEGACYRECTANGLE_H
#define LEARN_CPP_LEGACYRECTANGLE_H
#include <iostream>
using namespace std;
class LegacyRectangle {
public:
LegacyRectangle(double x, double y, double width, double height) :
_x(x), _y(y), _width(width), _height(height){
cout << "LegacyRectangle(double x, double y, double width, double height)" << endl;
}
void LegacyDraw() const{
cout << "LegacyDraw : x = " << _x << " , y = " << _y << " , width = " << _width << " , height = " << _height << endl;
}
private:
double _x;
double _y;
double _width;
double _height;
};
#endif //LEARN_CPP_LEGACYRECTANGLE_H
RectangleAdapter.h适配器
//
// Created by wenfan on 2021/5/31.
//
#ifndef LEARN_CPP_RECTANGLEADAPTER_H
#define LEARN_CPP_RECTANGLEADAPTER_H
#include "LegacyRectangle.h"
#include "Rectangle.h"
class RectangleAdapter : public LegacyRectangle, public Rectangle{
public:
RectangleAdapter(double x1, double y1, double width1, double height1) :
LegacyRectangle(x1, y1, width1, height1) {
cout << "RectangleAdapter(double x, double y, double width, double height)" << endl;
}
virtual void Draw(){
cout << "RectangleAdapter::Draw()" << endl;
LegacyDraw();
}
};
#endif //LEARN_CPP_RECTANGLEADAPTER_H
这样就通过继承实现了适配器,也就是说通过继承调用了原来的函数。
组合实现
RectangleAdapter2.h是一个继承接口的类,在该类中维护了一个原来类的对象。
//
// Created by wenfan on 2021/5/31.
//
#ifndef LEARN_CPP_RECTANGLEADAPTER2_H
#define LEARN_CPP_RECTANGLEADAPTER2_H
#include "Rectangle.h"
#include "LegacyRectangle.h"
class RectangleAdapter2 : public Rectangle {
public:
RectangleAdapter2(double x,double y,double width,double height) :
legacyRectangle(x,y,width,height){
cout << "RectangleAdapter2(double x,double y,double width,double height)" << endl;
}
virtual void Draw();
private:
LegacyRectangle legacyRectangle;
};
#endif //LEARN_CPP_RECTANGLEADAPTER2_H
RectangleAdapter2.cpp
//
// Created by wenfan on 2021/5/31.
//
#include "RectangleAdapter2.h"
void RectangleAdapter2::Draw(){
cout << "RectangleAdapter2::Draw()" << endl;
legacyRectangle.LegacyDraw();
}
测试两种实现
//
// Created by wenfan on 2021/5/31.
//
#ifndef LEARN_CPP_TEST_ADAPTER_H
#define LEARN_CPP_TEST_ADAPTER_H
#include "RectangleAdapter.h"
#include "RectangleAdapter2.h"
void test_multiInherit(){
double x = 100.0;
double y = 200.0;
double width = 10;
double height = 10;
RectangleAdapter adapter(x,y,width,height);
Rectangle* p_rectangle = &adapter;
p_rectangle->Draw();
}
void test_compose_adapter(){
double x = 300.0;
double y = 400.0;
double width = 20;
double height = 20;
RectangleAdapter2 adapter2(x, y, width, height);
Rectangle* rectangle = &adapter2;
rectangle->Draw();
}
#endif //LEARN_CPP_TEST_ADAPTER_H
实验结果
所以,不管是多重继承还是组合的方式,本质就是如何能调用到原来类的函数。