The Visitor Pattern – part 9: summary

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

As this article is the last one of the series I want to give a short retrospective and summarize the advantages and disadvantages of the Visitor pattern.

 

Retrospective

The article series started with a short introduction of the Visitor pattern, shows its strength but also explained the reasons for existing confusions and misunderstandings regarding this pattern. To remove these misunderstandings, we started with an investigation regarding the technical needs for this pattern and explored the difference of single dispatch and double dispatch.

Based on the technical needs we could derive the aims of the pattern. So, the article series continued with explanations and examples for the two main needs: enumeration and dispatching. A further article combined these two aspects to create a wholesome Visitor pattern example.

Based on this Visitor pattern example we investigated some additional questions often seen in practical application of the pattern. We created an extended enumerator Visitor with access to container elements which are not part of the visible objects and we analyzed the pros and cons of a monolithic single Visitor versus small specialized Visitors.

At next there followed articles about topics regarding the code quality. We learned how to create reusable enumerator and query visitors following the separation of concerns rule. And we have seen some examples which can lead to over engineering if we use the Visitor pattern.
 

Based on these articles I want to summarize the advantages and disadvantages of the pattern. Additional, as the pattern is a good candidate for over engineering, I want to name some use cases for alternative patterns.

Within the articles of this series we have seen some different implementations of the pattern. They all following the same main principles but have some differences in implementation according the practical needs of the respective use cases. Therefore, I don’t want to finish the article with a default implementation of the pattern as there is no singe default implementation. Instead I want to summarize the implementation aspect by list the involved components, the concepts to connect these components and I want to give some recommendations according the naming of the Visitor interfaces and methods.

 

Advantages

In object-oriented implementations, data classes often inherit from base classes or interfaces. Data containers which hold collections of these data classes often store pointers to the base classes. This object-oriented concept works fine as long as no type specific methods are needed. In case the client wants to call type specific methods he must cast the base class pointer to the specific type. This may result in code which is hard to understand, hard to maintain and has a good chance for errors.

The Visitor pattern resolves this issue. It allows a type safe access to the origin data type without the need of casting. Furthermore, the Visitor pattern offers traversing mechanism. As result, the pattern allows an easy access of data elements independent whether they are stored in a simple list or a complex container and independent whether they are stored in origin type or as pointers to any base type.

 

Disadvantages

It isn’t easy to find the right balance between many small specialized Visitor interfaces and a few or a single monolithic wide Visitor interface. And both approaches came with drawbacks. Many small interfaces increasing the complexity of the system and few wide interfaces lead to implementations containing empty method implementations.

The Visitor pattern traverse elements of a data container. This traversing is done via double dispatch. As a result of this dispatching, the traversing is some kind of hidden feature. A client implements the dispatching callbacks but he may easily overlock that the callback is one step of a traversing over a data container. On one hand that’s fine because the client should not have to think about the data structures, but on the other hand this may cause issues. Like in any other traversing mechanism, the client should not change the data container during traversing. So, the dispatching callback methods of the client should not change the data container itself. Furthermore, a traversing step should normally not execute long running complex functionalities. In summary, the traversing is some kind of hidden feature for a client developer, but it is an important software design fact which has to be respected by the client developer. This contradiction may result in bad software designs, especially if the developer isn’t that familiar with the Visitor pattern.

 

Alternatives

The Visitor pattern is a powerful but complex pattern. It unites two functionalities: type conversation by dispatching and traversing of complex data structures. But if only one of these functionalities is needed, you will find more specialized patterns which are less complex. If you want to traverse over a data structure only, you should implement an Iterator pattern. And if you want to do the dispatching only, you may use Callback mechanisms.

 

Components and Dependencies

The Visitor pattern contains the following components.

  • Data Elements
  • Data Container
  • Enumerator
  • Algorithm

The Data elements are the Visitable components and the Enumerator and Algorithm together are the Visitor component which execute some functionality based on the data elements. As explained and shown within the article series, I prefer a clear separation of concerns. Therefore, I recommend implementing the Algorithm aspect and the Enumeration aspect in different and independent components. Different Algorithms and different Enumeration can be loosely coupled by composition according the use case specific needs. A strong coupling by using inheritance should only be used in case there is no need for a reuse of any of the components.

 

Naming

At the very beginning of the article series I mentioned that, in my opinion, the Visitor pattern is one of the most misused and misunderstand patterns and the main source of this issue is the misleading naming of the pattern interfaces and methods.

Names of methods, interfaces and components should reflect their purpose. Therefore, I recommend avoiding the naming which is used within the origin Visitor pattern as this naming is meaningless. Following I would give you some naming I like to use. But of course, feel free to find your own naming.

As the name Visitor is well known and most software developers know the pattern, you should use this name anyway, even if it is universal and therefore some kind of meaningless. You should use the name but extend it with a more describing naming. As mentioned before, we have a separation of concerns and we may create specialized visitors. Therefore, we may create visitors for following purposes: enumeration, queries, data updates and so on. So, you should not implement a “IVisitor” interface or a “Visitor” component, but specialized ones like a “CustomerEnumerationVisitor” or a “OrderQueryVisitor”. Some may argue that implementation details should not part of naming. That’s totally fine, but as the Visitor contains the hidden enumeration feature it may be very useful for a client developer to know that he uses a component which is implemented as Visitor. Therefore, in this case it is fine to add the “Visitor” prefix to the component name.

The data elements will be passed to the Visitors. Therefore, I would call them “Visitable”. This results in according interface names like “IVisitableCustomer” or “IVisitableOrder”.

