0% found this document useful (0 votes)
3 views16 pages

Lecture 8 - Behavioral Design Pattern-Command Pattern

The Command Pattern is a behavioral design pattern that encapsulates a request as an object, allowing for delayed execution, queuing, and undoable operations. It separates concerns by dividing the application into GUI and Business Logic layers, where GUI objects delegate tasks to the Business Logic layer through command objects. This pattern enables flexibility in handling requests and maintains a clean architecture by avoiding direct dependencies between GUI and business logic.

Uploaded by

m09221011
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Lecture 8 - Behavioral Design Pattern-Command Pattern

The Command Pattern is a behavioral design pattern that encapsulates a request as an object, allowing for delayed execution, queuing, and undoable operations. It separates concerns by dividing the application into GUI and Business Logic layers, where GUI objects delegate tasks to the Business Logic layer through command objects. This pattern enables flexibility in handling requests and maintains a clean architecture by avoiding direct dependencies between GUI and business logic.

Uploaded by

m09221011
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Behavioral design pattern

Command Pattern
‫عبد المطلب العبور‬
aelabour@[Link]
Command Pattern
• Command is a behavioral design pattern that turns a request into a stand-alone
object that contains all information about the request. This transformation lets you
pass requests as a method arguments, delay or queue a request’s execution, and
support undoable operations.

2
‫عبد المطلب العبور‬
Command Pattern
Problem
1. Imagine that you’re working on a new text-editor app. Your current task is to create a
toolbar with a bunch of buttons for various operations of the editor. You created a
very neat Button class that can be used for buttons on the toolbar, as well as for
generic buttons in various dialogs.

3
‫عبد المطلب العبور‬
Command Pattern
Problem
2. While all of these buttons look similar, they’re all supposed to do different things.
Where would you put the code for the various click handlers of these buttons? The
simplest solution is to create tons of subclasses for each place where the button is
used. These subclasses would contain the code that would have to be executed on a
button click.

4
‫عبد المطلب العبور‬
Command Pattern
Problem
3. Before long, you realize that this approach is deeply flawed. First, you have an
enormous number of subclasses, and that would be okay if you weren’t risking
breaking the code in these subclasses each time you modify the base Button class.
Put simply, your GUI code has become awkwardly dependent on the volatile code of
the business logic.

5
‫عبد المطلب العبور‬
Command Pattern
Solution
• Good software is built using separation of concerns, dividing the application into layers.
• A common structure includes:
❑ GUI Layer – Handles display, user input, and showing results.
❑ Business Logic Layer – Handles processing, calculations, and core operations.
• The GUI doesn’t perform complex tasks itself (e.g., calculations or report generation).
• Instead, it delegates work to the business logic layer.
• In code, this looks like a GUI object calling a method of a business logic object with some arguments
— essentially, sending a request.

6
‫عبد المطلب العبور‬
Command Pattern
The Command pattern suggests that GUI objects shouldn’t send these requests directly.
Instead, you should extract all of the request details, such as the object being called, the
name of the method and the list of arguments into a separate command class with a
single method that triggers this request.

7
‫عبد المطلب العبور‬
Command Pattern
The Command pattern suggests that GUI objects shouldn’t send these requests directly.
Instead, you should extract all of the request details, such as the object being called, the
name of the method and the list of arguments into a separate command class with a
single method that triggers this request.

8
‫عبد المطلب العبور‬
Command Pattern
• All commands should implement a common
interface, usually with a single, no-parameter
Execute() method.

• This allows the same sender (invoker) to work with


different commands without depending on their
concrete classes.

• You can swap command objects at runtime,


changing the sender’s behavior easily.

• Problem: Commands need request parameters, but


Execute() has no arguments.

Solution: Commands must be pre-configured with the


required data or fetch it themselves before execution

9
‫عبد المطلب العبور‬
"Multiple documents share the same toolbar and menus. The same buttons on the toolbar and the
same menu items work across all documents."
10
‫عبد المطلب العبور‬
Command Pattern
In this example, the
Command pattern helps to
track the history of executed
operations and makes it
possible to revert an
operation if needed.

11
‫عبد المطلب العبور‬
‫‪12‬‬
‫عبد المطلب العبور‬
‫‪13‬‬
‫عبد المطلب العبور‬
‫‪14‬‬
‫عبد المطلب العبور‬
‫‪15‬‬
‫عبد المطلب العبور‬
‫‪16‬‬
‫عبد المطلب العبور‬

You might also like