The visitor pattern

The visitor pattern allows you to separate an algorithm via the visitor class from an object structure (aggregating elements) so that the new operations can be added without modifying the object structure.

It's a cleaner way by design for an old way of C++ friend class, which is allowed to access the private members of another class.

In simple words, you break the class into two classes, one only with elements/variables, and another only with the methods. In this way, you keep the one with variables the same while varying the methods, that is, algorithms in the other class. You define the methods grouped together in an interface called visitor so that you can have more than one implementation of this visitor interface, ...

Get Enterprise Application Architecture with .NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.