开发者

Decorator pattern in C++

开发者 https://www.devze.com 2023-01-02 07:30 出处:网络
Can someone give me an example of the Decorator design pattern in C++ ? I have come across the Java version of it, but found it difficul开发者_开发知识库t to understand the C++ version of it (from th

Can someone give me an example of the Decorator design pattern in C++ ? I have come across the Java version of it, but found it difficul开发者_开发知识库t to understand the C++ version of it (from the examples I found).

Thanks.


Vince Huston Design Patterns, even though its layout is poor, has C++ implementation for most design patterns in the Gang of Four book.

Click for Decorator.

There isn't much difference with Java, except the manual memory handling that you'd better wrap with smart pointers :)


I've found the website Sourcemaking to be a pretty good one when it comes to explaining different Design Patterns.

The Decorator design pattern has C++ examples, such as an overview example, a "before and after", and an example with packet encoding/decoding.


#include <iostream>
using namespace std;

class Computer
{
public:
    virtual void display()
    {
        cout << "I am a computer..." << endl;
    }
};

class CDDrive : public Computer
{
private:
    Computer* c;
public:
    CDDrive(Computer* _c)
    {
        c = _c;
    }
    void display()
    {
        c->display();
        cout << "with a CD Drive..." << endl;
    }
};

class Printer : public Computer
{
private:
    CDDrive* d;
public:
    Printer(CDDrive* _d)
    {
        d = _d;
    }
    void display()
    {
        d->display();
        cout << "with a printer..." << endl;
    }
};

int main()
{
    Computer* c = new Computer();
    CDDrive* d = new CDDrive(c);
    Printer* p = new Printer(d);

    p->display();
}


Decorator pattern in C++

I'm learning c++ and design pattern recently, I'll give an example from <Head First Design Pattern>.

the main.cpp is here:

#include <iostream>

#include "Decaf.h"
#include "Espresso.h"
#include "MochaDecorator.h"
#include "MilkDecorator.h"

using std::cout;
using std::endl;


int main(){
    cout << "---------------------------" << endl;
   
    // only Decaf, no addsOn
    Beverage* decafOnly = new Decaf();
    cout << decafOnly->getDescription() << endl;
    cout << decafOnly->cost() << endl;
    delete decafOnly;
    cout << "---------------------------" << endl;

    // Decaf + Mocha
    Beverage* decaf = new Decaf();
    MochaDecorator mocha_decaf = MochaDecorator(decaf);
    cout << mocha_decaf.getDescription() << endl;
    cout << mocha_decaf.cost() << endl;
    delete decaf;
    cout << "---------------------------" << endl;

    // Espresso + Milk
    Beverage* espresso = new Espresso();
    MilkDecorator milk_espresso = MilkDecorator(espresso);
    cout << milk_espresso.getDescription() << endl;
    cout << milk_espresso.cost() << endl;
    delete espresso;
    cout << "---------------------------" << endl;

    // Note: decorators can wrap not only components but the other decorators as well

    // Esresso + Mocha + Milk + Milk,  method 1-------------------------------
    // Beverage* espresso2 = new Espresso();                               // Espresso
    // MochaDecorator* mocha_espresso = new MochaDecorator(espresso2);     // Espresso + Mocha
    // MilkDecorator* milk_deco = new MilkDecorator(mocha_espresso);       // Espresso + Mocha + Milk
    // MilkDecorator* milk_deco2 = new MilkDecorator(milk_deco);           // Espresso + Mocha + Milk + Milk
    // cout << milk_deco2->getDescription() << endl;
    // cout << milk_deco2->cost() << endl;
    // delete espresso2;
    // delete mocha_espresso;
    // delete milk_deco;
    // delete milk_deco2;

    // Esresso + Mocha + Milk + Milk,  method 2------------------------------
    Beverage* espresso2 = new MilkDecorator(new MilkDecorator(new MochaDecorator(new Espresso())));
    cout << espresso2->getDescription() << endl;
    cout << espresso2->cost() << endl;
    delete espresso2;
 
    
    cout << "---------------------------" << endl;
    return 0;
}

other files please see my github:

https://github.com/jwbecalm/Head-First-Design-Patterns-in-CPP/tree/main/ch03_Decorator

0

精彩评论

暂无评论...
验证码 换一张
取 消