Professional Documents
Culture Documents
Behavioral Patterns
Behavioral Patterns
Behavioral Patterns
BEHAVIORAL PATTERNS
Command Pattern
Chain of Responsibility
Command pattern
invoker
Interface Order acting as a command. Stock class
acts as a request. We have concrete classes BuyStock
and SellStock implementing Order interface which will
do actual command processing. A class Broker is
created which acts as an invoker object. It can take and
place orders.
Broker object uses command pattern to identify
which object will execute which command based on
the type of command. CommandPatternDemo, will use
Broker class to demonstrate command pattern.
Advantages and disadvantages of Command patterns:
advantages:
• It decouples the classes that invoke the operation from the object that
knows how to execute the operation.
• It allows you to create a sequence of commands by providing a queue
system.
• Extensions to add a new command is easy and can be done without
changing the existing code.
• You can also define a rollback system with the Command pattern.
disadvantages :
• To reduce the coupling degree. Decoupling it will request the sender and
receiver.
• Simplified object. The object does not need to know the chain structure.
• Enhance flexibility of object assigned duties. By changing the members within
the chain or change their order, allow dynamic adding or deleting
responsibility.
• Increase the request processing new class of very convenient.