CS 151 ObjectOriented Design October 8 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www. cs. sjsu. edu/~mak
The Java Interface as a Contract public interface Household. Pet { void feed(Food f); } Any class that implements an interface is guaranteed to implement each and every one of the interface methods. public interface Action. Listener { void action. Performed(Action. Event event); } public interface Icon { int get. Icon. Width(); int get. Icon. Height(); void paint. Icon(Component c, Graphics g, int x, int y); } SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 2
Review: Design Patterns o A design pattern is n n o Design patterns show you how to build good software with good object-oriented design qualities. n o o Design patterns are proven object-oriented experience. Design patterns are not code, but are general solutions to design problems. n o A description of a problem. A solution that you can apply to many programming situations. You apply them to your specific application. Design patterns are not invented – they’re discovered. Design patterns address how to manage change. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 3
Review: The Factory Method Design Pattern o Context n o An application can instantiate any one of several subclasses of a superclass (or any one of several implementers of an interface). Description n You know that your application needs to instantiate one of the subclasses. o n You need to provide a means to instantiate the subclass as determined by the application at run time. o o But you won’t know which subclass until run time. Your code must have the flexibility to instantiate and work with any of the subclasses. Solution n Design a factory method that will, based on its parameters, create and return an object of the correct subclass type. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 4
Review: Factory Method Example Computer. Player Throw Calculator Smart. Throw Random. Throw. Calculator calc = Throw. Calculator. make. Calculator(type, . . . ); Genius. Throw public abstract class Throw. Calculator { public static Throw. Calculator make. Calculator(int type, . . . ) { switch (type) { case RANDOM: return new Random. Throw(. . . ); break; case SMART: return new Smart. Throw(. . . ); break; case GENIUS: return new Genius. Throw(. . . ); break; } } public abstract Throw calculate. Throw(. . . ); // abstract method } SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 5
Review: Coding to the Interface Computer. Player Throw Calculator Smart. Throw Random. Throw Genius. Throw. Calculator calc = Throw. Calculator. make. Calculator(type, . . . ); Throw t = calculate. Throw(. . . ); o o Variable calc has the interface type Throw. Calculator. n It refers to an object that can be a Random. Throw, a Smart. Throw, or a Genius. Throw. n An object of one of those types is returned by the factory. Variable t = calculate. Throw(. . . ) doesn’t care which type of object calc refers to. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 6
The Strategy Design Pattern o Context n o Description n o There are different algorithms (“strategies”) to solve a particular problem. The algorithms all have similar public interfaces, but each solves the problem in a different way. Solution n Create a strategy interface that is an abstraction of the algorithm. o n n Declare the public 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 public interface methods. _ SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 7
The Strategy Design Pattern o We’ve already seen examples of this pattern: Computer. Player Calendar Throw Calculator Gregorian Calendar o Lunar Calendar Smart. Throw Random. Throw Genius. Throw Recall the Comparator interface used by the Collections class to sort an array list of objects. n n How can you apply the strategy design pattern? You can have several classes that implement the Comparator interface all with different compare() methods. o Compare by weight, height, etc. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 8
A Powerful Technique for Flexibility o To achieve maximum code flexibility, combine n n The strategy design pattern The factory method design pattern o o o with dynamic class loading Coding to the interface The strategy design pattern tells us how to handle multiple algorithms to solve a problem. The factory method design pattern tells us how to instantiate an algorithm at run time. Coding to the interface ensures we don’t hard-code to any one algorithm. Demo SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 9
Net. Beans Swing Tutorials o Introduction to GUI Building http: //netbeans. org/kb/docs/java/gui-functionality. html o Designing a Swing GUI in Net. Beans IDE http: //netbeans. org/kb/docs/java/quickstart-gui. html Net. Beans Swing design demo SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak 10
- Slides: 10