🟣 BEHAVIORAL PATTERNS
(How objects interact, communicate, and change behavior)
Behavioral patterns focus on:
-
Communication between objects
-
Delegation of responsibilities
-
Runtime behavior changes
They help reduce tight coupling and make workflows extensible.
🔟 Observer Pattern
What
Observer defines a one-to-many dependency between objects.
When the Subject changes state, all Observers are notified automatically.
Why
Without Observer:
-
Subject must directly call every dependent class
-
Tight coupling between publisher and subscribers
-
Adding a new listener requires modifying subject code
Observer promotes event-driven design.
When
Use Observer when:
-
Multiple objects depend on one object’s state
-
You want publish-subscribe behavior
Real-world examples:
-
UI event listeners
-
Stock price notifications
-
Kafka consumers
Full Java Program (with Client)
1️⃣1️⃣ Strategy Pattern
What
Strategy defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime.
Why
Without Strategy:
-
Large
if-elseorswitchblocks -
Adding a new algorithm requires modifying existing logic
-
Violates Open–Closed Principle
When
Use Strategy when:
-
Multiple ways exist to perform an operation
-
Algorithm must change at runtime
Real-world examples:
-
Payment methods
-
Compression algorithms
-
Sorting strategies
Full Java Program (with Client)
1️⃣2️⃣ Command Pattern
What
Command encapsulates a request as an object, allowing it to be queued, logged, or undone.
Why
Without Command:
-
Sender tightly coupled to receiver
-
No easy undo/redo
-
Hard to queue or schedule operations
When
Use Command when:
-
You need undo/redo functionality
-
UI actions trigger business logic
-
Operations must be queued or logged
Real-world examples:
-
Button clicks
-
Job queues
-
Transaction commands
Full Java Program (with Client)
1️⃣3️⃣ State Pattern
What
State allows an object to change its behavior when its internal state changes, without if-else logic.
Why
Without State:
-
Large conditional blocks
-
Hard to add new states
-
Violates Open–Closed Principle
When
Use State when:
-
Object behavior depends on state
-
State transitions are well defined
Real-world examples:
-
Order lifecycle
-
ATM states
-
Traffic lights
Full Java Program (with Client)
1️⃣4️⃣ Chain of Responsibility Pattern
What
Chain of Responsibility passes a request through a chain of handlers until one handles it.
Why
Avoids:
-
Large
if-elsechains -
Tight coupling between sender and receiver
When
Use Chain when:
-
Multiple handlers may process a request
-
Order of processing matters
Real-world examples:
-
Authentication filters
-
Logging pipelines
-
Validation chains
Full Java Program (with Client)
1️⃣5️⃣ Mediator Pattern
What
Mediator centralizes communication between objects so they don’t talk to each other directly.
Why
Without Mediator:
-
Objects become tightly coupled
-
Changes ripple across classes
When
Use Mediator when:
-
Many objects interact
-
Communication logic is complex
Real-world examples:
-
Chat rooms
-
UI component interaction
-
Air traffic control
Full Java Program (with Client)
1️⃣6️⃣ Memento Pattern
What
Memento captures and stores an object’s internal state so it can be restored later.
Why
Direct access to internal state breaks encapsulation.
When
Use Memento when:
-
Undo functionality is required
-
State rollback is needed
Real-world examples:
-
Text editors
-
Game save points
Full Java Program (with Client)
1️⃣7️⃣ Iterator Pattern
What
Iterator provides a way to traverse a collection without exposing its internal structure.
Why
-
Prevents exposing internals
-
Provides uniform traversal
When
Use when:
-
Traversing collections
-
Uniform access required
Full Java Program (with Client)
1️⃣8️⃣ Interpreter Pattern
What
Defines a grammar and interprets expressions of that grammar.
Why
Simplifies expression evaluation logic.
When
Use when:
-
Simple DSL required
-
Rule engines or expression evaluators
Full Java Program (with Client)
1️⃣9️⃣ Visitor Pattern
What
Visitor allows adding new operations to object structures without modifying them.
Why
Prevents bloated domain classes.
When
Use when:
-
Object structure is stable
-
Operations change frequently
Full Java Program (with Client)
No comments:
Post a Comment