But what’s with the method names? The origin method names are “accept” and “visit”. To be honest, I think these names are terrible. They don’t say anything about their purpose. So, let’s think about the purpose of the methods and find some better names. The Visitor pattern is implemented by double dispatch. For this purpose, the data elements offer a method to get the instance of the data element. This getter method will pass the element instance to an instance receiver method which is provided by the Visitor. The method names should reflect this dispatching process. So, let’s call the method to get the element instance “GetInstance” and call the method which receives these instance “InstanceReceiver”.

By using such a clear naming, you could avoid some of the misunderstanding and errors which are a result of the complexity of the Visitor pattern.

 

Conclusion

The Visitor pattern is one of the base implementation patterns in software development. So, it should be part of the tool kit of a good software engineer. But unfortunately, the Visitor Pattern comes with a big disadvantage: it is one of the most misunderstood and misused patterns I know. This article series provided an extensive overview about the Visitor pattern and give you the needed knowledge to use the Visitor pattern within your applications.

Advertisements
Veröffentlicht unter C++ | Kommentar hinterlassen

The Visitor Pattern – part 8: over engineering

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

 

Motivation

Normally, I would like to limit myself to explain the use cases of a pattern and don’t waste time to write about the cases where you should not use this pattern. But if you read about the Visitor pattern you will find a lot of statements about the purpose and advantages of the Visitor pattern which are not directly related to the pattern. This may result in implementations using the Visitor pattern instead of some other pattern which is more suitable for the specific situation. Such kind of over engineering comes with a higher complexity of the code and therefore creates more effort and is more error prone. Within this article I want to mention some of the statements you will find about the Visitor pattern, analyze them and may find better alternatives.

 

Extensibility of the elements

A lot of articles about the Visitor pattern contain the following statement: “The Visitor pattern is used to extend the functionality of data elements without changing these data elements”.

Of course, this statement isn’t wrong. But this is a side effect of the pattern only. If you want to get this feature, you don’t have to use the Visitor pattern at all. There are better alternatives.

By using the Visitor pattern, you pass the element instance to a dispatcher which will use the data to execute some query. If you want to add some new functionality, you can create a new query and you don’t have to extend the data element. But think about an implementation without the Visitor pattern. Will you implement the data query within the data class in this case? Of course not. According to the “separation of concerns” principle you create a separate query class. This class will use the data element or data structure to execute its functionality. Therefore, if you follow the base software design concepts, like “separation of concerns”, you can extend the functionality without changing the data elements. Using the Visitor pattern is over engineering in this case.

 

Extensibility of the queries

In connection with the previous statement it is also mentioned that the Visitor allows the add new queries without changing the actual implementation. Again, this statement is true but a side effect only. The separation between data elements, data structures, enumerators and queries is a base concept of the object-oriented paradigm and therefore it is a base concept in object-oriented programming languages. No additional or special implementation pattern is needed. Again, the Visitor pattern is over engineering in case the separation is the only reason using it.

 

Use case specific Visitable methods in data elements

So far, we have implemented several examples with different visitors and visitable elements. But in all examples, we have used the same base interface: the functions “GetInstance” and “InstanceReceiver” do not have a return value and the only function parameter is the object instance.

Now we want to think about the question whether it is advantageous to break this strict concept and offer use case specific functions. For example, a Visitor can be used to validate all data elements. So, we could extend the “InstanceReceiver” function and add a new parameter which contains validation information. Or we can even define a return value for this method and the element changes its internal state according the return value, for example set a validation flag.

At first this use case specific interfaces may sound fine as they fulfill the specific needs. But I would not recommend such a specialization of the interface as it comes with big disadvantages. The main purpose of the visitor pattern is the dispatching of elements. This is a very common functionality which can be used in a lot of situations. If we now mix in a use case specific interface, we limit the visitor to exactly this single case and therefore we reduce maintainability and extensibility of the implementation. Furthermore, the strength of the Visitor interface is based on the strict separation of concerns. The involved object instances are very tightly coupled. If we extend the interface and add use case specific parameters and return values, we create a strong relationship between the elements and again reduce maintainability and extensibility. Furthermore, we add some “hidden” functionality. Within the example above we added a return value to set some data within the date element after the Visitor executes something. Such object changes should be done by using setter functions or properties but nor by evaluating a return value of a function which should get the object instance. Therefore, I call it “hidden” functionality or “side effect” of the method as something is done which does not correspond with the main purpose of the method. Such side effects will reduce maintainability and are a good source for errors.

 

High effort in case a new data element is added

One disadvantage often mentioned about the Visitor pattern is the high effort resulting on a change of the data base. If you add a new data element you must add in in the Visitor interface(s) and of course adapt all visitors which implement the interface. But is this a real disadvantage of the pattern? I think no, it isn’t. Maybe it is even an advantage.

If you have a list of elements with type specific element interfaces and you want to evaluate or change the elements, you always must traverse about the list and implement type specific functionality. Independent of the used pattern or way of implementation, you must adapt or extend this implementation in case a new element type is added or an existing one is removed. So, this is a use case dependent need and not a pattern specific disadvantage.

If you use the Visitor pattern you have the advantage of a central Visitor interface. You can add the new element type to this interface, by adding a new “InstanceReceiver” function, and the compiler will show you all source code elements – all visitors – which must be adapted. If you use other implementations, like switch cases in combination with type casts, you may have to find all code elements by yourself, which is a very error prone process.

As conclusion, this often-mentioned disadvantage of the Visitor pattern isn’t valid because it is a use case specific need and not a pattern specific result. On the contrary, you can say the Visitor pattern has the advantage to support this use case in a very easy way. You just have to change the according Visitor interface and the compiler will do the critical work and find all code elements you have to change.

 

