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.
- Host: GitHub
- URL: https://github.com/avinashkr91/gof-design-patterns
- Owner: avinashkr91
- Created: 2024-11-21T11:51:09.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-01-01T14:26:43.000Z (5 months ago)
- Last Synced: 2026-03-07T23:40:49.742Z (3 months ago)
- Topics: design-patterns, gang-of-four, gang-of-four-design-patterns
- Language: Java
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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)