/template-and-visitor

Some samples to go over the template method and visitor pattern

Primary LanguageTypeScript

Template Method and Visitor Patterns

This repo gives a small taste of what you can do with the template method pattern and the visitor pattern.

The template-method directory shows us how overriding implementation in a base class can allow us to extend a flow of logic in a process.

Pros

  • You can let clients override only certain parts of a large algorithm, making them less affected by changes that happen to other parts of the algorithm.
  • You can pull the duplicate code into a superclass.

Cons

  • Some clients may be limited by the provided skeleton of an algorithm.
  • You might violate the Liskov Substitution Principle by suppressing a default step implementation via a subclass.
  • Template methods tend to be harder to maintain the more steps they have.

The visitor directory implements the worlds smallest programming language making it easy to add numbers.

Pros

  • Open/Closed Principle. You can introduce a new behavior that can work with objects of different classes without changing these classes.
  • Single Responsibility Principle. You can move multiple versions of the same behavior into the same class.
  • A visitor object can accumulate some useful information while working with various objects. This might be handy when you want to traverse some complex object structure, such as an object tree, and apply the visitor to each object of this structure.

Cons

  • You need to update all visitors each time a class gets added to or removed from the element hierarchy.
  • Visitors might lack the necessary access to the private fields and methods of the elements that they’re supposed to work with.