Outlook

The next article will finish the series with a summary of the whole Topic.

Veröffentlicht unter C++ | Kommentar hinterlassen

The Visitor Pattern – part 7: reusable enumerator and query visitors

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

The example implementation of this article is focused on the Visitor pattern. To keep the example as short as possible I intentional ignored some mandatory programming guidelines. Therefore, please keep in mind that the implementation should show the idea of the Visitor pattern but it ignores things like const correctness, private members or RAII (resource acquisition is initialization).

 

Reusable visitors

Within the examples so far, we created enumerator and query visitors. This separation of concerns leads to clean, easy to understand and maintainable code. The query visitors inherit from the enumerator visitors and can use their traversing features. This will allow to implement different queries based on one enumerator. But so far it is not possible to use one query based on different enumerators. As C++ supports multiple inheritance it would be possible to inherit from several enumerators. But this will result in a complex and difficult interface of the query visitor because depending on the data structure we must use an individual subset of the interface only.

It would be better to have independent components and create a loose coupling depending on the actual use case. Therefore, we should remove the inheritance which creates a very strong coupling and use composition instead.

If we use composition, we will implement independent enumerators and queries. The enumerators will be implemented as concreate classes and no longer as abstract base classes. Furthermore, the enumerators will support an enumeration interface. The queries will no longer be coupled with a concreate data structure. Instead they use the enumeration interface to access the elements, independent of the concreate structure.

Let’s implement an according example. We will use the example application of the previous articles with the order history data structure. Additional we create a new data structure which represents the article stock. To keep it simple we will use one article only. Please keep in mind that the Visitor pattern isn’t the best choice in this kind of use cases, as the dispatching aspect disappears. I removed the dispatching aspect only to focus on the composition software design.

A query should be created which lists all book titles. This query should be used together with each of the two data structures. Therefore, we want to create independent components and create a use case specific loose coupling only.

At first, we define the interfaces. Beside the well-known default Visitor interfaces, we add an additional interface for the enumerator visitor.

class IVisitable
{
public:
  virtual void GetInstance(IVisitor* visitor) = 0;
};

class IVisitor
{
public:
  virtual void InstanceReceiver(Book* book) = 0;
};

class IVisitorEnumerator : public IVisitor
{
public:
  virtual void EnumerateAll() = 0;
};

 

At next we add the date element and two data structures. The stock is a simple list and the order history a tree-like structure.

//-------------------------------------
// Element
//-------------------------------------

class Book : IVisitable
{
public:
  Book(std::string title) : mTitle(title) {};

  std::string mTitle;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

//-------------------------------------
// container 1
//-------------------------------------

class StockItem
{
public:
  StockItem(Book book, int count) : mBook(book), mCount(count) {};

  Book mBook;
  int mCount;
};

class Stock
{
public:
  std::vector mStockItems;
};

//-------------------------------------
// container 2
//-------------------------------------

class Order
{
public:
  Order(Book book, int count) : mBook(book), mCount(count) {};

  Book mBook;
  int mCount;
};

class DailyOrders
{
public:
  std::vector mOrders;
  std::string mDate;
};

class OrdersHistory
{
public:
  std::vector mDailyOrders;
};

 

Based on the enumerator visitor interface we will now be able to implement the enumerators for the two data structures. Within the constructor we will pass the query visitor and the data structure. Of course, this is a kind of implicit design definition and you may use another way to set the dependencies between the instances. For example, instead of the generic enumerator interface you can create two individual interfaces and specify these methods.

class OrderHistoryEnumerator : public IVisitorEnumerator
{
public:
  OrderHistoryEnumerator(IVisitor& elementsReceiver, OrdersHistory& ordersHistory) 
    : mElementsReceiver(elementsReceiver), mOrdersHistory(ordersHistory) {};

  void EnumerateAll()
  {
    for (auto& dailyOrders : mOrdersHistory.mDailyOrders)
    {
      std::for_each(dailyOrders.mOrders.begin(), dailyOrders.mOrders.end(),
        [&](Order& order){order.mBook.GetInstance(this); });
    }
  }

  void InstanceReceiver(Book* book)
  {
    mElementsReceiver.InstanceReceiver(book);
  }

private:
  IVisitor& mElementsReceiver;
  OrdersHistory& mOrdersHistory;
};


class StockEnumerator : public IVisitorEnumerator
{
public:
  StockEnumerator(IVisitor& elementsReceiver, Stock& stock) 
    : mElementsReceiver(elementsReceiver), mStock(stock) {};

  void EnumerateAll()
  {
    std::for_each(mStock.mStockItems.begin(), mStock.mStockItems.end(),
      [&](StockItem& item){item.mBook.GetInstance(this); });
  }

  void InstanceReceiver(Book* book)
  {
    mElementsReceiver.InstanceReceiver(book);
  }

private:
  IVisitor& mElementsReceiver;
  Stock& mStock;
};

 

The query itself is very easy to implement. You pass the enumerator visitor and execute enumeration and you implement the instance receiver methods. Like before you may use implicit design rules or you may define a query visitor interface to define the “ExecuteQuery” method.

class BookTitlesQuery : public IVisitor
{
public:

  std::vector ExecuteQuery(IVisitorEnumerator& enumerator)
  {
    mTitles.clear();
    enumerator.EnumerateAll();
    return mTitles;
  }

