Visitor classes are commonly used to perform operations on a set of objects without modifying the structure of their underlying classes.
Visitor Design Pattern allows you to add new operations(behaviours) via Visitor classes to existing classes without modifying their structure. Instead of adding the operation within the existing class, you create a visitor class named as operation you want to have in existing class, so that the operation is externalised from a set of objects without modifying the structure of their underlying classes.
Visitor Design Pattern is more about adding new operations to existing classes via Visitor classes without modifying their structure, rather than adding new fields or methods directly to the classes themselves.
Consider a system where you have a hierarchy of classes representing different shapes, like Circle, Rectangle, and Triangle. Each of these shapes implements Visitable interface(or its equivalent) to allow visitor classes to operate on them.
nut-pipe npm package is an implementation of Chain of Responsibility Pattern. We can encapsulate or wrap every aspect of code in separate module or class or function, etc. So that, separated aspect modules would implement Single Responsibility Principle(SRP) and Seperation of Concern Principle(SoC) properly.
🙂 so if you want to implement OOP basics and principles, then you can use nut.pipe npm package.
This design provides to developer clean business logic code. They don't need to think about Error, Exception, Log handling business logic. Before their business logic code is called, pipeline is called and it means first middleware is triggered then first middleware will trigger next one, but no middleware knows about next middleware. Middleware should know about its business logic then should call next one.
I know too much words but developers want to see code 🙂 same code will be developed in different ways.
Code example v1
I wrote a greeting service with sayHello() and sayGoodbye() functions. They include all business logic in their body. This is not good approach definitely. Code duplications are everywhere 🙂 For example, every functions have try/catch, console.log('ENTRY: …'), console.log('SUCCESS: …'), console.log('ERROR: …'), etc. Notice that getFullName doesn't contain these business logics. So, we can't know whether it is called or not. If developer like me 🙂 doesn't write these kind of aspects(logging and exception handling is kind of aspect) then we never understand that that method is called in run-time.
I read Head First Design Patterns book shown as below. It is pretty good reference of Design Patterns. If you want to be familiar of Design Patterns, i recommend it definitely. This post contains some examples of this book but in a different presentation. When I want to remember fundamentals of Design Patterns, this post will remind me 🙂 I hope that you will benefit from this post.
I will create a class diagram and improve it step by step.
If you don't want to read all paragraphs, you just view class diagrams and code samples step by step. But, i heavily recommend to read and think of this post 🙂