0% found this document useful (0 votes)
24 views6 pages

Java_Design_Pattern_1721933899

Java design pattern

Uploaded by

vinod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Java_Design_Pattern_1721933899

Java design pattern

Uploaded by

vinod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Creational Design Patterns

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.

- Examples: Logger, Configuration classes.

2. Factory Method Pattern

- 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.

- Examples: Document creation, Parser for different formats.

3. abStract Factory Pattern

- 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.

- Examples: GUI toolkits that support multiple look-and-feel standards.

4. builder Pattern

- Scenario: Use when you need to construct a complex object step by step and the
construction process should allow different representations.

- Implementation: Separate the construction of a complex object from its


representation.

- Examples: StringBuilder, DocumentBuilder in XML processing, Lombok library builder.


5. PrototyPe Pattern

- 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.

- Implementation: Implement a clone method in the existing object.

- Examples: Object cloning, Registry of objects.

Structural Design Patterns

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.

- Examples: Wrapping legacy code, Adapter for different database drivers.

2. bridge Pattern

- Scenario: Use when you need to decouple an abstraction from its implementation so
that the two can vary independently.

- Implementation: Separate the abstraction and implementation in different class


hierarchies and use composition to link them.

- Examples: GUI framework that supports multiple backends.

3. coMPoSite Pattern

- Scenario: Use when you need to treat individual objects and compositions of objects
uniformly.

- Implementation: Create a component interface with a common method, and


implement this interface in both the leaf and composite classes.

- Examples: File system hierarchy, GUI components.


4. decorator Pattern

- 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.

- Examples: Buffered streams in Java IO, Adding functionalities to a window in a GUI.

5. Facade Pattern

- Scenario: Use when you need to provide a simplified interface to a complex


subsystem.

- Implementation: Create a facade class that provides a simplified interface to the


subsystem.

- Examples: JDBC, Simple HTTP API for complex service.

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.

- Examples: String pooling, Caching of immutable objects.

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.

- Examples: Remote proxies, Virtual proxies, Protection proxies.


Behavioral Design Patterns

1. chain oF reSPonSibility Pattern

- Scenario: Use when you need to pass a request along a chain of handlers.

- Implementation: Define a handler interface and link handlers together.

- Examples: Logging systems, Event handling systems, ATM design.

2. coMMand Pattern

- Scenario: Use when you need to parameterize methods with different requests,
queue requests, or support undoable operations.

- Implementation: Create a command interface and implement command classes for


each action.

- Examples: GUI button actions, Transaction management.

3. interPreter Pattern

- Scenario: Use when you need to interpret expressions in a language.

- Implementation: Define a grammar for the language and create an interpreter for the
grammar.

- Examples: Parsing and interpreting mathematical expressions, Regular expressions.

4. iterator Pattern

- Scenario: Use when you need to access the elements of a collection sequentially
without exposing its underlying representation.

- Implementation: Implement an iterator class that encapsulates the iteration logic.

- Examples: Collection iterators in Java, Tree traversal algorithms.


5. Mediator Pattern

- 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.

- Examples: Chat application, Flight control systems.

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.

- Examples: Undo mechanisms, State history.

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.

- Examples: Event listeners, MVC pattern, Weather Update app

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.

- Examples: State machines, Workflow systems, Vending Machine.


9. Strategy Pattern

- Scenario: Use when you need to define a family of algorithms and make them
interchangeable.

- Implementation: Create a strategy interface and implement concrete strategy classes.

- Examples: Sorting algorithms, Compression algorithms.

10. teMPlate Method Pattern

- Scenario: Use when you need to define the skeleton of an algorithm in a method,
deferring some steps to subclasses.

- Implementation: Define an abstract class with a template method and abstract


methods for the steps.

- Examples: Game loops, Report generation.

11. viSitor Pattern

- Scenario: Use when you need to perform operations on elements of an object


structure without changing the classes of the elements.

- 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.

You might also like