BCS1430
Dr. Ashish Sai
📅 Week 5 Lecture 2
💻 BCS1430.ashish.nl
📍 EPD150 MSM Conference Hall
Behavioral patterns in software design focus on effective communication and the assignment of responsibilities among objects.
| Pattern | Description | Covered |
|---|---|---|
| Observer | Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. | ✅ |
| Strategy | Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. | ✅ |
| Command | Encapsulates a request as an object, thereby allowing for parameterization of clients with queues, requests, and operations. | ❌ |
| State | Allows an object to alter its behavior when its internal state changes. The object will appear to change its class. | ❌ |
| Chain of Responsibility | Passes the request along the chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain. | ❌ |
| Interpreter | Provides a way to evaluate language grammar or expressions. The Interpreter pattern defines a grammar for the language, as well as an interpreter that uses the grammar to interpret sentences in the language. | ❌ |
| Memento | Captures and externalizes an object’s internal state so the object can be restored to this state later. | ❌ |
| Visitor | Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. | ❌ |
| Template Method | Defines the skeleton of an algorithm in the method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing its structure. | ❌ |
Composition over inheritance!
Strategy Pattern is defined as:
quack are shared.Can RubberDucks fly?
The intent of the Strategy pattern is to define a set of interchangeable algorithms or strategies that can be selected at runtime according to the needs of the context or client.
Problem: Need for a flexible way to incorporate different behaviors or algorithms within a class and the ability to change them at runtime.
Solution: The Strategy pattern suggests separating the behavior into different strategy classes and using a reference to these strategies in the context class.
fly method to Duck class leads to issues.fly and quack behaviors into different strategies.Different types of ducks inherit from Duck class.
Each subclass implements its own display method.
Promotes flexible code structure.
Allows behaviors to change dynamically.
Reduces dependency on inheritance.
Behaviors are not hard-coded in the Duck class.
They can vary independently from the duck type.
Context: Maintains a reference to a Strategy object and delegates it the algorithm execution.
Strategy: Common interface for all strategies defining the algorithm execution method.
ConcreteStrategy: Implements the algorithm using the Strategy interface.
// Strategy Interface
interface Strategy {
void executeAlgorithm();
}
// Context Class
class Context {
private Strategy strategy;
Context(Strategy strategy) {
this.strategy = strategy;
}
void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
void executeStrategy() {
strategy.executeAlgorithm();
}
}Use the Strategy pattern when:
You have different variations of an algorithm and want to switch between them at runtime.
You want to avoid exposing complex, algorithm-specific data structures.
You want to replace inheritance with composition for behavioral variations.
Pros:
Enables the Open/Closed Principle by allowing the introduction of new strategies without changing the context.
Simplifies unit testing by isolating algorithms.
Cons:
Increases the number of objects in the application.
Clients must be aware of the differences between strategies to select the right one.
Observer pattern allows for the establishment of a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.
Problem: Managing knowledge about changes in a system’s state can be complex when multiple entities need updates.
Solution: Observer pattern offers a subscription model where subjects notify observers about changes, promoting decoupling and efficient data distribution.
Push Model: Subject sends detailed data to observers.
Pull Model: Observers request data from the subject.