Author image

Base Friend Design Pattern

A software design pattern I've seen multiple times in codebases, that I've decided to designate (Name & Conquer). It answers the following question:

We have a certain ResourceClass, that we wish to use its internals in various other UserClasses. How do we achieve interaction using the base friend pattern?

It is a means to efficiently share code between different components/classes without breaking (too much) the rule of encapsulation. Classifying it as a behavioral design pattern should do nicely.

Design

We can leverage the C++ friendship rules. We're aware that C++ friends break encapsulation, "friends invade our private life". We don't like that, we hate it. However.. remember that friendship is not inherited nor bidirectional. Given this fact, we could create a base class for all our UserClasses and that class alone can...

Author image

Thread Pool


A thread pool is a software design pattern that helps programmers achieve concurrency in an optimal way. By maintaining a pool of threads ready to execute at any moment performance is increased and latency is minimized by keeping a set of threads always resident in a pool; at the ready, instead of frequently starting them up and destroying them because of frequent brief tasks. Thus we avoid short lived threads which break concurrency speedup.

All this because creating and destroying threads takes a longer time than keeping them constantly alive and waiting.

Design

  • there is an array of threads - m_pool
  • there is a FIFO queue of tasks (a Task should be a wrapper for a Callable object) - m_tasks. Tasks are enqueue()ed into the task queue
  • all...

Author image

Observer Design Pattern


Observer is a behavioral software design pattern, that is being extensively utilized in almost every single self-respecting codebase as a means to efficiently communicate between objects in an event-based fashion. It wouldn't be an understatement to say it is the foundation of event based programming.

In fact, underlying the ubiquitous, in network programming, “Model View Controller” (MVC) design pattern, is the observer pattern.

Observers are used in every single game engine - they're known there mostly as “Event dispatchers”.

Alternative terminology for this pattern includes the following: - Publish-Subscribe, Listener-Reporter.

Design

  • a class, called the Subject, maintains a list of...

Author image

Command Design Pattern


Definition:
Encapsulate a request as an object, thereby letting users parameterize clients with different requests, queue or log requests, and support undoable operations.

Breakdown:
You are not encapsulating the object that sends the request, or the object that receives the request, but encapsulate the actual request as an object with state. Of course, some commands can be stateless chunks of pure behavior (no data members).
The command pattern also makes undoable/redoable operations a piece of cake (they're considerably harder to implement without it). Derived Commands can leverage CRTP to be statically bound.

Note that the code executing the commands is responsible for freeing their memory.

Design

  • define a base abstract class Command that represents a triggerable...

Author image

Curiously Recurring Template Pattern


Curiously Recurring Template Pattern, or CRTP for short, is essentially a C++ idiom. It is also known as Static Polymorphism or Simulated polymorphism.

Here's the differences between (actual) Dynamic polymorphism and Static polymorphism (CRTP):

Requirements for dynamic polymorphism:

  1. Is-a relationship between base and derived class.
  2. Base class defines a 'generic' algorithm that is used by derived class.
  3. The 'generic' algorithm is customized by the derived class.

Requirements for static polymorphism:

  1. 'Is-a' relationship between base and derived class.
  2. Base class defines a 'generic' algorithm that is used by derived class.
  3. The 'generic' algorithm is customized by the derived class.
  4. This method is pure virtual. The derived method implements it.

Benefits

  • CRTP is simulating dynamic...

1