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.

Slides:



Advertisements
Similar presentations
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Advertisements

Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 5: Patterns and GUI Programming CPSC 2100 Software Design and Development 1.
Chapter 10 Introduction to Arrays
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Winter 2012ACS-3913 Ron McFadyen1 Model View Controller Originated with Smalltalk development (1979) Separates a system’s classes into View, Controller,
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Hwajung Lee.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
CS 235: User Interface Design August 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 151: Object-Oriented Design December 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
ITEC324 Principle of CS III Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Modified from slides by Dr. Hwajung Lee.
Design Patterns and Graphical User Interfaces Horstmann ,
CS 151: Object-Oriented Design September 3 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 235: User Interface Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1 CSC 222: Computer Programming II Spring 2004 See online syllabus at: Course goals:
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design September 5 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 21 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
CS 151: Object-Oriented Design October 31 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design November 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Programming Paradigms Different paradigms Procedural paradigm, e.g. Pascal Basic Functional paradigm, e.g. Lisp Declarative paradigm, e.g. Prolog Object-Oriented.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 17 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CS 151: Object-Oriented Design November 19 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 28 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Chapter 5 Patterns and GUI Programming -Part 1-. Pattern A pattern is a description of a problem and its solution that you can apply to many programming.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Chapter 5 Patterns and GUI Programming. Pattern A pattern is a description of a problem and its solution that you can apply to many programming situation.
Pattern and GUI Programming
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CS 153: Concepts of Compiler Design August 29 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
Composite Pattern Context:
08/15/09 Decorator Pattern Context: We want to enhance the behavior of a class, and there may be many (open-ended) ways of enhancing the class. The enhanced.
Chapter 17 JavaScript Arrays
CS 144 Advanced C++ Programming April 9 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting
Presentation transcript:

CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak

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

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

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

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

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.

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.

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.

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

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.

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.

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() )

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.

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()

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

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

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

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?

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.

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?

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.

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?

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.

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, The Iterator Design Pattern to traverse the invoice items.

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?

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, The Stragegy Design Pattern to manage the invoice formatters. Fancy Formatter

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.