Skip to content

Latest commit

 

History

History

patterns

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Design patterns

Table of Content

Creational Patterns

Builder

Construct complex objects step by step.

img.png

  • create different representations of the object by using the same construction code

  • the Builder doesn’t allow other objects to access the product while it’s being built

  • builder

    • extract the construction code
    • can execute a series of steps on a builder object
    • call only those steps that are necessary for producing a particular configuration of an object
    • can have several builder classes that implement the same steps but in a different way
    • builders need to provide their own methods for returning results as different products might be created from different builders (the products don't follow a common interface)
  • director

    • not mandatory
    • extract a series of calls to the builder steps
    • it defines the order in which to execute the building steps
    • provides reusability - can be used for grouping different construction routines
    • the director class completely hides the details of product construction from the client code
    • the client only needs to associate a builder with a director, launch the construction with the director, and get the result from the builder.
  • used when

    • the products are quite complex and require extensive configuration
    • want to create different representations of the products (same base product; for eg., stone and wooden houses)
    • the representations are based on similar steps with different details only
    • isolate complex product construction logic from business logic (Single Responsibility Principle)

Code here.

Factory Method

Hide the creation of the object and expose it under an interface.

img.png

  • the objects (aka products) are created inside a factory function

  • the creator, the one which defines the factory method, has other primary responsibility instead of creating products

  • it has some business logic related to the created products

  • the client uses an instance of a concrete creator but doesn't know which concrete product it uses

  • used when the exact types and dependencies are not known beforehand

  • the product creation is separated from its use

  • can be reusing existing products instead of creating new ones each time

  • used when

Code here.

Abstract Factory

Create families of objects

img.png

  • factory of factories
  • add new products or families of products easily
  • the products from factories are compatible with each other
  • loose coupling between concrete products and client code
  • the product creation is in one place only (Single Responsibility Principle)
  • makes it easy to add new products or variants (Open / Closed Principle)

Code here.

Structural Patterns

Behavioral Patterns

State

An object alters its behavior when its internal state changes.

img.png

  • there are a finite number of states which a program can be in

  • for each one the program behaves differently

  • can change from one to another easily by some predefined and finite rules (transitions)

  • any change to the transition logic may require changing state conditions in every method

  • create new types for each possible state and extract all state-specific behavior in them

  • transition by replacing the current active state with the new state

  • create new states or change existing one independently of each other

  • used when

Code here.

Strategy

For designing a family of interchangeable algorithms hidden behind an abstraction.

img.png

  • strategy

    • interface defining the work
    • one method that triggers the algorithm for the strategy implementation
  • context

    • contains a reference to the chosen strategy implementation
    • it delegates the work to a linked strategy
    • the client passes to the context the chosen strategy
    • the context is independent of the concrete strategy
  • used when

    • using variations of an algorithm
    • you can alter the context's behavior at runtime and associate a different strategy to it
    • respects Open / Closed Principle - introduce different strategies or policies without changing the context

Code here.

Example: AoC 2022, day 11 (the operation done by each monkey)

Links