Skip to content

Instantly share code, notes, and snippets.

@SureshKL
Last active November 22, 2018 17:27
Show Gist options
  • Save SureshKL/be8b550c286ef1ece65690f618c36df2 to your computer and use it in GitHub Desktop.
Save SureshKL/be8b550c286ef1ece65690f618c36df2 to your computer and use it in GitHub Desktop.
Design Patterns

Design Patterns

Definition

Design Patterns are solutions to to commonly occurring problems.

We need design patterns to ensure our work is consistent, reliable and understandable.

Examples

  • Building Architecture
  • Electrical and plumbing
  • Automobile design
  • Mobile phone interfaces

Classification

  • Creational - Used to design object creation

    Examples: Singleton, Factory, Builder

  • Structural - Used to design relationship between objects

    Examples: Adapter, Facade, Composite

  • Behavioural - Used to design object interaction and responsibility

    Examples: Command, Observer, Stratergy

Object Oriented Design Principles (SOLID)

  1. Single Responsibility: Means a Class should have only one responsibility i.e, Cooking or Washing but not both
  2. Open-Closed: Class should be open for extension usually by inheritance but closed for modification
  3. Liskov Substitution: Subclasses should stand in for parents without breaking anything
  4. Interface Segregation: Many specific interfaces are better than one do it all interface (in python we use Abstract Base Class combined with multiple inheritance to achieve this)
  5. Dependency Inversion: We should program towards Abstraction not concrete Implementation cause Implementation can vary. In python ABC(Abstract Base Classes) can be used just for this.

Note: About Interfaces in Python

Unlike other languages Python do not have standard way to define interfaces. In Python we can implement Interfaces using ABC(Abstract Base Classes)

Commonly used Design Patterns

  • Behavioural Patterns

    1. Strategy Pattern

      • Also known as Policy pattern
    2. Observer Pattern

      • Also known as Dependents pattern, Publish-Subscribe pattern
      • Description: When the state of one object changes it's dependents are notified
    3. Command Pattern

      • Also known as Action pattern, Transaction pattern
  • Creational Patterns

    1. Singleton Pattern

      • Also known as Anti pattern because of it's downsides
    2. Builder Pattern

    3. Abstract Factory Pattern

  • Structural Patterns

    1. Facade Pattern

      • Unified interface to a set of interfaces
    2. Adapter Pattern

      • Bridging the incompatible APIs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment