Repository files navigation
Design Patterns for Software Engineering
Strategy Pattern
Observer Pattern
Factory Pattern
When to use the Strategy Pattern?
When a method returns one of several possible classes that share a common super class
Create a new enemy in a game
Random number generator picks a number assigned to a specific enemy
The factory returns the enemy associated with the number
The class is chosen at run time
When you don't know ahead of time what class object you need
when all of the potential classes are in the same subclass hierarchy
To centralize class selection code
When you don't want the user to have to know every subclass
To encapsulate object creation
UML Diagram for Strategy Pattern
When to use the Observer Pattern?
When you need many other objects to receive an update when another object changes
Examples:
Stock market with thousands of stocks need to send updates to objects representing individual stocks
The Subject (publisher) sends many stocks to the Observers
The Observers (subscribers) taken the ones they want and use them
Loose coupling is a benefit
The Subject (publisher) doesn't need to know anything about the Observers (subscribers)
Negatives: The Subject (publisher) may send updates that don't matter to the Observer (subscriber)
UML Diagram for Observer Pattern
When to use the Factory Pattern?
When you need many other objects to receive an update when another object changes
Examples:
Stock market with thousands of stocks need to send updates to objects representing individual stocks
The Subject (publisher) sends many stocks to the Observers
The Observers (subscribers) taken the ones they want and use them
Loose coupling is a benefit
The Subject (publisher) doesn't need to know anything about the Observers (subscribers)
Negatives: The Subject (publisher) may send updates that don't matter to the Observer (subscriber)
UML Diagram for Factory Pattern
About
Design Patterns for Software Engineering
Resources
Stars
Watchers
Forks
You can’t perform that action at this time.