Visitor Design Pattern Notes

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.

Continue reading

Node.js nut-ioc usage

nut-ioc npm package is a simple, lightweight and fast IoC Container Framework.

nut-ioc injects dependencies run-time so that developers don't need to require modules.

Developers can implement in their codes following OOP basics, principles, patterns and concepts and probably more than that 🙂

  • Separation of Concern(SoC)
  • Single Responsibility Principle(SRP)
  • Open Closed Principle
  • Dipendency Inversion(Injection) Principle(DIP)
  • Chain of Responsibility Pattern
  • Aspect Oriented Programming

you can reach github repository.

https://github.com/nodejs-projects-kenanhancer/nut-ioc

Installing nut-ioc with npm

npm i nut-ioc

Demo GitHub Repository

You can find different usages of nut-ioc framework in separate brances.

https://github.com/nodejs-projects-kenanhancer/nut-ioc-basic-demo.git

Branch list
load-dependencies-with-dependencyPath
load-dependencies-with-dependencyPath-and_dynamically
load-dependencies-with-different-loaders
load-dependencies-with-interceptors
load-dependencies-with-new-loaders-and-filters
load-dependencies-with-programatic-notation
nut-swagger-usage
Continue reading

Node.js nut-pipe usage

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.

Continue reading

Designing OOP Step by Step

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 🙂

Continue reading

C# Asynchronous Programming Patterns

There are 3 Asynchronous Programming Patterns in C#.

1- Asynchronous Programming Model (APM)
2- Event-based Asynchronous Pattern (EAP)
3- Task-based Asynchronous Pattern (TAP)

 

1- Asynchronous Programming Model (APM)

Asynchronous Programming Model using Delegates

you can reach that article from Calling synchronous methods asynchronously

2- Event-based Asynchronous Pattern (EAP)

3- Task-based Asynchronous Pattern (TAP)

Chain of Responsibility Pattern and Aspect Oriented Programming with C#.NET

Each class contains only the business logic code, while the aspects will be responsible of intercepting the code in order to inject the cross-cutting concerns.

This is first version of example code.

This is second version. You can find MethodInfo and parameter values in the aspect classes.