🤖
@qwertyvipul | code
  • Code
  • DSA
    • Disjoint Set
    • Segment Tree
    • Bit Operations
    • Binary Exponential
    • Kadane's Algorithm
    • Modulus Multiplicative Inverse
  • Quick Notes
    • Design Patterns
    • System Design
    • React.js
  • LeetCode With JavaScript
Powered by GitBook
On this page
  • Quick notes
  • Head First Design Patterns (2nd Edition)
  • Chapter 1: The strategy pattern
  • Chapter 2: The observer pattern
  • Chapter 3: The decorator pattern
  • Chapter 4: The factory pattern
  • Chapter 5: The singleton pattern
  • Chapter 6: The command pattern
  • Chapter 7.1: The adapter pattern
  • Chapter 7.2: The facade pattern
  • Chapter 8: The template method pattern
  • Chapter 9.1: The iterator pattern
  • Chapter 9.2: The composite pattern
  • Chapter 10: The state pattern
  • Chapter 11: The proxy pattern
  • Some more patterns
  • Design principles
  • Quick Links
  1. Quick Notes

Design Patterns

PreviousQuick NotesNextSystem Design

Last updated 1 year ago

Quick notes

  1. Interface -> implementation

  2. Pattern: A pattern is a solution to a problem in a context.

    1. Context: A context is the situation in which the pattern applies. This should be a recurring situation.

    2. Problem: The problem refers to the goal you are trying to achieve in this context, but it also refers to any constraints that occur in the context.

    3. Solution: The solution is what you're after: a general design that anyone can apply that resolves the goal and set of constraints.

  3. Anti-pattern: An anti-pattern tells you how to go from a problem to a BAD solution.

Head First Design Patterns (2nd Edition)

Chapter 1: The strategy pattern

  1. Inheritance can make the rubber ducks fly

  2. Interfaces affects code reusability

  3. Encapsulate behaviors

Chapter 2: The observer pattern

  1. Publishers (subject) + subscribers (observers) = Observer Pattern

  2. Publisher notifies the subscribers

  3. Subscribers upon being notified pulls the required data from the publisher

Chapter 3: The decorator pattern

  1. Inheritance by composition and inheritance by delegation

  2. A thing or two about decorators:

    1. Decorators have the same supertype as the objects they decorate

    2. An object can be wrapped by one or more decorators

  3. Pros:

    1. Provides an alternative to subclassing or extending behavior

    2. Can extend behavior without modifying existing code

  4. Cons:

    1. Overuse can add complexity

Chapter 4: The factory pattern

  1. Factory pattern encapsulate object creation by letting sub-classes decide what objects to create.

  2. Dependency inversion: Depend upon abstractions. Do not depend upon concrete classes.

  3. Dependency inversion principles:

    1. No variable should hold a reference to a concrete class.

    2. No class should derive from a concrete class.

    3. No method should override an implemented method of any of its base classes.

  4. The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete objects.

Factory method
Abstract factory

Relies on inheritance: object creation is delegated to subclasses, which implement the factory method to create objects.

Relies on object composition: object creation is implemented in methods exposed in the factory interface.

Chapter 5: The singleton pattern

  1. The singleton pattern ensures a class has only one instance, and provides a global point of access to it.

Chapter 6: The command pattern

  1. The command pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

  2. In simple words it is like event listeners attached to different UI buttons which is fired on common onClick event.

Chapter 7.1: The adapter pattern

  1. The adapter pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

  2. Object adapter: normally the calls made by the client to the adapter ends of invoking corresponding call(s) at the adaptee.

  3. Class adapter: Not possible in Java as it requires multiple inheritance, but ideally it gives the adaptee respond to client calls as well.

Chapter 7.2: The facade pattern

  1. The facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Chapter 8: The template method pattern

  1. The template method pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Chapter 9.1: The iterator pattern

  1. The iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Chapter 9.2: The composite pattern

  1. The composite pattern allows you to compose objects into tree structures to represent part-whole heirarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Chapter 10: The state pattern

  1. The state pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Chapter 11: The proxy pattern

  1. The proxy pattern provides a surrogate or placeholder for another object to control access to it.

  2. Use the proxy pattern to create a representative object that controls access to another object, which may be remote, expensive to create, or in need of securing.

Some more patterns

  1. Bridge

  2. Builder

  3. Chain of responsibility

  4. Flyweight

  5. Interpreter

  6. Mediator

  7. Memento

  8. Prototype

  9. Visitor

Design principles

  1. Pinciple of least knowledge: talk only to your immediate friends.

  2. The hollywood principle: Don't call us, we'll call you.

  3. A class should have only one reason to change.

Quick Links

Single responsibility principle:

DRY (Don't repeat yourself):

Pure function:

View code
View code
View code
View code
View code
https://en.wikipedia.org/wiki/Single_responsibility_principle
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
https://en.wikipedia.org/wiki/Pure_function
View code
View code