Docsity

Command Pattern: Encapsulating Invocations for Home Automation, Lecture notes of Law

Partial preview of the text

Download Command Pattern: Encapsulating Invocations for Home Automation and more Lecture notes Law in PDF only on Docsity!

CMPSCI 220 Programming Methodology

08 : Command Pattern

Based on Head First Design Patterns

Objectives

Command Pattern

● Learn how objects can encapsulate invocation.

● Apply the command pattern in Scala.

● Learn how to evolve the command pattern to support undo.

The Vendor Classes

This should give you a good idea of what we need to control from the remote!

A Brief Introduction to the Command Pattern

Let us look at

this a little

closer…

The customer knows what he

wants and creates an order.

Let us look at

this a little

closer…

The order consists of an order

slip and the customer’s menu

items that are written on it.

Let us look at

this a little

closer…

The Order has all the

instructions necessary to

prepare the meal. The Order

directs the Short Order Cook

with methods like makeBurger()

Let us look at

this a little

closer…

The Short Order cook follows

the instructions of the Order and

produces the meal.

From Diner to Command Pattern

create Command object Client

The client is responsible for

creating the command object.

The command object consists of

a set of actions on a receiver.

From Diner to Command Pattern

create Command object Client execute Command createCommandObject()

The actions and the Receiver

are bound together in the

Command object

def execute: Unit { receiver.action1(); receiver.action2(); } action1() action2() …. Receiver

From Diner to Command Pattern

create Command object Client execute Command createCommandObject()

The client calls setCommand()

on an Invoker object and passes

it the command object, where it

gets stored until it is needed.

setCommand Invoker

From Diner to Command Pattern

create Command object Client execute Command createCommandObject()

At some point in the future the

Invoker calls the command

object’s execute() method…

setCommand Invoker

What Does

What?

What Does

What?