An open API service indexing awesome lists of open source software.

https://github.com/avinashkr91/gof-design-patterns

This repo discusses important Gang of Four (GOF) design patterns with simple java real time use cases.
https://github.com/avinashkr91/gof-design-patterns

design-patterns gang-of-four gang-of-four-design-patterns

Last synced: 28 days ago
JSON representation

This repo discusses important Gang of Four (GOF) design patterns with simple java real time use cases.

Awesome Lists containing this project

README

          

# Creational Design Patterns

Creational design patterns are essential for controlling the instantiation process, ensuring that objects are created in
a way that is consistent with the application's needs.

## 1. Singleton Pattern:

The Singleton Pattern restricts a class to a single instance and provides a global access point to that instance.
It’s widely used when only one instance of a class is needed to coordinate actions across the system.

**Use Cases:**

* Logging classes.
* Database connection pools.
* Configuration classes where a single instance manages settings.

**When to Use:**

* When you need to control access to a shared resource, like database connections.
* When initializing objects is resource-intensive, and you want a single, shared instance.

[**πŸ”— Example code πŸ‘†**](src/creational/singleton)

## 2. Factory Method Pattern

The Factory Method Pattern defines an interface for creating an object but allows subclasses to alter the type of object
that will be created. This is ideal for cases where the instantiation logic is complex or depends on the input
parameters.

**Use Cases:**

* When the exact type of the object cannot be determined until runtime.

* Creating objects with a common interface but differing properties.

**When to Use:**

* When there is a need to centralize object creation logic.
* To simplify code maintenance by encapsulating the instantiation of complex objects.

[**πŸ”— Example code πŸ‘†**](src/creational/factory)

## 3. Abstract Factory Pattern

The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their
concrete classes. It’s beneficial when creating product families that should be used together.

**Use Cases:**

* When you want to enforce the use of related objects that share a common theme.

* Creating UI components for multiple platforms, where each platform has specific characteristics.

**When to Use:**

* When you need to provide a suite of related objects that must be used together.

* When enforcing consistency across related components is crucial.

[**πŸ”— Example code πŸ‘†**](src/creational/abstractfactory)

## 4. Builder Pattern

The Builder Pattern is used to create complex objects step by step. It separates object construction from its
representation, allowing the same construction process to create different representations.

**Use Cases:**

* When an object requires many fields or has optional parameters.

* To construct immutable objects with controlled steps.

**When to Use:**

* When constructing an object with a large number of parameters or optional parameters.

* When you want to create immutable objects with controlled instantiation steps.

[**πŸ”— Example code πŸ‘†**](src/creational/abstractfactory)

## 5. Prototype Pattern

The Prototype Pattern is used to clone existing objects instead of creating new ones. This pattern is often used when
the cost of creating a new object is expensive.

**Use Cases:**

* Cloning complex objects that may take time to initialize.
* Creating copies of objects with slight modifications.

**When to Use:**

* When creating a new instance is costly or resource-intensive.
* When you want to keep object properties but modify a few values for a new instance.

[**πŸ”— Example code πŸ‘†**](src/creational/builder)