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, June 12, 2011

C++ Command Design Pattern

 June 12, 2011     Design Patterns     No comments   

Command pattern is a behavioral design pattern where the command or request is encapsulated and treated as a object. This article talks about the various classes participating in a command design pattern.

Command Design Pattern

  • Command pattern is a behavioral design pattern.
  • Encapsulates a command/ request. The command itself is treated as an object.
  • Classes participating in a command pattern include:-
    • Command:- An abstract interface defining the execute method.
    • Concrete Commands:- Extend the Command interface and implements the execute method. Invokes the command on the receiver object.
    • Receiver:- Knows how to perform the command action.
    • Invoker:- Asks the command object to carry out the request.
    • Client:- Creates the commands and associates with the receiver.
  • Some examples:-
    • Used when history of requests is needed. (Stock orders executed for today)
    • Asynchronous processing. Commands need to be executed at variant times.
    • Installation wizards.

Implementation of command design pattern

#include <iostream>
#include <vector>
using namespace std;

// Command interface
class Command
{
public:
    virtual void execute() = 0;
};

// Receiver
class StockTrade
{
public:
    StockTrade() {}
    void buy() { cout << "Buy stock" << endl; }
    void sell() { cout << "Sell stock" << endl; }
};

// Concrete command 1
class BuyOrder : public Command
{
    StockTrade* stock;
public:
    BuyOrder(StockTrade* stock)
    {
        this->stock = stock;
    }

    void execute()
    {
        stock->buy();
    }
};

// Concrete command 2
class SellOrder : public Command
{
    StockTrade* stock;
public:
    SellOrder(StockTrade* stock)
    {
        this->stock = stock;
    }

    void execute()
    {
        stock->sell();
    }
};

// Invoker
class StockAgent
{
public:
    StockAgent() {}
    void order( Command* command )
    {
        commandList.push_back(command);
        command->execute();
    }
private:
    // Looking at this command list gives
    // this order history
    vector<Command*> commandList;
};

// Test program
int main()
{
    StockTrade* stock = new StockTrade();
    BuyOrder* buy1 = new BuyOrder(stock);
    BuyOrder* buy2 = new BuyOrder(stock);
    SellOrder* sell1 = new SellOrder(stock);

    StockAgent* agent = new StockAgent();
    agent->order(buy1);
    agent->order(buy2);
    agent->order(sell1);

    delete agent;
    delete sell1;
    delete buy2;
    delete buy1;
    delete stock;
}
OUTPUT:-
Buy stock
Buy stock
Sell stock
  • 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