Behavioural Patterns
These patterns focus on how objects distribute work.
Think of a behavioral pattern like a race car pit crew at a track. In the pit crew, the roles of the members describe how the team is able to achieve victory, which is their common goal. Each member has a specific responsibility, their role in the race. Some members change the tires, others unmount, and mount the wheel nuts, others refuel the car, but all must work together to win
Template
Have you seen the directions on making soup or mixing a drink from powdered ingredients? They would both have similarly described steps to get a container. Empty the ingredients into the container, add water, stir and prepare. The steps are ordered the same way, with some identical steps and some different in implementation.
The template method defines an algorithm's steps generally, deferring the implementation of some steps to subclasses. It is a behavioral design pattern and is concerned with the assignment of responsibilities
-
Chain of responsibility
You visit a doctor, who then refers you to a specialist, but this specialist is busy then you are referred to another specialist who eventually treats you.
-
Say you were fixing a chair and you needed a tool to tighten a particular screw. You're not sure which type of tool to use. You have several types of screwdrivers and wrenches. What would you do? You will probably take each tool and one at a time try it on the screw until one of them works
the intent of this design pattern is to avoid coupling the sender to the receiver by giving more than one object the chance to handle the request
like email filters on Inbox, which should all be checked unless one applies or no applies
State
Since the objects in your code are aware of their current state, you can use this premise in your code. They can choose an appropriate behavior based on their current state. When their current state changes, this behavior can be altered
The state pattern is primarily used when you need to change the behavior of an object based upon the state that it's in at run-time
Command
The boss needed a worker to schedule a meeting and also needed another worker to talk to an important client about business, the boss can use memos. The boss is a very busy person and may not have time to remember which workers do what jobs and won't have time to walk to the different workers to ask them to complete the tasks. Instead, the boss could just write the tasks down onto a sticky note and a secretary could give these to the workers that can complete them. The boss is encapsulating his commands into memos, the way requests could be encapsulated into command objects in software.
One purpose of using the command pattern is to store and schedule different requests. When an object calls a method of another object,
Another important purpose of the command pattern is allowing commands to be undone or redone. Like how you can undo or redo edits in a document
Observer
Every day you visit it multiple times per day to check for new blog posts. After a while, you get fed up with this routine. There must be a better way you think to yourself
The first solution that crosses your mind is to write a script to check the blog for new posts every fraction of a second.
-