Unformatted text preview:

Lecture 21: Design Patterns (Part 3)Kenneth M. AndersonObject-Oriented Analysis and DesignCSCI 6448 - Spring Semester, 20051March 29, 2005 © University of Colorado, Boulder, 20052Credit where Credit is DueSome of the material for this lecture is taken from “Head First Design Patterns” by Eric and Elisabeth Freeman; as such some of this material is copyright © O’Reilly, 2004March 29, 2005 © University of Colorado, Boulder, 20053Goals for this (short) lectureCover three more useful design patternsCommandFacadeProxyThis will bring the number of design patterns covered in this class to at least 15Twelve from lectures 13, 17, and 21 plus Double Dispatch, Blackboard, and Model-View-Controller.March 29, 2005 © University of Colorado, Boulder, 2005CommandThe Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operationsConsider the operation of a restaurantYou, the Customer, give your Waitress an OrderThe Waitress takes the Order to the kitchen and says “Order Up”The Cook prepares your meal from the OrderThink of the order as making calls on the Cook like “makeBurger()”A request is given to one object but implemented by another oneThis decouples the object making the request from the object that responds to the request 4March 29, 2005 © University of Colorado, Boulder, 2005Command’s Structure and Roles5ClientReceiveraction()InvokersetCommand()Commandexecute()undo()ConcreteCommandexecute()undo()public void execute() { receiver.action()}March 29, 2005 © University of Colorado, Boulder, 2005Back to the analogy...6ClientReceiveraction()InvokersetCommand()Commandexecute()undo()ConcreteCommandexecute()undo()public void execute() { receiver.action()}WaitressCookOrderCustomerMarch 29, 2005 © University of Colorado, Boulder, 2005Example: Home Remote Control7Imagine a programmable remote control that can control various devices around your homee.g. lights, TV, DVD player, etc.We’ll show code that has commands to turn a light on and off and an undo button to reverse the previously executed commandFirst, we need a light class; plays the role of Receiverpublic class Light {public Light(String name) { ... }public void on() { ... }public void off() { ... }}March 29, 2005 © University of Colorado, Boulder, 2005Command Interface; LightOnNext, we need the Command interfacepublic interface Command {public void execute();public void undo();}And a Command to turn the Light onpublic class LightOnCommand implements Command {Light light;public LightOnCommand(Light light) {this.light = light;}public void execute() {light.on();}public void undo() {light.off;}}8March 29, 2005 © University of Colorado, Boulder, 2005LightOffCommandAnd, a command to turn the light offpublic class LightOffCommand implements Command {Light light;public LightOffCommand(Light light) {this.light = light;}public void execute() {light.off();}public void undo() {light.on();}}9March 29, 2005 © University of Colorado, Boulder, 2005Remote ControlThe remote control stores three commands; acts as Invokerpublic class RemoteControl {Command onCommand;Command offCommand;Command undoCommand;public void setOnCommand(Command c) {onCommand = c;}public void setOffCommand(Command c) {offCommand = c;}public void on() { onCommand.execute(); undoCommand = onCommand;}public void off() { offComand.execute(); undoCommand = offCommand;}public void undo() {undoCommand.undo();}}10March 29, 2005 © University of Colorado, Boulder, 2005ClientThe client configures the remote control and then uses itpublic static void main(...) {RemoteControl rc = new RemoteControl();Light kitchenLight = new Light(“Kitchen”);LightOnCommand on = new LightOnCommand(kitchenLight);LightOffCommand off = new LightOffCommand(kitchenLight);rc.setOnCommand(on);rc.setOffCommand(off);rc.on(); -- Light Onrc.undo(); -- Light Off}11March 29, 2005 © University of Colorado, Boulder, 2005FacadeThe Facade Pattern provides a unified interface to a set of interfaces in a subsystem. The Facade defines a higher level interface that makes the subsystem easier to usePrinciple of Least KnowledgeTalk only to your immediate friendsor, for any one object, try to limit its knowledge of other objectsThe principle recommends the followinggiven an object, code in one of its methods can invoke methods onthe object itselfObjects passed as a parameter to the methodAny object the method createsAny components of the object (HAS-A relationships)12March 29, 2005 © University of Colorado, Boulder, 2005Facade’s Structure and Roles13ClientFacaderequest()subsystem classesClients ask the Facade to perform a service; the Facade responds by making appropriate calls on the objects contained within the subsystem; the Client has no direct access or knowledge of the classes contained within the subsystemMarch 29, 2005 © University of Colorado, Boulder, 2005Example: Home Theater System14Imagine a home theater system represented as a bunch of objectsYou might have objects likeAmplifier, tuner, DVDPlayer, Projector, CDPlayer, TheaterLights, Screen, and PopcornPopperTo watch a DVD, you might have to:Turn the popcorn popper onStart making popcornDim the lightsPut the screen downTurn the projector onSet the projector input to DVD...March 29, 2005 © University of Colorado, Boulder, 2005Watching a DVD via CodeIn code, this cooresponds to manipulating a lot of different objectspopper.on();popper.pop();lights.dim(10);screen.down();projector.on();projector.setInput(dvd);...15Plus, if you want to watch TV, you may need a way to undo these settings and then configure your system for TV viewingMarch 29, 2005 © University of Colorado, Boulder, 2005Facade to the RescueLets create an object to simplify our interactions with the Home Theatre “sub system”For instance:We would now only call these methods and not interact directly with the individual components16HomeTheaterFacadewatchMovie()endMovie()watchTV()endTV()playCD()endCD()March 29, 2005 © University of Colorado, Boulder, 2005Implement watchMovie()watchMovie() would look something like thispublic void watchMovie(...) {popper.on(); popper.pop();lights.dim(10); screen.down();projector.on(); ...}while endMovie() would look something like thispublic void endMovie() {popper.off; lights.on();screen.up(); projector.off();...}17March 29, 2005 © University of Colorado, Boulder, 2005ProxyThe Proxy Pattern provides a surrogate or


View Full Document

CU-Boulder CSCI 6448 - Design Patterns

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?