Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 2 Iterators  An iterator allows you to traverse a sequence of objects. Example: Going through the elements of a linked list.  An iterator hides the implementation of the sequence. You don’t have to know about links and pointers. _

3 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 3 Iterators  Traditional linked list traversal:  Traversal with an iterator: Link currentLink = list.head; while (currentLink != null){ Object current = currentLink.data; currentLink = currentLink.next; // Do something with the current object. } LinkedList list =...; ListIterator iterator = list.listIterator(); while (iterator.hasNext()) { String current = iterator.next(); // Do something with the current object. }

4 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 4 Advantages of Iterators  Hide the implementation of the sequence. A form of encapsulation: The implementation can change.  A sequence can have multiple iterators simultaneously.  The iterator concept can apply beyond linked lists. String tokenizers Input streams etc.  Therefore, iterators are a design pattern! _

5 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 5 The Iterator Design Pattern  Context An aggregate object contains element objects. Clients of the aggregate need to access the elements. The aggregate doesn’t expose its implementation. There can be multiple clients simultaneously accessing the elements.  Solution An iterator class that accesses one element at a time. Each iterator keeps track of the next element that it will access. _

6 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 6 The Iterator Design Pattern From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

7 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 7 Linked List Iterator Name in Design PatternActual Name for Linked List AggregateList ConcreteAggregateLinkedList IteratorListIterator ConcreteIterator anonymous class implementing ListIterator createIterator()listIterator() next() isDone() opposite of hasNext() currentItem() return value of next() From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

8 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 8 Scrollbars  Several Swing components can have scrollbars. Examples: Text areas, lists, panels  How can we do scrollbars? Should every class that wants scrollbars implement them? In general, scrollable Swing components do not want the responsibility to implement scrollbars.  Scroll vertically and/or horizontally.  Scroll by an atomic amount (e.g., one line at a time) and scroll by a large amount (e.g., a page at a time).  Find a solution where you can augment the functionality of a component by adding scrollbar capabilities. The component’s behavior is not otherwise changed. “Decorate” the component with scrollbars.

9 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 9 Swing ScrollPane  How to add scrollbars to a text area:  How to add scrollbars to a list: JTextArea area = new JTextArea(25, 80); JScrollPane scrolledArea = new JScrollPane(area); JList list = new JList(); JScrollPane scrolledList = new JScrollPane(list);  Now both the text area and the list have scrollbars. The text area and list components have not changed. The scrollable text area and scrollable list are also components. The scrollbars add functionality to the text area and to the list, and so they are called decorators. _ Demo

10 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 10 The Decorator Design Pattern  Context You want to enhance the behavior of a component class. The decorated component can be used like a plain component. The component class does not take on the responsibility of implementing the decoration. There may be multiple types of decorations.  Solution The decorator object manages the component object that it decorates. Both the component class and the decorator class implement the same component interface. When the decorator implements a method of the component interface, it applies the method to the decorated component and adds the effect of the decoration.  Example: The paint() method also paints scrollbars.

11 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 11 The Decorator Design Pattern From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

12 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 12 Scrollable Text Area Name in Design PatternActual Name for Scrollable Text Area Component ConcreteComponentJTextArea DecoratorJScrollPane method() a method of Component (e.g. paint() )

13 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 13 I/O Stream Decorators  Reader class Basic input operations. Read a single character or an array of characters.  FileReader subclass of Reader Read characters from a file. No method to read a line of input.  BufferedReader class Add the ability to read an entire line of text: BufferedReader in = new BufferedReader( new FileReader("input.txt")); String line = in.readLine(); Decorate the file reader with the ability to read entire lines.

14 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 14 Buffered File Reader Name in Design PatternActual Name for Input Streams ComponentReader ConcreteComponentInputStreamReader DecoratorBufferedReader method()read()

15 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 15 Assignment #5  Add a well-designed graphical user interface (GUI) to your Rock-Paper-Scissors game application using the Java Foundation Classes (Swing).  Your GUI should have: A text field to enter the number of throws per match and a list component to select which computer player’s throw choice calculator (random or smart) before the start of a match.  Disable these components once a match starts so that their values can’t be changed during the match. Buttons for the human player to make a throw choice (rock, paper, or scissors). A help button to display a help message. _

16 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 16 Assignment #5  You should also display: The computer’s prediction of the human player’s choice for each throw. The running score (human wins, computer wins, ties).  How much code from Assignment #4 can you reuse? How well did you encapsulate the parts of your software design that you knew would change? Inversion of control will be a challenge!  Due Monday, November 11. _

17 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 17 Online quiz 2013Oct24

18 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 18 Design Patterns Example: Invoice Application  An invoice is composed of line items. A line item has a description and the price of the item.  A store can sell bundles of related items. In the invoice, a bundle contains multiple line items. A bundle is itself a line item. The getPrice() method of a bundle is the sum of calling getPrice() on each item in the bundle: public double getPrice() { double price = 0; for (LineItem item : items) { price += item.getPrice(); } return price; }  Which design pattern does this suggest for bundles?

19 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 19 Design Patterns Example The Composite Design Pattern to implement bundles. From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

20 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 20 Design Patterns Example  A store can offer discounts. Discount the price of a line item.  Construct a DiscountedItem object by passing it the item to be discounted. The getPrice() method of the DiscountedItem class calls the getPrice() method of the item and applies the discount. public double getPrice() { return item.getPrice()*(1 - discount/100); }  Which design pattern does this suggest for discounts?

21 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 21 Design Patterns Example The Decorator Design Pattern to implement discounts. From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

22 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 22 Design Patterns Example  The invoice application uses a GUI. Invoice text is shown in a text area.  Whenever new items area added to the invoice, the invoice text should automatically update.  Add a collection of objects (such as the text areas) to the Invoice class. Each object’s class implements the ChangeListener interface: public interface ChangeListener { void stateChanged(ChangeEvent event); } Whenever the invoice changes, notify each object by calling its stateChanged() method.  Which design pattern does this suggest?

23 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 23 Design Patterns Example The Observer Design Pattern to update invoice text. From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

24 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 24 Design Patterns Example  Which design pattern should you use to print each item of an invoice? From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. The Iterator Design Pattern to traverse the invoice items.

25 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 25 Design Patterns Example  Print the invoice using one of several different formats. Implement each format in a formatter class, such as SimpleFormatter.  Which design pattern does this suggest for the invoice formatters?

26 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 26 Design Patterns Example From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. The Stragegy Design Pattern to manage the invoice formatters. Fancy Formatter

27 SJSU Dept. of Computer Science Fall 2013: October 24 CS 151: Object-Oriented Design © R. Mak 27 Design Patterns Example  Which design pattern should you use to create the invoice formatters at run time only as needed? The Factory Method Design Pattern to create the invoice formatters as needed.


Download ppt "CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google