Quick Guide to Structural and Creational Design Patterns
Tags: creational, design, java, designpatterns
Published: 2022-08-10 14:35:00

This post is in continuation to [Quick Guide to Behavioral Design Patterns](https://dev.to/jainnehaa/quick-guide-to-behavioral-design-patterns-32p0)

<u>USE-CASES Structural Patterns :</u>

<u>Adapter</u> 'adapts' one interface for a class into one that a client expects, both being incompatible.
**_Client -> Target Interface -> Adapter Class (for compatibility) -> Adoptee Interfaces_**

<u>Composite </u>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

<u>Decorator </u>**_add responsibilities to objects_** dynamically, without changing its interface.
(Vocab Hint : 'Decorate' means 'Wrapping a gift, putting it in a box')

<u>Facade </u>defines a new single **_unified interface to_** client to interact with **_a complex subsystem_** for existing objects

<u>Proxy </u>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.

<u>Bridge </u>**_decouples an abstraction/interface_** from its implementation so that the two can vary independently

<u>Private Class Data</u> 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.

<u>Flyweight </u>a fine-grained **_instance with common properties used for efficient sharing_**. Flyweight explains when and how State objects can be shared.

<u>USE-CASES Creational Patterns :</u>

<u>Singleton </u>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, <u>Factory Method</u> 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.
<u>Prototype </u>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.

<u>Abstract Factory</u> 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.

<u>Builder </u>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).

<u>References :</u>

[sourcemaking](https://sourcemaking.com/design_patterns)
[refactoring.guru](https://refactoring.guru/design-patterns)
[w3sdesign](http://w3sdesign.com/index0100.php)
[wiki.c2](https://wiki.c2.com/?GangOfFour)
[wikipedia](https://en.wikipedia.org/wiki/Design_Patterns)
[wikipedia](https://en.wikipedia.org/wiki/Behavioral_pattern)
[wikipedia](https://en.wikipedia.org/wiki/Structural_pattern)
[wikipedia](https://en.wikipedia.org/wiki/Software_design_pattern#Creational_patterns
)
[wikipedia](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)