  void InstanceReceiver(Book* book)
  {
    mTitles.push_back(book->mTitle);
  }

private:
  std::vector mTitles;
};

 

As mentioned before, this implementation approach allows a loose and use case specific coupling of the components. Within a test application we can therefore create the data structures, the enumerators and the queries and we use composition to create the connection between the components. As you can see, this connection is created within the context of the test method only. Such a loose coupling will allow an easy maintenance and extension of the source code. It will be easy to add new queries based on the existing enumerators as well as add new data structures and enumerators and use them within existing queries.

int _tmain(int argc, _TCHAR* argv[])
{
  // prepare data
  Book book1("My book 1");
  Book book2("My book 2");
  Book book3("My book 3");
  Book book4("My book 4");
  Book book5("My book 5");

  Stock stock;
  stock.mStockItems.push_back(StockItem(book1, 20));
  stock.mStockItems.push_back(StockItem(book2, 30));
  stock.mStockItems.push_back(StockItem(book3, 10));
  stock.mStockItems.push_back(StockItem(book4, 50));
  stock.mStockItems.push_back(StockItem(book5, 10));

  DailyOrders dailyOrders1;
  DailyOrders dailyOrders2;

  dailyOrders1.mDate = "20180101";
  dailyOrders1.mOrders.push_back(Order(book1, 3));
  dailyOrders1.mOrders.push_back(Order(book2, 4));
  dailyOrders1.mOrders.push_back(Order(book4, 2));

  dailyOrders2.mDate = "20180102";
  dailyOrders2.mOrders.push_back(Order(book2, 3));
  dailyOrders2.mOrders.push_back(Order(book4, 2));
  dailyOrders2.mOrders.push_back(Order(book5, 2));

  OrdersHistory ordersHistory;
  ordersHistory.mDailyOrders.push_back(dailyOrders1);
  ordersHistory.mDailyOrders.push_back(dailyOrders2);

  // execute queries
  std::vector titles;

  BookTitlesQuery bookTitlesQuery;

  StockEnumerator stockEnumerator(bookTitlesQuery, stock);
  OrderHistoryEnumerator historyEnumerator(bookTitlesQuery, ordersHistory);

  titles = bookTitlesQuery.ExecuteQuery(stockEnumerator);
  
  std::cout << std::endl;
  std::cout << "---Stock---" << std::endl;

  std::for_each(titles.begin(), titles.end(),
    [](std::string title){std::cout << title << std::endl;; });

  titles = bookTitlesQuery.ExecuteQuery(historyEnumerator);

  std::cout << std::endl;
  std::cout << "---Order History---" << std::endl;

  std::for_each(titles.begin(), titles.end(),
    [](std::string title){std::cout << title << std::endl;; });

  return 0;
}

 

Assessment

The enumerator and query Visitors together form a unit which is needed to solve a use case. Instead of implementing fix units we are now able to implement independent components and connect them depending on the use cases. This advantage of maintainable and extensible code comes with a minor disadvantage. As you can see within the example we have to define and implement some additional interfaces. But this additional work is negligible. Furthermore, the enumerator visitors must implement all visitor methods even if they are used only, to pass on the element instance to the query visitor.

I would recommend using composition over inheritance as the advantages go far beyond the disadvantages. Of course, of you have a very fixed use case with data structure specific queries only, you could use the inheritance concept. If you don’t use the flexibility of independent enumerators and queries, then you don’t need to implement such a flexibility.

 

Outlook

Within the next articles we will think about use cases which are often used as examples for the Visitor pattern but which could be implemented easier by using other patterns and we will finish the article series with a summary of the whole topic.

Veröffentlicht unter C++ | Kommentar hinterlassen

C# 7: binary literals, digit separators, out variables

Within this article I want to introduce some of the minor but helpful new features. These are binary literals, digit separators and out variables.

Binary Literal

So far, we could use decimal and hexadecimal literals in C#. With C# 7.0 a third type is supported, the binary literal.

static void Main(string[] args)
{
  int x = 42;       // decimal literal
  int y = 0x2A;     // hexadecimal literal
  int z = 0b101010; // binary literal
}

Digit Separators

Decimal, hexadecimal and binary literals may be difficult to read if they are long. With C# 7.0 we could use the underscore character within such literals as digit separators. The use of digit separators will not change the meaning of the literal. But you can increase the readability of your literals if you use them wisely.

static void Main(string[] args)
{
  int x = 1_542;          // decimal literal
  int y = 0x2A_BF_71_4D;  // hexadecimal literal
  int z = 0b1011_1010;    // binary literal

  int max = Math.Max(1_513, 2_712);
}

Out Variables inline declaration

With C#7 output variables can be declared inline directly when passing to the method.

static void Main(string[] args)
{
  // C# 6 style
  int x;
  DoSomething(out x);
  Console.WriteLine(x);

  // C# 7 style
  DoSomething(out int y);
  Console.WriteLine(y);
}

private static void DoSomething(out int value)
{
  value = 5;
}

In my opinion this feature is a matter of taste. On the one hand you become the possibility to slim down the source code but on the other hand the variable declaration will be hidden inside the method call. In some situations, the source code readability may be increased by using the inline declaration but in other cases the explicit declaration outside of the method will increase the readability. For example, in cases were the variable is used in several places within a complex function it may be better to declare it explicitly at the beginning of the function instead of using the hidden inline declaration.

Veröffentlicht unter C# | Kommentar hinterlassen

The Visitor Pattern – part 6: specialized visitors

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

The example implementation of this article is focused on the Visitor pattern. To keep the example as short as possible I intentional ignored some mandatory programming guidelines. Therefore, please keep in mind that the implementation should show the idea of the Visitor pattern but it ignores things like const correctness, private members or RAII (resource acquisition is initialization).

Specialized visitors

Implementation of the Visitor pattern often tends to be create wide interfaces with a huge number of methods. This happens because normally there is a big number of visitable elements already on start of the implementation and this number will get increased during the project lifetime. Generally, you should avoid wide interfaces as they are often complex and difficult to use. As the Visitor pattern creates one kind of methods only, the complexity of the interface stays constant no matter whether you have ten or hundred interface methods. But on implementation of the Visitor you will feel the pain if you have to implement such a huge number of methods. For example, let’s say you want to implement a couple of queries which should work with a group of elements only. Even if you are interested in this element group only, you have to implement all other interface methods too. This may result in a huge number of empty method implementations.

But no fear, there is an easy way out of this situation. Instead to follow a “one fits it all” approach, you may implement specialized visitors. But before we think about this kind of implementation I want to mention another solution which is often mentioned in this context but which comes with a big disadvantage. To avoid the need to implement empty methods you could define empty visitor interfaces. Instead of this explicit interface methods, you will create an implicit method definition e.g. within a design document. Within your elements you will now be able to implement this implicit method or not. If you implement it you can use type cast to check whether the visitor supports this method and is able to receive the element instance. This kind of implementation removes the disadvantage of the empty methods but comes with the bid disadvantage of some kind of implicit interface definition somewhere in your project documentation. And of course, this is a good source for implementation errors. Therefore, I don’t recommend this kind of implementation.

Instead I would prefer specialized visitors. That means I want to create several visitors which operate on a group of elements only. Let’s think about the following element structure: you have a shop with several articles and within these articles you have some soup groups like food.

