Skip to content

Instantly share code, notes, and snippets.

@neenjaw
Last active September 12, 2024 10:46
Show Gist options
  • Save neenjaw/ecee7a49ca3caddf13ebe5e5c32a2986 to your computer and use it in GitHub Desktop.
Save neenjaw/ecee7a49ca3caddf13ebe5e5c32a2986 to your computer and use it in GitHub Desktop.
Dive Into Design Patterns

Notes for Dive Into Design Patterns

Pillars of OOP

  • Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance

Relations Between Objects

  • Dependency (dotted line, arrow) - some change to one might result in changes to another
  • Association (solid line, arrow) - one object uses or interacts with another
  • Aggregation (open diamond, solid line, arrow) - one-to-many, many-to-many, or whole-part relation
  • Composistion (solid diamond, solid line, arrow) - specific aggregation one obj is composed with another

SOLID Principles

  • S - Single Responsibility Principle
  • O - Open/Closed - open for extension, closed for modification
  • L - Liskov Substitution Principle
  • I - Interface Segregation Principle
  • D - Depenency Inversion Principle

Types

Creational patterns

Factory Method

  • Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that are created.
  • Delegate object creation to a factory class
  • Different objects related to a common interface
Applicability
  • Use Facotry Method when you don't know beforehand the exact types and dependencies of the objects your code should work with.
  • Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components.
  • Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.
How to Implement
  1. make all products follow the same interface. This interface should eclare methods that make sense in every product.
  2. Add an empty factory method inside the creator class. The return type of the method should match the common product interface.
  3. Move object creation inside of the factory method, replacing the constructor call with the method call.
  4. Create set of creator subclasses for each type of product listed in the factory method, moving creation code
Pros-Cons
  • (+) avoid tight coupling
  • (+) single responsibility principle
  • (+) open/closed principle
  • (-) code complexity

Abstract Factory

Builder

Prototype

Singleton

Structural patterns

Behavioral patterns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment