Saturday, January 06, 2024

The Decorator pattern using C++

Creating a C++ example using the Decorator Design Pattern involves several steps. This design pattern is used to add new functionalities to objects dynamically without altering their structure. Here's how you can structure your C++ project using this pattern: Project Structure Component.h - Abstract base class declaring the common interface. ConcreteComponent.h - Class implementing the Component interface. Decorator.h - Abstract class inheriting from Component, used as a base for concrete decorators. ConcreteDecoratorA.h, ConcreteDecoratorB.h, etc. - Concrete classes extending Decorator with additional functionalities. main.cpp - The main program to demonstrate the usage. Explanation of Each File Component.h
// Component.h
#ifndef COMPONENT_H
#define COMPONENT_H

// Abstract base class representing the primary interface
class Component {
public:
    virtual ~Component() {}
    virtual void operation() = 0;
};
#endif
ConcreteComponent.h
// ConcreteComponent.h

#ifndef CONCRETECOMPONENT_H
#define CONCRETECOMPONENT_H

#include "Component.h"
#include <iostream>

// Concrete implementation of Component
class ConcreteComponent : public Component {
public:
    void operation() override {
        std::cout << "Basic functionality.\n";
    }
};

#endif
Decorator.h
// Decorator.h
#ifndef DECORATOR_H
#define DECORATOR_H

#include "Component.h"

// Base decorator class
class Decorator : public Component {
protected:
    Component* component;

public:
    Decorator(Component* c) : component(c) {}
    void operation() override {
        if (component)
            component->operation();
    }
};

#endif
ConcreteDecoratorA.h
// ConcreteDecoratorA.h
#ifndef CONCRETEDECORATORA_H
#define CONCRETEDECORATORA_H

#include "Decorator.h"
#include <iostream>

// A concrete decorator adding extra features
class ConcreteDecoratorA : public Decorator {
public:
    ConcreteDecoratorA(Component* c) : Decorator(c) {}

    void operation() override {
        Decorator::operation();
        addedBehavior();
    }

private:
    void addedBehavior() {
        std::cout << "Added behavior A.\n";
    }
};
#endif main.cpp
// main.cpp
#include "ConcreteComponent.h"
#include "ConcreteDecoratorA.h"

int main() {
    Component* simple = new ConcreteComponent();
    Component* decorated = new ConcreteDecoratorA(simple);

    std::cout << "Running basic component:\n";
    simple->operation();

    std::cout << "\nRunning decorated component:\n";
    decorated->operation();

    delete simple;
    delete decorated;
    return 0;
}
Order of Creation Component.h:
Define the base interface.
ConcreteComponent.h: 
Implement the basic functionality.
Decorator.h 
Create the base decorator.
ConcreteDecoratorA.h: 
Add specific functionalities.
main.cpp: Demonstrate the pattern.
Expected Output in Terminal

When you run main.cpp, 
you should see the following output:
Running basic component:
Basic functionality.

Running decorated component:
Basic functionality.
Added behavior A.
This project demonstrates the Decorator Pattern in C++ where ConcreteDecoratorA adds additional behavior to ConcreteComponent dynamically.

No comments:

The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...