CS 210 Introduction to Design Patterns September 28 th, 2006.

15
CS 210 Introduction to Design Patterns September 28 th , 2006

Transcript of CS 210 Introduction to Design Patterns September 28 th, 2006.

Page 1: CS 210 Introduction to Design Patterns September 28 th, 2006.

CS 210

Introduction to Design Patterns

September 28th, 2006

Page 2: CS 210 Introduction to Design Patterns September 28 th, 2006.

Head First Design Patterns

Chapter 6

Command Pattern

Encapsulating Invocation

Page 3: CS 210 Introduction to Design Patterns September 28 th, 2006.

Motivating problem description

Build a remote that will control variety of home devices

Sample devices: lights, stereo, TV, ceiling light, thermostat, sprinkler, hot tub, garden light, ceiling fan, garage door

Page 4: CS 210 Introduction to Design Patterns September 28 th, 2006.

Introducing the command pattern

CreateCommandObject

execute()

setCommand

execute()action1action2

creatCommandObject()

setCommand()

execute()

action_X()

Page 5: CS 210 Introduction to Design Patterns September 28 th, 2006.

Command Pattern for home automation

action()

execute(){ receiver.action()}

execute()

execute()

execute()

An encapsulated Request

Invoker

Page 6: CS 210 Introduction to Design Patterns September 28 th, 2006.

Command Pattern defined

The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

Page 7: CS 210 Introduction to Design Patterns September 28 th, 2006.

Command Pattern Class Diagram

Client Invoker

setCommand()

<<Interface>>Command

execute()undo()

Receiver

action()

ConcreteCommand

execute()undo()

Page 8: CS 210 Introduction to Design Patterns September 28 th, 2006.

Command Pattern Class Diagram for Home automation

RemoteLoader

RemoteControl

onCommandsoffCommandssetCommand()onButtonPushed()offButtonPushed()

<<Interface>>Command

execute()undo()

Light

on()off()

LightOnCommand

execute()undo()

LightOffCommand

execute()undo()

Page 9: CS 210 Introduction to Design Patterns September 28 th, 2006.

Command pattern – Undo operation

Eclipse code review

Page 10: CS 210 Introduction to Design Patterns September 28 th, 2006.

Macro Commands – Party modepublic class MacroCommand implements Command {

Command[] commands; public MacroCommand(Command[] commands) { this.commands = commands;}

public void execute() {for (int i = 0; i < commands.length; i++) {

commands[i].execute();}

}

public void undo() {for (int i = 0; i < commands.length; i++) {

commands[i].undo();}

}}

Page 11: CS 210 Introduction to Design Patterns September 28 th, 2006.

Macro Command – Party mode

Eclipse code review

Page 12: CS 210 Introduction to Design Patterns September 28 th, 2006.

Page 228 – Head First Design Patterns

Page 13: CS 210 Introduction to Design Patterns September 28 th, 2006.
Page 14: CS 210 Introduction to Design Patterns September 28 th, 2006.

Summary so far.. OO Basics

• Abstraction• Encapsulation• Inheritance• Polymorphism

OO Principles• Encapsulate what varies• Favor composition over inheritance• Program to interfaces not to implementations• Strive for loosely coupled designs between objects that interact• Classes should be open for extension but closed for modification.• Depend on abstracts. Do not depend on concrete classes.

Page 15: CS 210 Introduction to Design Patterns September 28 th, 2006.

Summary so far… OO Patterns

• Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

• Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

• Decorator Pattern – attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality

• Abstractor Factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

• Factory Method – Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to the subclasses.

• Command Pattern – Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.