Ognjen Regoje bio photo

Ognjen Regoje
But you can call me Oggy


I make things that run on the web (mostly).
More ABOUT me and my PROJECTS.

me@ognjen.io LinkedIn

Examples of sofware design patterns

#technical

A running list of examples of design patterns listed in Design Patterns: Elements of Reusable Object-Oriented Software.

Periodically updated.

Creational

Create objects, rather than having to instantiate objects directly. This gives the program more flexibility in deciding which objects need to be created for a given case.

Abstract factory groups object factories that have a common theme

Builder constructs complex objects by separating construction and representation

  • JBuilder

Factory method creates objects without specifying the exact class to create

  • FactoryBot

Prototype creates objects by cloning an existing object

  • JavaScript uses prototypes

Singleton restricts object creation for a class to only one instance

Structural

Concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.

Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class

  • Database adapters (eg. mysql2 gem)

Bridge decouples an abstraction from its implementation so that the two can vary independently

  • ActiveRecord logging does this where
  • Also caching

Composite composes zero-or-more similar objects so that they can be manipulated as one object

  • As I understand it this is effectively any structure that can has_and_belongs_to_many of itself so for example the categorization in Supplybunny

Decorator dynamically adds/overrides behaviour in an existing method of an object

  • Concerns

Facade provides a simplified interface to a large body of code

  • Integrating with Lazada API where to get the full details of the product have to call get info and get details

Flyweight reduces the cost of creating and manipulating a large number of similar objects

  • Ruby symbols

Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity

  • Memoization

def price @price ||= get_expensive_price end

Behavioral

Specifically concerned with communication between objects.

Chain of responsibility delegates commands to a chain of processing objects

Command creates objects which encapsulate actions and parameters

  • Sidekiq and DelayedJob (when you just pass a worker in)

Interpreter implements a specialized language

  • All the domain specific languages, for example squeel

Iterator accesses the elements of an object sequentially without exposing its underlying representation

  • Ruby Enumerator class

Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods

  • Vue actions

Memento provides the ability to restore an object to its previous state (undo)

  • Marshalling, Marshal.dump and Marshal.load in Ruby
  • .delay in Sidekiq

Observer is a publish/subscribe pattern which allows a number of observer objects to see an event

  • Rails observers

State allows an object to alter its behavior when its internal state changes

  • Acts as a state machine

Strategy allows one of a family of algorithms to be selected on-the-fly at runtime

  • Devise authentication strategies (eg. LDAP, OAuth, Basic)

Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior

  • to_json

Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object