首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

我的設計模式之旅[6]:Adapter Pattern 概念與類別適配器 ISO C++ 實作

     編輯:關於C語言

Adapter Pattern 將不相容介面轉換為客戶期望介面。現實類比:筆電內部用 DC 電,插座提供 AC 電,需 AC-DC 轉接器;USB 轉 COM 連接線讓無 COM 埠筆電連接開發板。

兩種結構

  • 類別適配器:多重繼承(介面公有繼承、被適配類別私有繼承)
  • 物件適配器:組合(持有被適配物件參考)
類別適配器結構圖

範例情境

Grapher 使用 IDrawStrategy.draw(),既有框架提供 IPaint.paint() 及 Triangle/Circle/Square。無法修改框架碼,故撰寫適配器轉換介面。

ISO C++ 類別適配器實作

class TriangleDrawAdapter : public IDrawStrategy, private Triangle {
public:
    virtual void draw() const { paint(); }
};

對 IDrawStrategy 公有繼承(是一種關係),對 Triangle 私有繼承(僅為實作細節,非介面契約)。

完整程式碼

#include <iostream>
using namespace std;

class IDrawStrategy { public: virtual void draw() const = 0; };
class Grapher {
    IDrawStrategy* _drawStrategy;
public:
    Grapher(IDrawStrategy* ds = 0) : _drawStrategy(ds) {}
    void drawShape() const { if (_drawStrategy) _drawStrategy->draw(); }
    void setShape(IDrawStrategy* ds) { _drawStrategy = ds; }
};

class IPaint { public: virtual void paint() const = 0; };
class Triangle : public IPaint { public: void paint() const { cout << "Draw Triangle" << endl; } };
class Circle : public IPaint { public: void paint() const { cout << "Draw Circle" << endl; } };
class Square : public IPaint { public: void paint() const { cout << "Draw Square" << endl; } };

class TriangleDrawAdapter : public IDrawStrategy, private Triangle { public: virtual void draw() const { paint(); } };
class CircleDrawAdapter : public IDrawStrategy, private Circle { public: virtual void draw() const { paint(); } };
class SquareDrawAdapter : public IDrawStrategy, private Square { public: virtual void draw() const { paint(); } };

int main() {
    Grapher grapher(new TriangleDrawAdapter());
    grapher.drawShape();
    grapher.setShape(new CircleDrawAdapter());
    grapher.drawShape();
    grapher.setShape(new SquareDrawAdapter());
    grapher.drawShape();
}

此為經典類別適配器實作,後續將展示物件適配器與泛型改良版本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved