+ informatics 122 software design ii lecture 9 emily navarro duplication of course material for any...

52
+ Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited. 1 Portions of the slides in this lecture are adapted from http://www.cs.colorado.edu/~kena/classe s/5448/f12/lectures/

Upload: lynette-davis

Post on 25-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+

Informatics 122Software Design IILecture 9Emily Navarro

Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited. 1

Portions of the slides in this lecture are adapted from http://www.cs.colorado.edu/~kena/classes/5448/f12/l

ectures/

Page 2: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Today’s Lecture

Design patterns – part 3 of a 3-part series Three patterns:

Template Method Singleton Facade

Assignment 3, part 1

2

Page 3: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Template Method: Definition

The Template Method pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure Template Method defines the steps of an algorithm and allows

subclasses to provide the implementation for one or more steps Makes the algorithm abstract

Each step of the algorithm is represented by a method Encapsulates the details of most steps

Steps (methods) handled by subclasses are declared abstract

Shared steps (concrete methods) are placed in the same class that has the template method, allowing for code re-use among the various subclasses

3

Page 4: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Template Method: Structure

4

Page 5: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Example: Tea and Coffee

Consider an example in which we have recipes for making tea and coffee at a coffee shop

5

Coffee Boil water Brew coffee in boiling water Pour coffee in cup Add sugar and milk

Tea Boil water Steep tea in boiling water Pour tea in cup Add lemon

Page 6: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Coffee Implementation

6

Page 7: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Tea Implementation

7

Page 8: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Code Duplication!

We have code duplication occurring in these two classes boilWater() and pourInCup() are exactly the same

Let’s get rid of the duplication

8

Page 9: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Similar Algorithms

The structure of the algorithms in prepareRecipe() is similar for Tea and Coffee We can improve our code further by making the code in

prepareRecipe() more abstract brewCoffeeGrinds() and steepTea() -> brew() addSugarAndMilk() and addLemon() -> addCondiments()

Now all we need to do is specify this structure in CaffeineBeverage.prepareRecipe() and make sure we do it in such a way so that subclasses can’t change the structure By using the word “final” (see next slide)

9

Page 10: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Caffeine Beverage Implementation

10

Note: use of final keyword for prepareRecipe()

brew() and addCondiments() are abstract and must be supplied by subclasses

boilWater() and pourInCup() are specified and shared across all subclasses

Page 11: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Coffee and Tea Implementations

11

Nice and simple!

Page 12: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+What have we done?

Took two separate classes with separate but similar algorithms

Noticed duplication and eliminated it by adding a superclass

Made steps of algorithm more abstract and specified its structure in the superclass Thereby eliminating another “implicit” duplication between

the two classes

Revised subclasses to implement the abstract (unspecified) portions of the algorithm… in a way that made sense for them

12

Page 13: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Singleton Pattern

Used to ensure that only one instance of a particular class ever gets created and that there is just one (global) way to gain access to that instance

Let’s derive this pattern by starting with a class that has no restrictions on who can create it

13

Page 14: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Deriving Singleton (I)

public class Ball {

private String color;

public Ball (String color) { this.color = color; }

public void bounce() { System.out.println(“boing!”); }

}

Ball b1 = new Ball (“red”);

Ball b2 = new Ball (“blue”);

b1.bounce();

b2.bounce();

14

Page 15: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Problem: Universal Instantiation

As long as a client object “knows about” the name of class Ball, it can create instances of Ball Ball b1 = new Ball (“orange”);

This is because the constructor is public We can stop unauthorized creation of Ball instances by

making the constructor private

15

Page 16: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Deriving Singleton (II)

public class Ball {

private String color;

private Ball(String color) { this.color = color; }

public void bounce() { System.out.println (“boing!”); }

}

// next line now impossible by any method outside of Ball

Ball b2 = new Ball (“red”);

16

Page 17: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Problem: No Point of Access!

Now that the constructor is private, no class can gain access to instances of Ball But our requirements were that there would be at least

one way to get access to an instance of Ball

We need a method to return an instance of Ball But since there is no way to get access to an instance of

Ball, the method CANNOT be an instance method This means it needs to be a class method, aka a static

method

17

Page 18: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Deriving Singleton

public class Ball {

private String color;

private Ball (String color) { this.color = color; }

public void bounce() { System.out.println (“boing!”); }

public static Ball getInstance (String color) {

return new Ball (color);

}

}

18

Page 19: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Problem: Back to Universal Instantiation

We are back to this problem where any client can create an instance of Ball; instead of saying this: Ball b1 = new Ball (“blue”);

they just say Ball b1 = Ball.getInstance (“blue”);

Need to ensure only one instance is ever created Need a static variable to store that instance

No instance variables are available in static methods

19

Page 20: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Deriving Singleton (IV)

public class Ball {

private static Ball ball;

private String color;

private Ball (String color) { this.color = color; }

public void bounce () { System.out.println(“boing!”); }

public static Ball getInstance(String color) {

return ball;

}

}

20

Page 21: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Problem: No instance!

Now the getInstance() method returns null each time it is called Need to check the static variable to see if it is null

If so, create an instance Otherwise return the single instance

21

Page 22: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Deriving Singleton (V)

public class Ball {

private static Ball ball;

private String color;

private Ball (String color) { this.color = color;}

public void bounce() { System.out.println(“boing!”); }

public static Ball getInstance (String color) {

if (ball == null) { ball = new Ball(color); }

return ball;

}

}

22

Page 23: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Problem: First Parameter Wins

The code on the previous slide shows the Singleton pattern private constructor private static variable to store the single instance public static method to gain access to that instance

this method creates object if needed; returns it

But this code ignores the fact that a parameter is being passed in; so if a “red” ball is created all subsequent requests for a “green” ball are ignored

23

Page 24: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Solution: Use a Map

The solution to the final problem is to change the private static instance variable to a Mapprivate Map<String, Ball> ballRecord = new HashMap…

Then check if the map contains an instance for a given value of the parameterthis ensures that only one ball of a given color is ever createdthis is a very acceptable variation of the Singleton patternpublic static Ball getInstance(String color) {

if (!ballRecord.containsKey(color)) {

ballRecord.put(color, new Ball(color));

}

return ballRecord.get(color);

}

24

Page 25: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Singleton Pattern: Structure

25

Singleton involves only a single class (not typically called Singleton). That class is a full-fledged class with other attributes and methods (not shown).

The class has a static variable that points at a single instance of the class.

The class has a private constructor (to prevent other code from instantiating the class) and a static method that provides access to the single instance.

Page 26: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Singleton Pattern: Real World Examples

Centralized manager of resources Window manager File system manager …

Logger classes

Factories Especially those that issue IDs Singleton is often combined with Factory Method and

Abstract Factory patterns

26

Page 27: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade (I)

“Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.” Design Patterns, Gang of Four, 1995

There can be significant benefit in wrapping a complex subsystem with a simplified interface If you don’t need the advanced functionality or fine-grained

control of the former, the latter makes life easy

27

Page 28: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade Pattern: Structure

28

Page 29: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade (II)

Façade works best when you are accessing a subset of the system’s functionality You can add new features by adding them to the Façade

(not the subsystem); you still get a simpler interface

Façade not only reduces the number of methods you are dealing with but also the number of classes Imagine having to pull Employees out of Divisions that

come from Companies that you pull from a Database A Façade in this situation can fetch Employees directly

29

Page 30: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Example (Without a Façade)

30

Without a Façade, Client contacts the Database to retrieve Company objects. It then retrieves Division objects from them and finally gains access to Employee objects.

It uses four classes.

Page 31: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Example (With a Façade)

31

With a Façade, the Client is shielded from most of the classes. It uses the Database Façade to retrieve Employee objects directly.

Page 32: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade Example (I)

Imagine a library of classes with a complex interface and/or complex interrelationships Home Theater System

Amplifier, DvdPlayer, Projector, CdPlayer, Tuner, Screen, PopcornPopper, and TheatreLights each with its own interface and interclass dependencies

32

Page 33: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade Example (II)

Imagine steps for “watch movie” turn on popper, make popcorn, dim lights, screen down,

projector on, set projector to DVD, amplifier on, set amplifier to DVD, DVD on, etc.

Now imagine resetting everything after the movie is done, or configuring the system to play a CD, or play a video game, etc.

33

Page 34: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade Example (III)

For this example, we can place high level methods… like “watch movie”, “reset system”, “play cd”

… in a façade object and encode all of the steps for each high level service in the façade; Demo

Client code is simplified and dependencies are reduced A façade not only simplifies an interface, it decouples

a client from a subsystem of components

Indeed, Façade lets us encapsulate subsystems, hiding them from the rest of the system

34

Page 35: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade vs. Adapter Comparison (I)

To many people, these two patterns (Adapter/Façade) appear to be similar They both act as wrappers of a preexisting class They both take an interface that we don’t want and convert

it to an interface that we can use

With Façade, the intent is to simplify the existing interface

With Adapter, we have a target interface that we are converting to In addition, we often want the adapter to plug into an

existing framework and behave polymorphically

35

Page 36: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Façade vs. Adapter Comparison (II)

Superficial difference Façade hides many classes Adapter hides only one

But a Façade can simplify a single, very complex object an adapter can wrap multiple objects at once in order to

access all the functionality it needs

The key is simplify (façade) vs. convert (adapter)

36

Page 37: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+More Information on Design Patterns… Patterns

http://en.wikipedia.org/wiki/Software_pattern http://hillside.net/patterns/patterns-catalog http://c2.com/ppr/

Interaction design patterns http://en.wikipedia.org/wiki/Interaction_design_pattern

Anti-Patterns http://en.wikipedia.org/wiki/Anti-pattern#Programming_anti-patterns

And numerous, numerous, numerous books and other online sources

Page 38: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Assignment 3, Part 1: Design Patterns Use design patterns to improve the design of (a portion of)

SimSE Waterfall Game SimSE is an educational software engineering simulation game

environment developed at UCI Papers about SimSE are available on my Website (and SimSE has

its own Website, linked off my site); feel free to consult them

Get the SimSE Waterfall Game code from the subversion repository; detailed instructions follow

38

Page 39: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Assignment 3, Part 1: Design Patterns Improve the design of (a portion of) SimSE Waterfall Game by

replacing existing structures in the code with appropriate patterns To scope the assignment, focus on the following six files:

simse.adts.actions.Action simse.gui.SimSEMap.java simse.gui.World.java simse.state.RequirementsDocumentStateRepository.java simse.state.State.java simse.util.IDGenerator

You do not have to use all six files You do not have to use only these six files

You may also include other files (or portions of files) if you wish You should apply at least 3 patterns

at least one not discussed in class

39

Page 40: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Assignment 3, Part 1: Design Patterns

Each use of a pattern should be carefully motivated in a brief accompanying document

Each individual must turn in: a printed copy of their restructured code a document describing

the motivation for each pattern the impact the application of the pattern has on the original

design (i.e., how far reaching is the change to incorporate the pattern?)

40

Page 41: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Assignment 3, Part 1: Design Patterns

Graded on usefulness of the pattern, diversity of patterns, rationale, and level of design understanding

Printed copy due Tuesday, February 11th at the beginning of class

Part 2 of this assignment will be given on Thursday and will also be due February 11th

So start now!

41

Page 42: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Two steps:1. Install Subclipse plug-in for Eclipse

2. Check out the SimSE patterns repository

Note: This assumes you’re using Eclipse and are otherwise comfortable with it.

42

Page 43: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

In Eclipse, go to Help -> Install New Software

43

Page 44: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Click “Add Site”; enter name “Subclipse”; enter location http://subclipse.tigris.org/update_1.10.x

44

Page 45: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Hit “OK”; Check the Subclipse main box (also check the SVNKit main box for Mac); Hit “Next”

45

Page 46: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Agree to all licenses; let the libraries download; click “OK” at this dialogue box

46

Page 47: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Make a new Project (NOT Java Project) and choose this:

47

Page 48: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Make a new repository location

48

Page 49: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Enter “https://svn.ics.uci.edu/isr/projects/simsepatterns/” and hit “Next”

49

Page 50: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Select the “simsepatterns” folder and click “Next” (NOT “Finish”)

50

Page 51: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Detailed Checkout Instructions

Check it out in the workspace; give it a name; click “Finish”

51

Page 52: + Informatics 122 Software Design II Lecture 9 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission

+Tips

If you are prompted for a username and password, use: username: informatics122 password: simse2014

If you receive any SSL certificate errors, you can safely ignore them

Check out the docs folder included with the download for helpful manuals about how to use SimSE and its tools Also see papers on my Website, SimSE Website as

mentioned earlier

52