Java_Design_Pattern_1721933899
Java_Design_Pattern_1721933899
1. Singleton Pattern
- Scenario: Use when you need to ensure only one instance of a class is created and
provide a global point of access to it.
- Implementation: Use a private constructor, a static method that returns the instance,
and a static variable that holds the single instance.
- Scenario: Use when you have a superclass with multiple sub-classes and you need to
return one of the sub-classes based on some input.
- Implementation: Define an interface or abstract class for creating an object, but let
the subclasses decide which class to instantiate.
- Scenario: Use when you need to create families of related or dependent objects
without specifying their concrete classes.
- Implementation: Create interfaces for each kind of object that can be created, and
then create a factory class for each family of objects.
4. builder Pattern
- Scenario: Use when you need to construct a complex object step by step and the
construction process should allow different representations.
- Scenario: Use when you need to create new objects by copying an existing object,
typically to avoid the overhead of creating an object from scratch.
1. adaPter Pattern
- Scenario: Use when you need to convert an interface of a class into another interface
that a client expects.
- Implementation: Create an adapter class that implements the target interface and
uses composition to "wrap" the adaptee.
2. bridge Pattern
- Scenario: Use when you need to decouple an abstraction from its implementation so
that the two can vary independently.
3. coMPoSite Pattern
- Scenario: Use when you need to treat individual objects and compositions of objects
uniformly.
- Scenario: Use when you need to add responsibilities to individual objects dynamically
and transparently.
- Implementation: Create a set of decorator classes that are used to wrap concrete
components.
5. Facade Pattern
6. Flyweight Pattern
- Scenario: Use when you need to minimize memory usage by sharing as much data as
possible with other similar objects.
- Implementation: Store common data externally and pass it to the objects when
needed.
7. Proxy Pattern
- Scenario: Use when you need to provide a placeholder or surrogate to control access
to an object.
- Implementation: Create a proxy class that implements the same interface as the real
object and controls access to it.
- Scenario: Use when you need to pass a request along a chain of handlers.
2. coMMand Pattern
- Scenario: Use when you need to parameterize methods with different requests,
queue requests, or support undoable operations.
3. interPreter Pattern
- Implementation: Define a grammar for the language and create an interpreter for the
grammar.
4. iterator Pattern
- Scenario: Use when you need to access the elements of a collection sequentially
without exposing its underlying representation.
- Scenario: Use when you need to reduce the communication complexity between
multiple objects.
- Implementation: Create a mediator class that handles all communication between the
objects.
6. MeMento Pattern
- Scenario: Use when you need to capture and restore an object's internal state.
- Implementation: Create a memento class that stores the state of the originator
object.
7. obServer Pattern
- Scenario: Use when you need to notify multiple objects about changes to a single
object.
- Implementation: Create a subject class that maintains a list of observers and notifies
them of changes.
8. State Pattern
- Scenario: Use when an object's behavior depends on its state, and it must change its
behavior at runtime based on that state.
- Implementation: Define a state interface and implement state classes for each
possible state.
- Scenario: Use when you need to define a family of algorithms and make them
interchangeable.
- Scenario: Use when you need to define the skeleton of an algorithm in a method,
deferring some steps to subclasses.
- Implementation: Create a visitor interface with visit methods for each type of
element and implement concrete visitor classes.
- Examples: Object serialization, Abstract Syntax Tree (AST) traversal, Credit-Card and
Offer System.