DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

jainnehaa
jainnehaa

Posted on • Updated on

Quick Guide to Structural and Creational Design Patterns

This post is in continuation to Quick Guide to Behavioral Design Patterns

USE-CASES Structural Patterns :

Adapter 'adapts' one interface for a class into one that a client expects, both being incompatible.
Client -> Target Interface -> Adapter Class (for compatibility) -> Adoptee Interfaces

Composite has a tree structure of objects where every object has the same interface. It lets clients treat individual objects and compositions of objects uniformly.
1-to-many "has a" up the "is a" hierarchy

Decorator add responsibilities to objects dynamically, without changing its interface.
(Vocab Hint : 'Decorate' means 'Wrapping a gift, putting it in a box')

Facade defines a new single unified interface to client to interact with a complex subsystem for existing objects

Proxy creates an object representing another object. It uses an extra level of indirection as a wrapper or delegation to support distributed, controlled, or intelligent access.

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

Private Class Data pattern seeks to reduce exposure of attributes by limiting their visibility.
It restricts accessor or mutator access. It allows the class designer to remove write privilege of attributes that are intended to be set only during construction, even from methods of the target class.

Flyweight a fine-grained instance with common properties used for efficient sharing. Flyweight explains when and how State objects can be shared.

USE-CASES Creational Patterns :

Singleton lets you ensure that a class has only one instance, while providing a global access point to this instance.
Encapsulated "just-in-time initialization" or "initialization on first use".

There should be only one Singleton instance, whereas a Flyweight class can have multiple instances with different intrinsic states.
The Singleton object can be mutable. Flyweight objects are immutable.

To avoid tight coupling between the creator and the concrete products, Factory Method lets define an interface for creating an object but let subclasses decide which class to instantiate.
Factory Method is based on inheritance but doesn’t require an initialization step.
Single Responsibility Principle and Open/Closed Principle is achieved.

In order to get rid of repeated initialization code, Prototype lets you copy existing objects without making your code dependent on their classes.
Prototype requires an initialization of the cloned object, which is without coupling to their concrete classes.
This provides an alternative to inheritance when dealing with configuration presets for complex objects.

Abstract Factory defines a Factory Method per product.
It produces families of related objects without specifying their concrete classes. A class delegates object creation to a factory object instead of instantiating concrete classes directly.
Abstract Factory classes are often based on a set of Factory Methods, but you can also use Prototype to compose the methods on these classes.

Builder separates construction of a complex object from its representation, so that the same construction process can create different representations.
The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.

Abstract Factories, Builders and Prototypes can all be implemented as Singletons.
Many designs start by using Factory Method (less complicated and more customizable via subclasses) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, but more complicated).

References :

sourcemaking
refactoring.guru
w3sdesign
wiki.c2
wikipedia
wikipedia
wikipedia
wikipedia
wikipedia

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Good news! You can update to dark mode in your DEV settings.