StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Sunday, March 31, 2013

C++ Decorator Design Pattern

 March 31, 2013     Design Patterns     No comments   

In this article, we will explore the decorator design pattern with a sample implementation in C++.

Decorator Design Pattern

  • Useful, when you want to add capabilities (statically or dynamically) to an object without sub-classing the concrete object's class as well as not affecting other objects of the same concrete class.

Decorator Pattern Implementation

#include <iostream>
#include <string>

using namespace std;

class actor {
public:
    actor() {}
    virtual void act() {}
    virtual string name() {}
};

class actorConcretePerson: public actor {
public:
    actorConcretePerson(string name) { mName = name; }
    void act() { cout << mName << " is acting" << endl;}
    string name() { return mName; }
private:
    string mName;

};

class actorCharacter: public actor {
public:
    actorCharacter(string charName) { mCharName = charName; }
    void setActorPerson( actor *anActor) {mActor = anActor;}
    void act() { mActor->act(); }
    actor* getActor() { return mActor; }
    void actCharacter() { act(); cout << mCharName << " is the role" << endl;}
    string getCharName() { return mCharName; }
private:
    actor *mActor;
    string mCharName;
};

class actorCharacterTheif: public actorCharacter {
public:
    actorCharacterTheif(string charName):actorCharacter(charName) { }
    void setTarget(string target) { mTarget = target ; }
    void steal() { cout << getActor()->name() << ", the " << getCharName() << ": Stole " << mTarget << endl; }

private:
    string mTarget;

};

class actorCharacterPolice: public actorCharacter {
public:
    actorCharacterPolice(string charName):actorCharacter(charName) { }
    void setTrack(string track) { mTrack = track ; }
    void catchThief() { cout << getActor()->name() << ", the " << getCharName() << ": Cought " << mTrack << endl; }
private:
    string mTrack;
};


int main()
{
    actor *cage = new actorConcretePerson("NicolasCage");
    actor *travolta = new actorConcretePerson("JohnTravolta");

    actorCharacterPolice *police = new actorCharacterPolice("Commissioner");
    actorCharacterTheif *smuggler = new actorCharacterTheif("Smuggler");
    actorCharacterTheif *robber = new actorCharacterTheif("Robber");

    cout << "Basic concrete objects and methods..." << endl;
    cage->act();
    travolta->act();

    cout << endl << "Decorator objects and methods..." << endl;
    police->setActorPerson(travolta);
    smuggler->setActorPerson(cage);

    police->actCharacter();
    police->setTrack(smuggler->getCharName());
    police->catchThief();

    smuggler->act();
    smuggler->actCharacter();
    smuggler->setTarget("Gold");
    smuggler->steal();

    robber->setActorPerson(travolta);
    robber->act();
    robber->actCharacter();
    robber->setTarget("Dollars");
    robber->steal();

    return 0;
}
Output:-
Basic concrete objects and methods... 
NicolasCage is acting 
JohnTravolta is acting 
 
Decorator objects and methods... 
JohnTravolta is acting 
Commissioner is the role 
JohnTravolta, the Commissioner: Cought Smuggler 
NicolasCage is acting 
NicolasCage is acting 
Smuggler is the role 
NicolasCage, the Smuggler: Stole Gold 
JohnTravolta is acting 
JohnTravolta is acting 
Robber is the role 
JohnTravolta, the Robber: Stole Dollars  
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk