Design Patterns in a Nutshell

Coding

Design Patterns in a Nutshell

Design patterns provide a solution to familiar problems by offering an abstract blueprint that can be reused in various situations.

This article is a vague overview of design pattern techniques used in computer science and programming. I may want to write more in-depth articles for each design pattern with examples in the future.

Major categories

    • Creational. These design patterns control the object instantiation process.
    • Structural. These design patterns deal with the relationships among entities.
    • Behavioral. These design patterns are concerned with the communication patterns and responsibility of objects.
    • Concurrency. These design patterns deal with the multi-threaded programming paradigm.

Creational design patterns

  • Abstract Factory. Offers an interface for creating families of objects (related or dependent) without having to concern itself with the way each concrete class is built.
  • Builder. Simplifies the object instantiation process for complex objects, that normally take multiple steps to construct or are very complex to derive. Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.
  • Factory Method. Creates objects without exposing the instantiation logic to the client, by creating objects using a factory method rather than by a constructor.
  • Object Pool. Avoid expensive acquisition and release of resources by recycling objects that are no longer in use.
  • Prototype. Helps to save time and memory by having a blueprint from which multiple instances can be cloned or duplicated. It’s sort of the ‘lorem ipsum’ technique of the coding world, if we can put it such way.
  • Singleton. Restricts the the instantiation of a class to only one object. In order words, only a single instance of that class can exist. Think of the Highlander movies: “There can be only one!”. So, feel free to call this technique the Clan MacLeod Design Pattern.

Structural design patterns

  • Adapter. Helps two incompatible classes/interfaces to work together. The best analogy are real world adapters, like an adapter allowing to plug a Playstation gamepad into a Xbox, etc.
  • Bridge. Separate an interface from its implementation, to make it possible to modify and even replace the implementation without changing client code.
  • Composite. It’s a partitioning design pattern.  It’s used to work with hierarchies and networks where both the primitive and composite objects are treated alike and perform a task. The simplest example is Windows Explorer: Folders (composites) can themselves contain more folders and files, while files (primitives) are the leaf nodes with no more branches.
  • Decorator. Allows to attach additional behaviors and responsibilities to an object dynamically at run-time. This mechanism embellish an interface without changing its structure. Good analogy is a cook in a restaurant using a pizza dough (interface), which is decorated differently each time (run-time) a customer place a new order .
  • Facade. A single class that helps in simplifying an entire or multiple subsystems of various entities. It wraps everything with a simple interface, hiding all the complexity. It’s responsible for calling functions of existing subsystems.
  • Flyweight. Provides a useful way of sharing a large numbers of similar objects to dramatically reduce the memory footprint and increase the speed of the program.
  • Private Class Data. Restricts accessor/mutator access.
  • Proxy. Useful to represent another complex class, especially for firewall purpose that cannot be overstep. There are several reasons to use a Proxy instead of directly accessing another class, such as a login interface controlled by a username and password.

Behavioral design patterns

  • Chain of responsability. Helps to create more scalable and reusable code by defining concrete handlers and achieve lose coupling where a request from client is passed to a chain of objects to process them. A complex process is broken down into many individual steps, which each object send a command without knowing what object will receive and how it will handle it. Can help to avoid too many nested calls.
  • Command. Helps to create a framework that add multiple strands of execution to a program, increasing efficiency dramatically. Encapsulate a command request as an object. The command object may then be executed immediately, queued for later use or reused to support undo/redo actions. The main components involved are ‘Receiver’, ‘Command’ and ‘Invoker.’
  • Interpreter. A rarely used pattern. It’s used to convert one representation of data into another. Defines the grammar of a particular language in an object-oriented way which can be evaluated by the interpreter itself. It’s a heavy-duty pattern and gets messy if the coder jam all sorts of logic into the structure.
  • Iterator. Provide sequential access to all the entries in an aggregate structure (elements of a collection).
  • Mediator. Allow simplified interdependence between classes by introducing a plane in-between so that the interaction between objects happen via the plane.
  • Memento. Used to capture the state of an object and provides the mechanism to rollback the internal state of an object to some previous setting. Best analogy is undo operation or factory reset on a device.
  • Null Object. Designed to act as a default value of an object.
  • Observer. Defines a link between ‘observer’ object(s) and subject object(s). The observer must be notified of any state changes of the subject, usually by calling one of the subject methods.
  • State. Allows to change the behavior of a method through the state of an object, depending on the internal state when the object goes through different phases.
  • Strategy. Provides a family of algorithms that can be chosen on-the-fly, which are all interchangeable. Can help to reduce long lists of conditional statements and duplicate code. The drawback is that it increases the amount of classes/objects.
  • Template Method. Defer the exact steps of an algorithm to a subclass.
  • Visitor. Defines a new operation or a set of operations on an object or a set of objects without modifying the objects themselves.  The new logic is implemented in a separate object defined as visitor. Example, the visitor object is used to represent a user operation to be performed on elements of a graph structure, without modifying classes of these elements.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *