CS 151 ObjectOriented Design October 22 Class Meeting
CS 151: Object-Oriented Design October 22 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Midterm Solutions: Question 1 o Briefly describe how each of the following can help you write software that tolerates design changes. n encapsulation o o n factory method design pattern o o n Do not hard-code the instantiation of classes. Decide at run time what types of objects to create. strategy design pattern o o n Isolate parts of the design that will change. Changes will not affect other parts of your design. Design multiple strategies (algorithms) to solve a problem. Strategies share a common interface. programming to the interface o o Do not hard-code dependence on a particular object type. Handle different object types that share an interface or superclass. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 2
Midterm Solutions: Question 2 o Refactor for coherency and consistency. n Rewrite the class as multiple classes. public class Department. Enrollment { private String name; private Array. List<Klass> classes; private Array. List<Student> students; public int find. Class. Code(String name, int section) {. . . } Klass find. Class(String class. Name) {. . . } Section find. Class. Section(int class. Code) {. . . } Student find. Student(int student. Id) {. . . } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 3
Midterm Solutions: Question 2 public class Department. Enrollment {. . . public void enroll(int student. Id, int class. Code) { find. Class. Section(class. Code). enroll. Student(student. Id); find. Student(student. Id). set. Enrolled(true); } public int number. Of. Enrolled. Students() { // Scan students array and count enrolled students. }. . . } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 4
Midterm Solutions: Question 2 public class Department. Enrollment {. . . public int class. Code. With. Longest. Wait. List() { // Scan classes array and pick the class // with the longest wait list. } public void create. New. Class. Section(String class. Name) { find. Class(name). create. New. Class. Section(); } } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 5
Midterm Solutions: Question 2 public class Klass { private String name; private Array. List<Section> sections; public void create. New. Class. Section() {. . . } public int find. Class. Code(int section. Number) { // Return the class code of the section. } public int number. Of. Enrolled. Students() { // Sum enrolled students in the sections. } public int number. Of. Wait. List. Students() { // Sum waitlisted students in the sections. } SJSU Dept. of Computer Science CS 151: Object-Oriented Design Fall © R. Mak } 2013: October 22 6
Midterm Solutions: Question 2 public class Section { private int number; private int code; private Array. List<Student> enrolled; private Array. List<Student> wait. List; public int number. Of. Enrolled. Students() { return enrolled. size(); } public int number. Of. Wait. List. Students() { return wait. List. size(); } public void enroll. Student(int student. Id) {. . . } } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 7
Midterm Solutions: Question 2 public class Student { private String last. Name; private String first. Name; private String id; private boolean enrolled; public void set. Enrolled(boolean e) { enrolled = e; } } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 8
Midterm Solutions: Question 3 a o Draw a UML class diagram that captures all of the following information. Include as much detail and be as specific as necessary, but not more. Show any multiplicities that are not 1 (“several” means “at least one”). n n n n The library has several departments, three of which are Circulation, Reference, and Community. There is a head librarian for the entire library, and an assistant librarian for each department. Each assistant librarian reports to the head librarian. Most books are meant to circulate. Library patrons can check out each book for a period of time and take it out of the library. At any time, such a book can be Available (on the shelf ready to be checked out), Checked out, Overdue, Returned (but not yet Available), or Missing. Other books, such as encyclopedias and dictionaries, are for reference and cannot be checked out. They can only be used in the library. The Circulation Department is in charge of books that can circulate. The Reference Department is in charge of reference books. Each librarian is an SJSU employee and therefore has a campus email address, a campus phone number, and other such contact information. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 9
Midterm Solutions: Question 3 a Librarian reports. To Assistant Librarian Library 1. . n Book Department circulate : boolean Employee Circulation Contact Info Reference 1. . n Status 1. . n Community SJSU Dept. of Computer Science Fall 2013: October 22 <<enumeration>> CS 151: Object-Oriented Design © R. Mak Available Checked. Out Overdue Returned Missing 10
Midterm Solutions: Question 3 b o The MLK Library’s Community Department is in charge of fund raising for the library. It runs various fund-raising activities. Two current activities are the used book store and renting out conference rooms to community groups. Each activity requires planning, execution, and accounting operations. Future activities, unknown as this time, will also require these operations. o Draw the UML class diagram for the Community Department. _ SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 11
Midterm Solutions: Question 3 b Community 1. . n <<interface>> Activity plan() execute() do. Accounting() SJSU Dept. of Computer Science Fall 2013: October 22 Room. Rental Book. Store plan() execute() do. Accounting() CS 151: Object-Oriented Design © R. Mak 12
Midterm Solutions: Question 3 b o Write some Java code that models the department’s activities. Make your code as flexible as possible. Point out what design techniques you are using. n Use the Strategy Design Pattern o Room. Rental and Book. Store are different Activity strategies. _ SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 13
Midterm Solutions: Question 3 b n n Use the Factory Method Design Pattern to create new activities. Use dynamic class loading in the factory method to enable future activity plug-ins. public interface Activity { void plan(); void execute(); void do. Accounting(); } public class Activity. Factory { public static Activity make(String name) throws Class. Not. Found. Exception, Instantiation. Exception, Illegal. Access. Exception { return (Activity) Class. for. Name(name). new. Instance(); } SJSU Dept. of Computer Science CS 151: Object-Oriented Design } 2013: October 22 Fall © R. Mak 14
Midterm Solutions: Question 3 b n Code to the interface instead of hard-coding to a specific activity. try { Activity activity = Activity. Factory. make(activity. Name); activity. plan(); activity. execute(); activity. do. Accounting(); } catch (Exception ex) {. . . } SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 15
Midterm Solutions: Question 4 o Make the main class implement Action. Listener. n Then it must implement the action. Performed() method. public class Midterm. Timer 2 implements Action. Listener { private JFrame frame = new JFrame(); private final int FIELD_WIDTH = 20; private JText. Field text. Field = new JText. Field(FIELD_WIDTH); public void action. Performed(Action. Event event) { Date now = new Date(); text. Field. set. Text(now. to. String()); } private void test() {. . . Timer t = new Timer(DELAY, this); . . . } SJSU Dept. of Computer Science CS 151: Object-Oriented Design. . . Fall 2013: October 22 © R. Mak } 16
Layout Managers o A Swing container is a component that can contain other components. n o A Swing container delegates laying out its components to a layout manager. n n o Examples: A JFrame object is a container. Later, we’ll see that a JPanel object is also a container. Swing has several predefined layout managers. Example: The Flow. Layout manager. A layout manager also controls how the individual components reshape themselves when you reshape the container at run time. n Example: You reshape a JFrame window with the mouse and the window contents reshape themselves. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 17
Layout Managers SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 18
The Strategy Design Pattern o Having different layout managers is another example of the use of the Strategy Design Pattern. n Context: A container class can use one of several different algorithms to lay out its components. n Solution: Each layout manager class implements a specific layout strategy (algorithm) and implements interface Layout. Manager. Demo SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 19
Compound Components o In Swing, container components such as JFrame and JPanel objects can contain other components. n o A container component can itself be contained in another container component. n o Example: A panel contains labels, text fields, and buttons. Example: A JPanel component is contained in a JFrame window or in another JPanel component. If you call a method on a container object, such as repaint(), the container object calls the method with the same signature on each component object that it contains. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 20
The Composite Design Pattern o Context n n o “Primitive” objects (such as labels, text fields, and buttons) can be combined into a composite object (such as a panel). A client object can treat the composite object as if it were a primitive object. Solution n n Define a “primitive” interface type that is an abstraction for primitive objects. Composite objects collect primitive objects. The classes for the primitive objects and for the composite object implement the primitive interface. The implementation of each interface method in the composite class calls the method with the same signature on each primitive object that it contains, and combines the results. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak 21
The Composite Design Pattern o In Swing, the “Primitive” interface is the Component superclass. n o JButton is a Component. So is JPanel. When a “Primitive” interface method is called on the composite object, the composite object must call the method with the same signature on each “primitive” object that it contains. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 22
Swing Component Class Hierarchy Every JComponent is both a Component and a Container. SJSU Dept. of Computer Science Fall 2013: October 22 CS 151: Object-Oriented Design © R. Mak From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. 23
- Slides: 23