cs 160: software engineering october 22 class meeting department of computer science san jose state...
TRANSCRIPT
CS 160: Software EngineeringOctober 22 Class Meeting
Department of Computer ScienceSan Jose State University
Fall 2014Instructor: Ron Mak
www.cs.sjsu.edu/~mak
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
2
Project Team Design Reviews
Monday, October 27
Section 3 Code Monsters 160 Zaibatsu Tin Bullet
Section 4 Quiet Coders Surprise Error Dream Team
Wednesday, October 29
Section 3 List Ninja Merge Monkeys Noisy Coders
Section 4 Activate League of Gentlemen Not Applicable
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
3
Project Team Design Reviews, cont’d
Present and defend key aspects of your design. 20 minutes per team.
Use PowerPoint slides Turn in your slides after your presentation.
Briefly discuss your overall design.
Show and explain the design of your most important or core classes. UML class diagrams and/or sequence diagrams
_
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
4
Project Team Design Reviews, cont’d
Briefly discuss your implementation plan.
Questions and answers at the end.
Audience: Very technical
Other development engineers. Technical managers, stakeholders, financial backers,
etc. Expect “deep” questions from the audience!
_
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
5
A Useful Combination of Techniques
Interfaces
Coding to the interface
Polymorphism
Design patterns strategy design pattern factory method design pattern
Dynamic class loading
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
6
Interfaces and Classes
A Java class defines:
an interface a set of operations (methods)
an implementation statements that specify how to carry out the operations statements that specify how to represent object state
It can be useful to separate an interface from its implementation.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
7
Java Interface
A Java interface type Defines a set of operations (methods). Does not define their implementations.
A Java class can implement an interface. The class must implement (supply the statements
for) each of the interface’s methods.
A Java class can implement multiple interfaces. The class must implement all the methods of the
interfaces that it implements.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
8
The Interface as a Contract
A Java interface can be implemented by multiple classes.
Each class can implement an interface method in a different but related way.
An interface is a contract.
Any class that implements the interface is guaranteed to implement each and every one of the interface methods.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
9
Interface Example
Interface Shape defines the draw() method.
Classes Rectangle, Triangle, and Circle each implements interface Shape. Each class must therefore implement
the draw() method.
Each class implements draw() in a different but related way. Each class’s draw() method draws the shape. But each shape is drawn differently.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
10
A Class Hierarchy Puzzle
Animal
Lion Dog Piranha Goldfish Parrot Hummingbird
Mammal Fish Bird
We want to add the category HouseholdPet.
Do we make it a superclass? Where does it belong in this class hierarchy?
How do we also add the category Biter?
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
11
A Class Hierarchy Puzzle
Make HouseholdPet and Biter Java interfaces. A Java class can implement multiple interfaces.
Animal
Mammal Fish Bird
Lion Dog Piranha Goldfish Parrot Hummingbird
«interface»HouseholdPet
«interface»Biter
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
12
Java Subclass
If a class C is a subclass of superclass S, then C “is a” S:
Animal
Mammal Fish Bird
Lion Dog Piranha Goldfish Parrot Hummingbird
Dog is an Animal.Dog is a Mammal.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
13
Java Interface
If a class C implements interface N, then C “is a” N:
Animal
Mammal Fish Bird
Lion Dog Piranha Goldfish Parrot Hummingbird
«interface»HouseholdPet
«interface»Biter
Dog is a HouseholdPet.
Dog is a Biter.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
14
Java instanceof Operator
If the value of variable x is of type Dog, then the following conditionals are all true:
x instanceof Dog x instanceof Mammal x instanceof Animal x instanceof HouseholdPet x instanceof Biter
_
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
15
Polymorphism
A variable can have an interface type. Example:
At run time, variable sh can have any object as a value that was instantiated from a class that implements the Shape interface.
A call to sh.draw() will call the rectangle, triangle, or circle draw method, depending on the value of sh.
Polymorphism: The ability to determine automatically at run time which method to call.
Shape sh;
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
16
Polymorphism, cont’d
A variable can have an interface type. A value cannot have an interface type.
A value can be an object instantiated from a class that implements an interface. Or a value can be a scalar.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
17
Coding to the Interface
Compare:
Are there advantages of one vs. the other? The first declaration allows variable sh to be
assigned only a Rectangle object. The second declaration allows sh to be assigned
a Rectangle, Triangle, or Circle object. Or any object instantiated from a class that
implements interface Shape. This more flexible style is coding to the interface.
Shape sh;
Rectangle sh;
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
18
Design Patterns
A design pattern is
A description of a problem. A solution that you can apply
to many programming situations.
Design patterns show you how to build good software with good object-oriented design qualities.
Design patterns are proven object-oriented experience.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
19
Design Patterns, cont’d
Design patterns are not code, but are general solutions to design problems. You apply them to your specific application.
Design patterns are not invented – they’re discovered.
Design patterns address how to manage change._
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
20
Design Patterns, cont’d
Design patterns give programmers a very high-level, short-cut vocabulary to discuss design issues.
Independent of specific implementations or programming languages.
“We should use the factory method design pattern here.”
“The decorator pattern will simplify this code.”
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
21
Design Patterns, cont’d
Each design pattern has
A short name A brief description of the context A description of the problem that it solves A prescription for a solution
_
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
22
Design Patterns, cont’d
Building architect Christopher Alexander discovered over 250 patterns for architectural design. Co-authored A Pattern Language: Towns, Buildings,
Construction, published in 1977.
In 1995, four authors, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (AKA “The Gang of Four”) published the classic software book Design Patterns. Original 23 design patterns
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
23
The Strategy Design Pattern
Context
There are different algorithms (“strategies”) to solve a particular problem.
Description The algorithms all have similar public interfaces,
but each solves the problem in a different way.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
24
The Strategy Design Pattern, cont’d
Solution Create a strategy interface that is
an abstraction of the algorithm.
Declare the interface shared by the algorithms.
Code each strategy in a class that implements the strategy interface.
At run time, select one of the strategies and call its interface methods._
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
25
The Strategy Design Pattern, cont’d
<<interface>>Shape
draw() : void
Triangle
draw() : void
Rectangle
draw() : void
Circle
draw() : void
Strategy interface
Strategy Strategy Strategy
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
26
The Factory Method Design Pattern
Context
An application can instantiate any one of several classes that implement an interface.
Description
You know that your application needs to instantiate one of the classes. But you won’t know which class until run time.
You need to provide a means to instantiate the classas determined by the application at run time. Therefore, your code must have the flexibility
to instantiate and work with any of the classes.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
27
The Factory Method Design Pattern
Solution
Design a factory method that will, based on its parameters, create and return an object that is instantiated from the correct class.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
28
Factory Method Example
public class GreeterFactory1 { public static Greeter make(String language) throws Exception { if (language.equals("English")) { return new English(); } else if (language.equals("French")) { return new French(); } else { throw new Exception(); } }}
<<interface>>Greeter
English French
public interface Greeter { String greet();}
public class English implements Greeter{ public String greet() { return "Hello!"; }}
Demo
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
29
Factory Method Example, cont’d
public class GreeterFactory1 { public static Greeter make(String language) throws Exception { if (language.equals("English")) { return new English(); } else if (language.equals("French")) { return new French(); } else { throw new Exception(); } }}
How can we make this factory method even more flexible?
Hardcoded to work only with English and French.
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
30
Dynamic Class Loading
Suppose that when we write the factory method,we don’t know which languages are available.
At run time, the factory method can dynamically load a language class if it is given the name of the language:
where language is a string containing the name of a language.
Class.forName(language)
Computer Science Dept.Fall 2014: October 22
CS 160: Software Engineering© R. Mak
31
Dynamic Class Loading, cont’d
public class GreeterFactory2 { public static Greeter make(String language) throws ClassNotFoundException, InstantiationException, IllegalAccessException { return (Greeter) Class.forName(language).newInstance(); }} Dynamically load a language class,
and instantiate and return a language object.
Demo