  • Article
    • Book
    • Food
      • Cheese
      • Sausage

Within this little tree structure, we can find three leaves: Book, Cheese and Sausage. These elements are good candidates for the visitable interface. Furthermore, we have two nodes –  Article and Food – which are good candidates for the visitor interface.

The following source code shows a possible implementation of the two different Visitors. At first we define the interfaces for the Article Visitor and the Food Visitor and the according visitable elements.

class IArticleVisitor
{
public:
  virtual void InstanceReceiver(Book* book) = 0;
  virtual void InstanceReceiver(Cheese* radio) = 0;
  virtual void InstanceReceiver(Sausage* cheese) = 0;
};

class IFoodVisitor
{
public:
  virtual void InstanceReceiver(Cheese* book) = 0;
  virtual void InstanceReceiver(Sausage* radio) = 0;  
};

class IVisitableArticle
{
public:
  virtual void GetInstance(IArticleVisitor* visitor) = 0;
};

class IVisitableFood
{
public:
  virtual void GetInstance(IFoodVisitor* visitor) = 0;
};

Based on these interfaces we can implement the date elements.

class Article : IVisitableArticle
{
public:
  int mPrice;

  virtual void GetInstance(IArticleVisitor* visitor) {};
};

class Book : public Article
{
public:
  Book(std::string title) : mTitle(title) {};

  std::string mTitle;

