Behavioral design patterns deal with the communication of objects. Patterns of this type vary with the assignment of responsibilities to the communicating objects and the way they interact with each other.
Do check the creational patterns and the design patterns catalogue.
Sample code – Chain of responsibility in C#. ETL has been taken as an example chain comprising of 3 objects – extract, transform and load.
Sample code – Command pattern implementation in C#.
Sample code – Interpreter implementation in C#.
Sample code – Mediator implementation in C#.
Sample code – Iterator pattern implementation in C#.
visit
method. Data structure has an accept
method which accepts the visitor and calls the visit
method of visitor passing it’s reference. Visitor uses the reference to add functionality to manipulate the data structure.
Visitor pattern is useful in modeling collections (data structure in object oriented world) in libraries where there is a chance to add custom functionality by the consuming code.
Quick example code – Visitor pattern in C#
- Customer object is the data structure.
- Print Visitor access customer object and prints its details to console.
Code sample – Template pattern in C#. Usecase – ETL. ETL process is defined in the abstract parent class ETL
. StructuredDataETL
is a sub-class of ETL
and provides the behavior for the Extract, Transfer and Load steps.
Code sample – Strategy pattern in C#..
- Family of algorithms –
IAlgorithm
with 2 implementationsAdditionStrategy
andSubtractionStrategy
- One of the above algorithms applied the context at the run time to work upon on 2 integers
Refer C# sample on state pattern.
originator
and the object where the state is saved is called momento
. Structure of momento
has to be similar to the sturucture of the originator
. There is a role called care taker
which manages the momento
objects. Momento is similar to command pattern as both of them can be passed around. Often, momento along with command pattern can be used to accomplish undo operation.
Sample code – C# implementation of momento pattern.
null
condition. Null object pattern proposes to wrap null condition in an object called Null object which does nothing. It helps avoid the popular NPE (NUll Pointer Exceptions). Java 8 Optional
follow Null object pattern.