Thursday, February 06, 2025

Creational Patterns in Java

Creational patterns in Java focus on simplifying and standardizing object creation while ensuring flexibility and minimizing dependencies.

1. Singleton

Ensures a class has only one instance, implemented using private constructors and synchronized methods for thread safety.

2. Factory Method

Defines a method for object creation, allowing subclasses to decide which class to instantiate dynamically.

3. Abstract Factory

Provides an interface for creating families of related objects without specifying their concrete implementations.

4. Builder

Separates the construction of complex objects from their representation, using fluent methods for flexibility and clarity.

5. Prototype

Creates new objects by cloning existing ones, leveraging Cloneable and overriding the clone() method for efficient duplication.

Structural Patterns in Java

Structural patterns emphasize organizing classes and objects for better composition, maintainability, and scalability in Java.

1. Adapter

Converts one interface into another expected by clients, using inheritance or delegation to bridge incompatibility.

2. Bridge

Decouples abstraction from implementation by combining interfaces and concrete implementations, enabling independent extension.

3. Composite

Groups objects into tree structures to represent part-whole hierarchies, leveraging recursion and polymorphism in Java.

4. Decorator

Dynamically adds behavior to objects without modifying their code, often using wrapping and interfaces.

5. Facade

Provides a unified interface to a complex subsystem, simplifying interaction by encapsulating multiple components into one.

6. Flyweight

Shares common state among multiple objects to reduce memory usage, using object pooling or caching mechanisms.

7. Proxy

Acts as a surrogate to control access to another object, often used for lazy initialization or access control.

Behavioral Patterns in Java

Behavioral patterns in Java manage communication and workflows between objects, enhancing flexibility and dynamic interactions.

1. Chain of Responsibility

Passes a request through a chain of handlers, each deciding whether to handle or forward it.

2. Command

Encapsulates requests as objects, allowing parameterization, queuing, and undo/redo functionality in Java applications.

3. Interpreter

Defines a grammar and interprets expressions, suitable for scripting engines, configuration parsers, or DSLs.

4. Iterator

Provides a standard way to traverse collections using Java’s Iterator or enhanced for-loops.

5. Mediator

Centralizes communication between objects, reducing dependencies with event-based systems or a mediator class.

6. Memento

Captures and restores an object’s state without exposing its details, commonly used for undo operations.

7. Observer

Implements a one-to-many relationship where dependents are notified of changes, often using Java’s Observer and Observable.

8. State

Allows an object to change its behavior dynamically based on its internal state, modeled using state classes.

9. Strategy

Encapsulates interchangeable algorithms into separate classes, promoting flexibility and reuse for tasks like sorting or validation.

10. Template Method

Defines the skeleton of an algorithm in a base class, deferring specific steps to subclasses.

11. Visitor

Encapsulates operations performed on elements of an object structure, enabling new functionality without modifying them.

No comments:

Abstract Factory Pattern in C#

Below is a C# implementation of the Abstract Factory design pattern, a Creational pattern, following the Gang of Four (GoF) structure....