12
LECTURE 19: COMMAND PATTERN Computer Science 313 – Advanced Programming Topics

Lecture 19: Command Pattern

  • Upload
    kellsie

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Computer Science 313 – Advanced Programming Topics. Lecture 19: Command Pattern. Strategy Pattern. Define interface for family of algorithms Encapsulate algorithm in class of its own Algorithm instance held in field of Context class Delegate responsibility for action to this object - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 19: Command Pattern

LECTURE 19:COMMAND PATTERN

Computer Science 313 – Advanced Programming Topics

Page 2: Lecture 19: Command Pattern

Strategy Pattern

Define interface for family of algorithms Encapsulate algorithm in class of its own Algorithm instance held in field of Context class

Delegate responsibility for action to this object

Set field to change algorithm dynamically

Page 3: Lecture 19: Command Pattern

Limits To Strategy

Several limitations to strategy pattern Details of Strategy must be known by Context

Context must execute actual Strategy Strategy should not maintain state Cannot determine past Strategys

Page 4: Lecture 19: Command Pattern

Limits To Strategy

Several limitations to strategy pattern Details of Strategy must be known by Context

Context must execute actual Strategy Strategy should not maintain state Cannot determine past Strategys

Page 5: Lecture 19: Command Pattern

Why Is This A Problem?

Unable to log steps taken during program But a Strategy is not a tangible object

Cannot undo a Strategy once executed To enable “undo” would need log of

actions But Strategy lacks state to save actual

results Strategy independent of Context

Cannot use external objects when executed

Specific context cannot host a Strategy

Page 6: Lecture 19: Command Pattern

Command Pattern

Encapsulates single action as an object Each instance can be used one or more

times Define range of actions in abstract Command But pattern has one way to execute Command

Adjust the state of a Command using fields Modify how it executes and what it does Use to specify which instance Command

uses More than parameters to control

exposed method

Page 7: Lecture 19: Command Pattern

Command Pattern Intent

Encapsulate single request within an instance Client code can then pass requests as

parameter Pattern treats Command as normal

object Queue Commands to execute in certain

order Once executed, record Commands in log

Page 8: Lecture 19: Command Pattern

Command Pattern Usage

Commands support undo & redo options Additional method in Command for undo

support Use Command’s state to record what it did

Stack holds Commands once executed Actions then are un-done by popping Commands

Page 9: Lecture 19: Command Pattern

Command Pattern Intent

Split execution from performance Pattern creates one class executes Commands

Command can select actor who performs it

Page 10: Lecture 19: Command Pattern

Decoupling

Command pattern decouples (splits)What we want to do

Who does itWhen it is done

Page 11: Lecture 19: Command Pattern

Decoupling

Command pattern decouples (splits)What we want to do

Who does itWhen it is done Wish I

knew about the Command Pattern

Page 12: Lecture 19: Command Pattern

For Next Class

Lab #5 on Angel & due next Friday In the book, read pages 203 - 214

Start implementing the Command Pattern

What classes would we need? Who executes the command? And what the heck is a NOP (Null

Command)? Midterm #1 in class next Friday

Test is to be open-book, open-note, open-reports

Cannot use slides as part of your notes