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

Monday, June 13, 2011

C++ Visitor Design Pattern

 June 13, 2011     Design Patterns     No comments   

The visitor design pattern is a structural design pattern and allows the separation of data structures and algorithms from the data itself. In this article we will explore visitor design pattern and an implementation in C++.

Visitor design pattern

  • The visitor pattern is a behavioral design pattern.
  • Visitor pattern allows to separate the data structures and the algorithms to be applied on the data.
  • Both data structure objects and algorithm objects can evolve separately. Makes development and changes easier.
  • Data structure (element) objects have an "accept" method which take in a visitor (algorithmic) object.
  • Algorithmic objects have a "visit" method which take in a data structure object.

Visitor design pattern implementation

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

// Forwards
class VisitorIntf;

// Abstract interface for Element objects
class ElementIntf
{
public:
    virtual string name() = 0;
    virtual void accept(VisitorIntf* object) = 0;
};

// Abstract interface for Visitor objects
class VisitorIntf
{
public:
    virtual void visit(ElementIntf* object) = 0;
};

// Concrete element object
class ConcreteElement1 : public ElementIntf
{
public:
    string name()
    {
        return "ConcreteElement1";
    }

    void accept(VisitorIntf *object)
    {
        object->visit(this);
    }
};


// Concrete element object
class ConcreteElement2 : public ElementIntf
{
public:
    string name()
    {
        return "ConcreteElement2";
    }

    void accept(VisitorIntf *object)
    {
        object->visit(this);
    }
};

// Visitor logic 1
class ConcreteVisitor1 : public VisitorIntf
{
public:
    void visit(ElementIntf *object)
    {
        cout << "Visited " << object->name() <<
                " using ConcreteVisitor1." << endl;
    }
};

// Visitor logic 2
class ConcreteVisitor2 : public VisitorIntf
{
public:
    void visit(ElementIntf *object)
    {
        cout << "Visited " << object->name() <<
             " using ConcreteVisitor2." << endl;
    }
};

//  Test main program
int main()
{
    list<ElementIntf*> elementList1;
    elementList1.push_back(new ConcreteElement1());
    elementList1.push_back(new ConcreteElement2());

    VisitorIntf* visitor1 = new ConcreteVisitor1();
    while ( ! elementList1.empty() )
    {
        ElementIntf* element = elementList1.front();
        element->accept(visitor1);
        elementList1.pop_front();
    }

    list<ElementIntf*> elementList2;
    elementList2.push_back(new ConcreteElement1());
    elementList2.push_back(new ConcreteElement2());
    VisitorIntf* visitor2 = new ConcreteVisitor2();
    while ( ! elementList2.empty() )
    {
        ElementIntf* element = elementList2.front();
        element->accept(visitor2);
        elementList2.pop_front();
    }

    delete visitor1;
    delete visitor2;
}
OUTPUT:-
Visited ConcreteElement1 using ConcreteVisitor1.
Visited ConcreteElement2 using ConcreteVisitor1.
Visited ConcreteElement1 using ConcreteVisitor2.
Visited ConcreteElement2 using ConcreteVisitor2.
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

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

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • 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 ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • 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...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk