Command Pattern and Strategy Pattern
Command Pattern and Strategy Pattern
PATTERN AND
STRATEGY
PATTERN
COMMAND PATTERN
• Concrete Command: This class implements the Command interface and defines
the binding between the receiver and an action. It contains a reference to the
receiver object and implements the execute method by invoking the corresponding
operation(s) on the receiver.
• Receiver: The object that performs the actual action when the command's execute
method is called. The receiver knows how to perform the operations associated
with carrying out a request.
• Invoker: This class asks the command to carry out the request. It maintains a
reference to a command object and can call the command's execute method.
• Client: The client is responsible for creating and configuring concrete command
objects. It creates a concrete command object and sets its receiver.
USE CASE
GUI Button Actions: In a GUI, each button can be linked to a specific command.
• When you click a button, it triggers a command, making it easy to handle different actions (like saving,
opening, etc.) without cluttering the interface.
•Undo/Redo Functionality: In apps like text editors, each action is stored as a command.
•This way, you can easily "undo" or "redo" actions by executing the opposite commands, enabling easy reversal
and re-application of tasks.
•Transactional Systems: In systems that perform multiple actions in order, each action is wrapped as a
command.
•This ensures the system can handle operations in sequence and supports rolling back actions if something
goes wrong.
•Macro Recording: In automation tools, you can record actions as command objects.
• Later, you can replay these actions (macros) exactly as they were performed, saving time and effort for
repetitive tasks.
•Remote Control Systems: In a remote control, each button press is linked to a command.
• When a button is pressed, it triggers a specific action (like changing the channel or adjusting volume), making
it easy to manage multiple commands through one device.
Strategy Pattern:
The Strategy pattern is a behavioral design pattern that enables a
class's behavior or algorithm to be selected at runtime.
This pattern defines a family of algorithms, encapsulates each one,
and makes them interchangeable.
It allows the algorithm to vary independently from the clients that use
it, promoting flexibility and reusability.
The Strategy pattern is particularly useful when you need to
implement different variants of an algorithm.
KEY COMPONENTS:
Context: This is the class that contains a reference to a Strategy object. It delegates tasks to the
strategy object, ensuring the correct algorithm is executed.
Strategy Interface: Defines a common interface for all supported algorithms. This allows
different algorithms to be interchangeable within the context.
Concrete Strategy Classes: These classes implement the Strategy interface. Each class
represents a specific algorithm, providing a concrete implementation for the Strategy.
Client: The client sets a strategy in the context and calls its methods. The client can switch
strategies dynamically, depending on the required behavior.
USECASES:
Sorting Algorithms: The Strategy pattern is often used to implement different sorting
algorithms. This allows the selection of the most efficient algorithm based on the data set size
and type.
Payment Methods: In e-commerce systems, the Strategy pattern can handle various
payment methods. The system can switch between credit cards, PayPal, and other payment
methods at runtime.
Data Compression: When dealing with file compression, the Strategy pattern enables
switching between different compression algorithms. This can optimize the compression based
on file types and sizes.
Path finding Algorithms: In navigation systems, the Strategy pattern helps choose the best
pathfinding algorithm. This can be based on criteria like distance, traffic conditions, or time.
Logging Strategies: The Strategy pattern allows switching between different logging
strategies. This can include logging to files, databases, or external systems depending on the
environment or requirements.
Conclusion
Understanding the Strategy and Command patterns enhances
software design flexibility.
The Strategy pattern allows for dynamic algorithm selection,
while the Command pattern encapsulates requests as objects.
Both patterns decouple components, promoting cleaner and
more maintainable code.
Choosing the right pattern depends on your specific needs
and use cases. By mastering these patterns, developers can
create more robust and adaptable applications.
Implementing these patterns effectively can greatly improve
your software's design and functionality.
THANK YOU!!