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 HouseholdPet { void feed(Food f); } Any class that implements an interface is guaranteed to implement each and every one of the interface methods. public interface ActionListener { void actionPerformed(ActionEvent event); } public interface Icon { int getIconWidth(); int getIconHeight(); void paintIcon(Component c, Graphics g, int x, int y); } SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak
Review: 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 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. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak
Review: The Factory Method Design Pattern Context An application can instantiate any one of several subclasses of a superclass (or any one of several implementers of an interface). Description You know that your application needs to instantiate one of the subclasses. But you won’t know which subclass until run time. You need to provide a means to instantiate the subclass as determined by the application at run time. Your code must have the flexibility to instantiate and work with any of the subclasses. Solution 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
Review: Factory Method Example ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow ThrowCalculator calc = ThrowCalculator.makeCalculator(type, ...); public abstract class ThrowCalculator { public static ThrowCalculator makeCalculator(int type, ...) switch (type) { case RANDOM: return new RandomThrow(...); break; case SMART: return new SmartThrow(...); break; case GENIUS: return new GeniusThrow(...); break; } public abstract Throw calculateThrow(...); // abstract method SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak
Review: Coding to the Interface ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow ThrowCalculator calc = ThrowCalculator.makeCalculator(type, ...); Throw t = calc.calculateThrow(...); Variable calc has the interface type ThrowCalculator. It refers to an object that can be a RandomThrow, a SmartThrow, or a GeniusThrow. An object of one of those types is returned by the factory. Variable t = calc.calculateThrow(...) 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
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. Solution Create a strategy interface that is an abstraction of the algorithm. 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
The Strategy Design Pattern We’ve already seen examples of this pattern: Calendar Gregorian Lunar ComputerPlayer Throw Calculator SmartThrow RandomThrow GeniusThrow Recall the Comparator interface used by the Collections class to sort an array list of objects. How can you apply the strategy design pattern? You can have several classes that implement the Comparator interface all with different compare() methods. Compare by weight, height, etc. SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak
A Powerful Technique for Flexibility To achieve maximum code flexibility, combine The strategy design pattern The factory method design pattern 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
NetBeans Swing Tutorials Introduction to GUI Building http://netbeans.org/kb/docs/java/gui-functionality.html Designing a Swing GUI in NetBeans IDE http://netbeans.org/kb/docs/java/quickstart-gui.html NetBeans Swing design demo SJSU Dept. of Computer Science Fall 2013: October 8 CS 151: Object-Oriented Design © R. Mak