  void GetInstance(IArticleVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Food : public Article, IVisitableFood
{
public:
  virtual void GetInstance(IFoodVisitor* visitor) {};
};

class Cheese : public Food
{
public:
  Cheese(std::string manufacturer, std::string name)
    : mManufacturer(manufacturer), mName(name){};

  std::string mManufacturer;
  std::string mName;

  void GetInstance(IArticleVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
  
  void GetInstance(IFoodVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Sausage : public Food
{
public:
  Sausage(std::string brandName)
    : mBrandName(brandName){};

  std::string mBrandName;

  void GetInstance(IArticleVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }

  void GetInstance(IFoodVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

The implementation of the Visitors is done according to the examples we have seen in the previous articles. Therefore, the following code shows a template only and you can have a look into the previous articles for more information about implementation of the data containers, the Enumerator Visitors and the Query Visitors.

class MyFoodQuery : public IFoodVisitor
{
public:

  void ExecuteQuery(std::vector& foods)
  {    
  }

  void InstanceReceiver(Cheese* cheese)
  {
    // ...   
  }

  void InstanceReceiver(Sausage* sausage)
  {
    // ...
  }
  
private:
  std::vector mDescriptions;
};

Inherit Interfaces

Within the above example implementation of the interfaces you may see a possibility for a code optimization. The underlying elements which are covered by the Visitors exist in a base-derived relationship. If we have this kind of element hierarchy, the Visitor interfaces can also inherit from each other. So, we can use the “BaseVisitor” as base class of the “ArticleVisitor” to create a visitor interface hierarchy.

Interface Concept

During software design phase, you may want to decide about the structure of your Visitors. Should you create one Visitor for each group of elements or should you combine some of them or is it better to create one Visitor with all elements? Of course, this question is use case specific and depends on your application. Following I want to give you some common guidelines which may help you during software design.

Beside the “empty methods” the Visitor pattern comes with the downside of a bad maintainability in case the data elements change. If you must introduce new data elements, you must change all existing Visitors too. You can weaken both disadvantages if you create specialized interfaces because the data structure changes will affect a part of you Visitors only. As conclusion: if you have fix data elements and data structures you could use a small number of wide and general Visitor interfaces and if you have variable structures with a high possibility of changes, you should use specialized Visitor interfaces.

During software design phase, you can define the Visitor interfaces by using a top-down or bottom-up approach. The top-down approach starts at use case level. You should analyze your use cases and think about the concrete queries needed in these cases. At next you can think about the data elements needed in these queries. Based on the knowledge about the queries and their data elements, you can look for similarities and differences and create according use case specific Visitor interfaces.

If you don’t know the use cases and queries yet, or if you already know that there will be a lot more use cases in future, you can use the bottom-up approach. This method is based on the data. You should analyze the data structure and create Visitor interfaces which are suitable for software extensions and maintenances. As we already know, this can be solved with specialized Visitor interfaces. You should analyze the structure of your data. Like in the example above, your data normally has a tree-like inheritance architecture. Each tree node is a possible candidate for a Visitor interface. Of course, if you have a lot of nodes you may not create the same number of Visitors. Instead you should find the most important ones. Furthermore, you should create an architecture of inherited Visitors equal to the data structure. This will allow an easy integration of additional Visitors for nodes without Visitor in future.

Assessment

Often, the “one fits it all” approach results in a rigid software design which isn’t suitable in case of extensions or code maintenance. Therefore, you should create specialized Visitor patterns. The challenge is to find the right balance between too less wide interfaces and too much small interfaces. The top-down or bottom-up approach may help you during software design phase to define the interfaces according to the use cases and/or the data structure.

Outlook

Within the next articles we will think about the reusability of the different Visitors and remove the currently existing strict relationship between the enumerator and query Visitors. Furthermore, we will think about use cases which are often used as examples for the Visitor pattern but which could be implemented easier by using other patterns. This should help to avoid a misuse of the pattern, resulting in over-engineering. And finally, we will finish the article series with a summary of the whole topic.

Veröffentlicht unter C++ | Kommentar hinterlassen

The Visitor Pattern – part 5: extended enumerator visitor

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

The example implementation of this article is focused on the Visitor pattern. To keep the example as short as possible I intentional ignored some mandatory programming guidelines. Therefore, please keep in mind that the implementation should show the idea of the Visitor pattern but it ignores things like const correctness, private members or RAII (resource acquisition is initialization).

 

Extended enumerator visitor

Within the previous articles we have seen the enumerator Visitor. This kind of Visitor is responsible to traverse a data structure and enumerate all elements. It is used by the query Visitors or algorithm Visitors.

As we have seen, this separation of concerns results in a clean code with reusable Visitors. But there may be one issue we didn’t consider so far. Data structures may not contain the elements only. The may contain additional information too. This additional information could be needed by the query Visitors but at the moment our implementation will enumerator the elements only and does not provide the additional information.

Let’s have a look at the example we used so far. We have defined the Visitor interface and created some element objects.

//-------------------------------------
// forward declaration
//-------------------------------------

class Book;
class Radio;
class Cheese;

//-------------------------------------
// interfaces
//-------------------------------------

class IVisitor
{
public:
  virtual void InstanceReceiver(Book* book) = 0;
  virtual void InstanceReceiver(Radio* radio) = 0;
  virtual void InstanceReceiver(Cheese* cheese) = 0;
};

class IVisitable
{
public:
  virtual void GetInstance(IVisitor* visitor) = 0;
};

//-------------------------------------
// Elements
//-------------------------------------

class Article : IVisitable
{
public:
  int mPrice;

  virtual void GetInstance(IVisitor* visitor) {};
};

class Book : public Article
{
public:
  Book(std::string title, int price) : mTitle(title)
  {
    mPrice = price;
  };

  std::string mTitle;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Radio : public Article
{
public:
  Radio(std::string manufacturer, std::string model, int price)
    : mManufacturer(manufacturer), mModel(model)
  {
    mPrice = price;
  };

  std::string mManufacturer;
  std::string mModel;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Cheese : public Article
{
public:
  Cheese(std::string manufacturer, std::string name, int price, int importDuty)
    : mManufacturer(manufacturer), mName(name), mImportDuty(importDuty)
  {
    mPrice = price;
  };

  std::string mManufacturer;
  std::string mName;

  int mImportDuty;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

 

As data container, we have a tree like structure which contains the history of ordered articles.

class Order
{
public:
  Order(Article* article, int count) : mArticle(article), mCount(count) {};

  Article* mArticle;
  int mCount;
};

class DailyOrders
{
public:
  std::vector mOrders;
  std::string mDate;
};

class OrdersHistory
{
public:
  std::vector mDailyOrders;
};

 

As you can see, this data container stores the elements and it contains additional information like the count and data of orders. A standard use case based on this data may be to calculate the total turnover. In this case we need the count-information stored within the order element. An easy solution is to create an extended enumerator Visitor. I call it “extended” because the enumerator will traverse over the elements and additional it will provide extended information about the data structure which stores the elements.

class ElementsEnumerator : IVisitor
{
public:
  void EnumerateAll(OrdersHistory& ordersHistory)
  {
    for (auto& dailyOrders : ordersHistory.mDailyOrders)
    {
      for (auto& order : dailyOrders.mOrders)
      {
        mActualOrder = &order;
        order.mArticle->GetInstance(this);
      }
    }
  }

  int ActualOrderCount() { return mActualOrder->mCount; };

private:
  Order* mActualOrder;
};

 

This extended enumerator Visitor is quite simple. It is based on the standard enumerator Visitor we used so far and adds a property only. So, we can reuse or extend existing code. The extended enumerator Visitor can be used within the query to calculate the total turnover.

class TurnoverQuery : public ElementsEnumerator
{
public:

  int ExecuteQuery(OrdersHistory& ordersHistory)
  {
    mTurnover = 0;

    EnumerateAll(ordersHistory);

    return mTurnover;
  }

  void InstanceReceiver(Book* book)
  {
    mTurnover += book->mPrice * ActualOrderCount();
  }

  void InstanceReceiver(Radio* radio)
  {
    mTurnover += radio->mPrice * ActualOrderCount();
  }

  void InstanceReceiver(Cheese* cheese)
  {
    mTurnover += (cheese->mPrice + cheese->mImportDuty) * ActualOrderCount();
  }

private:
  int mTurnover;
};

And again, we have the same advantage. The query Visitors can be implemented in the same way as we already seen in the other examples. We do not reinvent the wheel or create a new concept. We use the existing concept and extend it with the new features.

The following code shows the example application which creates some data elements and executes the query.

int _tmain(int argc, _TCHAR* argv[])
{
  // prepare data
  Book* book = new Book("My book", 5);
  Radio* radio = new Radio("My manufacturer", "My model", 30);
  Cheese* cheese = new Cheese("My manufacturer", "My name", 5, 2);

  DailyOrders dailyOrders1;
  DailyOrders dailyOrders2;

  dailyOrders1.mDate = "20180101";
  dailyOrders1.mOrders.push_back(Order(book, 3));
  dailyOrders1.mOrders.push_back(Order(radio, 4));
  dailyOrders1.mOrders.push_back(Order(cheese, 2));

  dailyOrders2.mDate = "20180102";
  dailyOrders2.mOrders.push_back(Order(book, 3));
  dailyOrders2.mOrders.push_back(Order(cheese, 2));

  OrdersHistory ordersHistory;
  ordersHistory.mDailyOrders.push_back(dailyOrders1);
  ordersHistory.mDailyOrders.push_back(dailyOrders2);

  // execute queries
  int turnover = TurnoverQuery().ExecuteQuery(ordersHistory);

  std::cout << "turnover: " << turnover << std::endl;

  // delete data
  delete book;
  delete radio;
  delete cheese;

  return 0;
}

 

Assessment

As mentioned before, the implementation has the big advantage of matching with the base implementation of the pattern. These kinds of extended enumerators are equal to the simple enumerators. This increases the readability of the source code and does not add new complexity. Furthermore, it allows to extend existing source code without impact to the queries or algorithms which use the existing enumerator.

The downside of the concept is the separation of the data accesses. The query must get the needed data over two different interfaces and principles. The main data, stored within the data element, will be forwarded to the query by the double dispatch mechanism. The additional data will be determined by accessing the according properties via single dispatch. This will result in a more complex data query mechanism.

An alternative would be to make the needed structure elements visitable too. In this case the standard dispatching could be used. But this will increase the complexity of the whole data access mechanism because new we have a system of dependent elements. So far, the elements are independent of each other. If we make a structure element visitable which contains visitable elements, we have a hierarchical structure of inner and outer visitable objects. Writing enumerator Visitors and query Visitors will now become more difficult as we always have to think about whether we want to visit the inner element, the outer element or both. This will become very complex in systems with a deep element hierarchy. Beside the complexity of the system, such a kind of implementation will inevitable result in specialized enumerators and queries.

 

Outlook

Within the next articles we will think about the reusability of the different Visitors and remove the currently existing strict relationship between the enumerator and query Visitors. Furthermore, we will think about use cases which are often used as examples for the Visitor pattern but which could be implemented easier by using other patterns. This should help to avoid a misuse of the pattern, resulting in over-engineering. And finally, we will finish the article series with a summary of the whole topic.

Veröffentlicht unter C++ | Kommentar hinterlassen

The Visitor Pattern – part 4: complete visitor

This article is one part of a series about the Visitor pattern. Please read the previous articles to get an overview about the pattern and to get the needed basics to understand this article.

The example implementation of this article is focused on the Visitor pattern. To keep the example as short as possible I intentional ignored some mandatory programming guidelines. Therefore, please keep in mind that the implementation should show the idea of the Visitor pattern but it ignores things like const correctness, private members or RAII (resource acquisition is initialization).

 

Complete visitor

Within the previous articles we have seen two use cases for the Visitor pattern: enumeration and dispatching. For each use case, we have implemented an according Visitor. In the context of these implementations we have seen that the Visitor pattern is over-engineering for the single use cases. There exist lightweight patterns exactly matching such single tasks. But if the two uses cases come together, the Visitor pattern may be appropriate. Therefore, only in this case we have a “complete” Visitor which unites enumeration and dispatching features.

Like in the previous examples we start with the interfaces.

class Book;
class Radio;
class Cheese;

class IVisitor
{
public:
  virtual void InstanceReceiver(Book* book) = 0;
  virtual void InstanceReceiver(Radio* radio) = 0;
  virtual void InstanceReceiver(Cheese* cheese) = 0;
};

class IVisitable
{
public:
  virtual void GetInstance(IVisitor* visitor) = 0;
};

 

Again, we want to use the three different article elements based on a common base class.

class Article : IVisitable
{
public:
  int mPrice;

  virtual void GetInstance(IVisitor* visitor) {};
};

class Book : public Article
{
public:
  Book(std::string title) : mTitle(title) {};

  std::string mTitle;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Radio : public Article
{
public:
  Radio(std::string manufacturer, std::string model)
    : mManufacturer(manufacturer), mModel(model){};

  std::string mManufacturer;
  std::string mModel;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

class Cheese : public Article
{
public:
  Cheese(std::string manufacturer, std::string name)
    : mManufacturer(manufacturer), mName(name){};

  std::string mManufacturer;
  std::string mName;

  void GetInstance(IVisitor* visitor)
  {
    visitor->InstanceReceiver(this);
  }
};

 

As in the enumeration example we will store the elements within a data container which contains the history of orders.

class Order
{
public:
  Order(Article* article, int count) : mArticle(article), mCount(count) {};

  Article* mArticle;
  int mCount;
};

class DailyOrders
{
public:
  std::vector mOrders;
  std::string mDate;
};

class OrdersHistory
{
public:
  std::vector mDailyOrders;
};

 

For the enumeration feature, we will use two generic enumerator Visitors. One will enumerate all elements and the other one will additionally filter distinct elements and therefore enumerate each object instance only once.

class ElementsEnumerator : IVisitor
{
public:
  void EnumerateAll(OrdersHistory& ordersHistory)
  {
    for (auto& dailyOrders : ordersHistory.mDailyOrders)
    {
      std::for_each(dailyOrders.mOrders.begin(), dailyOrders.mOrders.end(),
        [&](Order& order){order.mArticle->GetInstance(this); });
    }
  }
};

class DistinctElementsEnumerator : IVisitor
{
public:
  void EnumerateAll(OrdersHistory& ordersHistory)
  {
    mAlreadyEnumerated.clear();

    for (auto& dailyOrders : ordersHistory.mDailyOrders)
    {
      for (auto& order : dailyOrders.mOrders)
      {
        auto iterator = std::find(mAlreadyEnumerated.begin(), mAlreadyEnumerated.end(), reinterpret_cast(order.mArticle));
        if (iterator == mAlreadyEnumerated.end())
        {
          mAlreadyEnumerated.push_back(reinterpret_cast(order.mArticle));
          order.mArticle->GetInstance(this);
        }
      }
    }
  }

private:
  std::vector mAlreadyEnumerated;
};

 

Bases on the generic enumeration Visitors we can implement the query functions as dispatching Visitors. Here we have two queries. One will list the complete order history and the other one an overview with the ordered elements.

class OrderedArticlesHistoryQuery : public ElementsEnumerator
{
public:

  std::vector ExecuteQuery(OrdersHistory& ordersHistory)
  {
    EnumerateAll(ordersHistory);

    return mDescriptions;
  }

  void InstanceReceiver(Book* book)
  {
    mDescriptions.push_back("Book: " + book->mTitle);
  }

  void InstanceReceiver(Radio* radio)
  {
    mDescriptions.push_back("Radio: " + radio->mManufacturer + ", " + radio->mModel);
  }

  void InstanceReceiver(Cheese* cheese)
  {
    mDescriptions.push_back("Cheese: " + cheese->mManufacturer + ", " + cheese->mName);
  }

private:
  std::vector mDescriptions;
};


class OrderedArticlesQuery : public DistinctElementsEnumerator
{
public:

  std::vector ExecuteQuery(OrdersHistory& ordersHistory)
  {
    EnumerateAll(ordersHistory);

    return mDescriptions;
  }

  void InstanceReceiver(Book* book)
  {
    mDescriptions.push_back("Book: " + book->mTitle);
  }

  void InstanceReceiver(Radio* radio)
  {
    mDescriptions.push_back("Radio: " + radio->mManufacturer + ", " + radio->mModel);
  }

  void InstanceReceiver(Cheese* cheese)
  {
    mDescriptions.push_back("Cheese: " + cheese->mManufacturer + ", " + cheese->mName);
  }

private:
  std::vector mDescriptions;
};

 

Now we can use the Visitor within an example. At first, we create a data history element container and then we use the two query Visitors to get the according information about the ordered articles.

int _tmain(int argc, _TCHAR* argv[])
{
  // prepare data
  Book* book = new Book("My book");
  Radio* radio = new Radio("My manufacturer", "My model");
  Cheese* cheese = new Cheese("My manufacturer", "My name");

  DailyOrders dailyOrders1;
  DailyOrders dailyOrders2;

  dailyOrders1.mDate = "20180101";
  dailyOrders1.mOrders.push_back(Order(book, 3));
  dailyOrders1.mOrders.push_back(Order(radio, 4));
  dailyOrders1.mOrders.push_back(Order(cheese, 2));

  dailyOrders2.mDate = "20180102";
  dailyOrders2.mOrders.push_back(Order(book, 3));
  dailyOrders2.mOrders.push_back(Order(cheese, 2));  

  OrdersHistory ordersHistory;
  ordersHistory.mDailyOrders.push_back(dailyOrders1);
  ordersHistory.mDailyOrders.push_back(dailyOrders2);

  // execute queries
  std::vector titles;

  titles = OrderedArticlesHistoryQuery().ExecuteQuery(ordersHistory);

  std::cout << "---Complete Articles History---" << std::endl;

  std::for_each(titles.begin(), titles.end(),
    [](std::string title){std::cout << title << std::endl;; });

  titles = OrderedArticlesQuery().ExecuteQuery(ordersHistory);

  std::cout << std::endl;
  std::cout << "---Ordered Articles---" << std::endl;

  std::for_each(titles.begin(), titles.end(),
    [](std::string title){std::cout << title << std::endl;; });

  // delete data
  delete book;
  delete radio;
  delete cheese;

  return 0;
}

 

Assessment

The implemented Visitor contains enumeration and dispatching of the elements. The implementation has the same complexity as the ones seen in the previous examples which solved one use case only. But this time the disadvantage of the complex pattern is exceeded by the advantage that we can solve both use cases at once. The Visitor pattern will match perfectly with this kind of implementation task and therefore it can show its strength.

But of course, there are still some open questions. For example: Will it be easy to add new elements? How can we write queries which have to access some other properties of the data container, like the count of ordered elements? Can we reuse enumerator Visitors?

We will look at these questions soon and see whether the Visitor pattern is suitable to solve these use cases or whether we reach the borders of the pattern.

 

Outlook

Within the next articles we want to analyze some advanced topics, like reusable enumerator Visitors or Visitors which can access not visitable elements of the data container.

Veröffentlicht unter C++ | Kommentar hinterlassen