310 likes | 436 Views
CS 160: Software Engineering October 22 Class Meeting. Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak www.cs.sjsu.edu/~ mak. Project Team Design Reviews. Monday, October 27 Section 3 Code Monsters 160 Zaibatsu Tin Bullet Section 4 Quiet Coders
E N D
CS 160: Software EngineeringOctober 22 Class Meeting Department of Computer ScienceSan Jose State UniversityFall 2014Instructor: Ron Mak www.cs.sjsu.edu/~mak
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
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_
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!_
A Useful Combination of Techniques • Interfaces • Coding to the interface • Polymorphism • Design patterns • strategy design pattern • factory method design pattern • Dynamic class loading
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.
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.
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.
Interface Example • Interface Shapedefines 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.
Mammal Fish Bird Lion Dog Piranha Goldfish Parrot Hummingbird A Class Hierarchy Puzzle • 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? Animal
Animal Mammal Fish Bird Lion Dog Piranha Goldfish Parrot Hummingbird «interface» HouseholdPet «interface» Biter 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 Java Subclass • If a class C is a subclass of superclass S, then C “is a” S: • Dog is an Animal. • Dog is a Mammal.
Animal Mammal Fish Bird Lion Dog Piranha Goldfish Parrot Hummingbird «interface» HouseholdPet «interface» Biter Java Interface • If a class C implements interface N, then C “is a” N: • Dog is a • HouseholdPet. • Dog is a • Biter.
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 instanceofHouseholdPet • x instanceof Biter_
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;
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.
Coding to the Interface • Compare: • Are there advantages of one vs. the other? • The first declaration allows variable shto 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. • Rectangle sh; • Shape sh;
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.
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._
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.”
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_
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
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.
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._
The Strategy Design Pattern, cont’d <<interface>> Shape Strategy interface draw() : void Triangle Rectangle Circle draw() : void draw() : void draw() : void Strategy Strategy Strategy
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.
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.
<<interface>> Greeter English French Factory Method Example public interface Greeter { String greet(); } 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(); } } } public class English implements Greeter { public String greet() { return "Hello!"; } } Demo
Factory Method Example, cont’d • How can we make this factory method even more flexible? 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(); } } } Hardcoded to work only with English andFrench.
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 languageis a string containing the name of a language. Class.forName(language)
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