Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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 _

4 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! _

5 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak A Useful Combination of Techniques  Interfaces  Coding to the interface  Polymorphism  Design patterns strategy design pattern factory method design pattern  Dynamic class loading 5

6 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 6

7 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 7

8 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 8

9 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 9

10 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 10 A Class Hierarchy Puzzle Animal LionDogPiranhaGoldfishParrotHummingbird MammalFishBird  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 ?

11 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter

12 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird Dog is an Animal. Dog is a Mammal.

13 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 MammalFishBird LionDogPiranhaGoldfishParrotHummingbird «interface» HouseholdPet «interface» Biter Dog is a HouseholdPet. Dog is a Biter.

14 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 _

15 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 15 Shape sh;

16 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 16

17 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 17 Shape sh; Rectangle sh;

18 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.

19 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. _

20 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.”

21 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 _

22 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

23 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.

24 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. _

25 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak The Strategy Design Pattern, cont’d 25 > Shape draw() : void Triangle draw() : void Rectangle draw() : void Circle draw() : void Strategy interface Strategy

26 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 class as determined by the application at run time.  Therefore, your code must have the flexibility to instantiate and work with any of the classes.

27 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.

28 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(); } > Greeter EnglishFrench public interface Greeter { String greet(); } public class English implements Greeter { public String greet() { return "Hello!"; } Demo

29 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.

30 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak 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. 30 Class.forName(language)

31 Computer Science Dept. Fall 2014: October 22 CS 160: Software Engineering © R. Mak Dynamic Class Loading, cont’d 31 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


Download ppt "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."

Similar presentations


Ads by Google