| 敘述 | 目的 | |
|---|---|---|
| Adapter | 將一個類別的介面轉換為另一個介面 | 使原本由於介面不相容而不能一起工作的兩個類別可以一起工作 | 
| Decorator | 向一個物件添加新的功能 | 通過創建一個包含目標物件的類,並重寫目標物件的方法來實現 | 
| Facade | 為子系統中的一組接口提供一個統一的高層接口 | 使得子系統更容易使用,通常用於簡化一組繁複接口或者隱藏子系統的內部細節從而提供一個簡化介面到客戶端 | 
組合:
// "Engine" 類別
class Engine {
public:
    void start() {
        std::cout << "Engine started" << std::endl;
    }
};
// "Car" 類別包含 "Engine"
class Car {
private:
    Engine engine;
public:
    Car() : engine() {}
    void start() {
        engine.start();
    }
};
int main() {
    Car car;
    car.start();
}
委派:
// "Printer" 介面
class Printer {
public:
    virtual void print_page(const string& content) = 0;
};
// "InkjetPrinter" 實現 "Printer"
class InkjetPrinter : public Printer {
public:
    void print_page(const string& content) override {
        cout << "Printing from Inkjet: " << content << endl;
    }
};
// "LaserPrinter" 實現 "Printer"
class LaserPrinter : public Printer {
public:
    void print_page(const string& content) override {
        cout << "Printing from Laser: " << content << endl;
    }
};
// "Document" 使用委派
class Document {
private:
    Printer* printer;
public:
    Document(Printer* p) : printer(p) {}
    
    void print(const string& content) {
        printer->print_page(content);
    }
};
int main() {
    InkjetPrinter inkjet;
    LaserPrinter laser;
    Document doc1(&inkjet);
    Document doc2(&laser);
    doc1.print("Hello, world!");
    doc2.print("Hello, world!");
    
    // 輸出為:
    // Printing from Inkjet: Hello, world!
    // Printing from Laser: Hello, world!
}