Design Patterns
Design Patterns
4 Design Patterns
• class Singleton:
• _instance = None
Factory Pattern
• - Creates objects without specifying the exact
class.
• - Useful when dealing with multiple related
object types.
• - Example in Python:
• class AnimalFactory:
• def get_animal(self, type):
• if type == 'Dog':
Observer Pattern
• - Defines a dependency between objects, so
when one changes, others are notified.
• - Used in event-driven systems like GUI
applications.
• - Example in Python:
• class Observer:
• def update(self, message):
• print(f'Observer received: {message}')
Strategy Pattern
• - Defines a family of algorithms and lets the
client choose one at runtime.
• - Helps in designing flexible systems with
interchangeable behaviors.
• - Example in Python:
• class Strategy:
• def execute(self):
• pass
Conclusion
• - **Design patterns** help solve common
coding problems efficiently.
• - The **Singleton pattern** ensures a single
instance.
• - The **Factory pattern** simplifies object
creation.
• - The **Observer pattern** manages
dependent objects dynamically.
• - The **Strategy pattern** enables
interchangeable behaviors.