0% found this document useful (0 votes)
24 views

Command Pattern: Chihung Liao Cynthia Jiang

The command pattern encapsulates a request as an object, allowing it to be passed to other objects and executed at a later time. It decouples the invoker of the command from the receiver of the command, making it possible to add new commands without changing existing code. The pattern can be implemented in Java by defining a Command interface with an execute method, and ConcreteCommand classes that implement the interface and call methods on a Receiver object.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Command Pattern: Chihung Liao Cynthia Jiang

The command pattern encapsulates a request as an object, allowing it to be passed to other objects and executed at a later time. It decouples the invoker of the command from the receiver of the command, making it possible to add new commands without changing existing code. The pattern can be implemented in Java by defining a Command interface with an execute method, and ConcreteCommand classes that implement the interface and call methods on a Receiver object.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Command Pattern

Chihung Liao Cynthia Jiang

Waiter

Order Execute()

Hamburger Execute()

Hot Dogs Execute() Cook Make Food()

Fries Execute()

Abstract command (superclass)

Order Execute()

Hamburger Execute()

Hot Dog Execute()


Concrete command (subclass)

Fries Execute()

Client

Invoker

Command Execute()

Receiver Action()

ConcreteCommand Execute() State

Flexibility and Extensibility


Encapsulate a request as an object Compare to C function pointer Decouple the client and receiver by the invoker who ONLY invoke an operation Any request can be cancelled or added in form of stack Request to be executed can be stored as a queue

Translate into JAVA 1


Interface Command: an interface for executing an operation (it could also be an abstract class, refer to the sample codes comment) Class Client: creates a ConcreteCommand object and set its receiver Class Receiver: who actually DOES things

Translate into JAVA 2


Class Invoker: ask Command to carry out an execution, an agent between ConcreteCommand and Client Class ConcreteCommand implements Command: MUST have the method in Interface Command

QUESTIONS

You might also like