What is a design pattern?

Design patterns :
  • Design patterns represent the best practices
  • Design patterns are solutions to general problems that software developers faced during software development.
  • Gang of Four (GOF)? :
    • In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development.
    • These authors are collectively known as Gang of Four (GOF)
    • According to these authors design patterns are primarily based on the following principles of object orientated design:
      • Program to an interface not an implementation
      • Favor object composition over inheritance
  • Usage of Design Pattern :
    • Common platform for developers
    • Best Practices
  • Types of Design Patterns :
    • Creational Patterns :
      • These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator.
      • It’ not commonly used in Python. Why? Because of the dynamic nature of the language.
      • This gives program more flexibility in deciding which objects need to be created for a given use case.
      • Ex. Singleton
      • Singleton :
        • The Singleton pattern is used when we want to guarantee that only one instance of a given class exists during runtime.
        • class Logger(object):
             def __new__(cls, *args, **kwargs):
                 if not hasattr(cls, '_logger'):
                     cls._logger = super(Logger, cls
                             ).__new__(cls, *args, **kwargs)
                 return cls._logger
        • In this example, Logger is a Singleton.
    • Structural Patterns :
      • These design patterns concern class and object composition.
      • Ex. Facade, Adapter,Decorator
    • Behavioral Patterns :
      • These design patterns are specifically concerned with communication between objects.
      • According to GOF principles, there are a total of 11 behavioral patterns in Python: Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template, Visitor.

Comments

Popular posts from this blog

What is composite key?

What are the different data types in Python?

What is __repr__ function?