Behavioral Design pattern — [Notes]
· Observer design pattern
∘ Pros
∘ Cons
∘ What is the difference between Publish-Subscribe and Observer Pattern?
· Strategy Design Pattern
· Command Design Pattern
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
Observer design pattern
Observer: An object that monitors for an event possibly raised by another object
Pros
- [Source] This design pattern allows information or data transfer to multiple objects without any change in the observer or subject classes.
- [Source] It adheres to the loose coupling concept among objects that communicate with each other.
- [Source] This design pattern follows the Open/Closed Principle, which says that entities should be open for extension but closed for modification. Here the observers can easily be added or removed anytime without any change in the code.
Cons
- [Source] The Observer pattern can increase complexity and potentially cause efficiency issues if it’s not executed properly.
- [Source] The fundamental shortcoming of this design pattern is that the subscribers/observers are updated in a random sequence.
What is the difference between Publish-Subscribe and Observer Pattern?
Source — https://www.scaler.com/topics/design-patterns/observer-design-pattern/
- Observer is an object-action oriented pattern, while Publish-Subscribe is a network-oriented architectural pattern.
- They’re both utilized at diverse levels of software.
- Secondly, because the observer and the subject are coupled, the observer pattern must be implemented in the same space, but because publish-subscribe is a cross-application communication pattern, there is no such limitation.
Strategy Design Pattern
For an Action there can be multiple strategies to perform that action
Strategy: An object that allows for different algorithms to be selected on the fly
- If a action can be in multiple ways then we can think of Strategy pattern
- Strategy is often used with Factory pattern (Strategy + Factory)
Command Design Pattern
Command design pattern is a behavioral design pattern (communication pattern among objects) that converts a request or an operation into an object with all the required information such as what method to call, arguments, etc. [source]
- Common Interface
→ match — if the input the same as one of the commands
→ execute — executes the command