Design Patterns
Updated: October 15, 2024
Design Patterns are in every language. Some patterns pertain particularly to a specific language.
Design patterns are typical solutions to common problems. These problems can exist because a language
may be missing a level of abstraction. For example JS does not have Range but can be created with
an iterator pattern.
References:
Fireship
refactoring.guru
Table of Contents
Must know
Lets says window is an object, an instance.
It could have METHODS open and close, defined in a class.
Then it could have a state to track if it is open or closed, this would be a property.
CREATIONAL
Provide object creation mechanisms that increase flexibility and reuse of existing code.
Factory Method
- Instead of conditional checking create subclass from a superclass.
- Rather than use keyword to instantiate an object use a function or method to do it.
- Objects returned from the factory method are referred to as products.
- Subclasses can alter the class of objects being returned by the factory method.
- Can return existing objects from a cache, an object pool, or another source.
- Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time.
Abstract Factory
Prototype (clone, think inheritance class)
- Instead of inheriting functionality from a class, inherite from an object already created.
Builder
- Create object step-by-step using methods rather than a constructor.
Singleton
- Object that can only be instantiated once.
- Typical use case is in global settings.
STRUCTURAL
Explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient.
Facade
- Like a simple API that hides low level details user does not need to know about.
- Like how jQuery is a facade for Javascript.
Proxy (substitute)
- Used to be able to manipulate an object.
- Ability to intercept data and update UI when data changes.
BEHAVIORAL
Take care of effective communication and the assignment of responsibilities between objects.
Iterator
- Allows to traverse through a collection of objects.
- For Loop and Range are an abstractions of the iterator pattern.
- Is a pull based system.
Observer
- Is a push based system.
- Allows many objects to subscribe to events that are broadcast by another object.
- One to Many relationship. Like a radio signal from a tower.
- Like && Subscribe is an example of this pattern.
Mediator
- Used to handle Many-to-Many relationships.
- Like middleware that provides separation of concerns for requests being sent for a response. (place in proper format)
State
- An object behaves differently based on a finite number of states.
- Used in place of case, switch or other conditional statements.
- Start with a base class and then provide it different funtinionality based on its internal state.
Strategy
- Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.