Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 151: Object-Oriented Design December 3 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 December 3 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 December 3 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: December 3 CS 151: Object-Oriented Design © R. Mak 2 Frameworks  A framework consists of a set of cooperating classes. These classes implement the essential mechanisms for a particular problem domain. Example: Swing is a framework for what problem domain?  GUI programming  A framework is not a design pattern. Not a general design rule. A framework can use multiple design patterns. _

3 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 3 Application Framework  A set of classes that implement services that are common to a particular type of application. Examples: web application, enterprise application, database application, banking application  An application programmer builds an application by: Subclassing framework classes. Adding new classes that provide custom functionality.  Inversion of control The framework classes control the execution flow, not the application-specific classes. _

4 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 4 Applet Framework  An applet is a Java program that runs inside a web browser. Runs as a client-side application using the browser’s JVM. Its GUI is embedded in a web page. Its.class and.jar files are downloaded from a server along with the web page. _

5 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 5 Applet Class  An applet application extends the Applet class.  Methods init() : Called only once when the applet is first loaded. Initialize data structures and GUI elements. start() : Called when the applet is first loaded and whenever the user restores the browser window containing the applet. Start or restart animations or other computationally intensive tasks. stop() : Called whenever the user leaves the browser window containing the applet or the browser terminates. Stop computationally intensive tasks. destroy() : Called when the browser terminates. Relinquish resources obtained by init(). paint() : Called whenever the applet window needs repainting to update the view.

6 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 6 Applet Class, cont’d  The applet framework takes care of interacting with the browser, parsing parameters, determining when the applet is visible, etc. The application programmer only has to provide the classes that implement custom behavior.  Inversion of control means that the framework takes care of control flow. The application programmer only has to provide the functionality to initialize, start, stop, destroy, and paint, _ Applet demo http://www.apropos-logic.com/nc/PiBorwein.html

7 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 7 Applet Class, cont’d  On the web page that contains the applet: <applet code="numbercruncher.program13_3.PiBorweinApplet.class" codebase="./" align="middle" width="600" height="400" archive="PiBorwein.jar" name="PiBorweinApplet">

8 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 8 The Java Collections Framework  A set of classes that implement useful data structures: linked lists hash tables sets trees  Also useful algorithms coded as static methods: Collections.binarySearch() Collections.sort() Collections.reverseOrder() Collections.swap()  And more! It’s a framework that make it easy for you to add new classes that work well with existing collections.

9 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 9 Collections Framework Interfaces  Collection The most general collection interface type.  Set An unordered collection that does not permit duplicate elements.  SortedSet A set whose elements are visited in sorted order.  List An ordered collection. _

10 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 10 Collections Framework Classes  HashSet A set implementation that uses hashing to locate the set elements.  TreeSet A sorted set implementation that stores the elements in a balanced binary tree.  LinkedList and ArrayList Two implementations of the List interface type.

11 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 11 Collections Framework Class Diagram From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006.

12 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 12 All Collection Classes are Generic Types  The type parameter denotes the type of the objects in the collection. Example: ArrayList = new ArrayList ();

13 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 13 The Collection Interface Type  A class that implements interface Collection holds objects in some way. Different collection classes may have different ways to store and access the objects in their collections. collection class = data structure  The interface declares many methods: boolean add(E obj) boolean addAll(Collection c) void clear() boolean contains(E obj) boolean containsAll(Collection c) boolean equals(E obj) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(E obj) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() E[] toArray() E[] toArray(E[] a) Don’t confuse interface Collection with class Collections.

14 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 14 The AbstractCollection Class  Because the Collection interface has so many methods, it would be a pain to implement.  Therefore, Java helpfully provides the AbstractCollection class that supplies a default implementation for each method. Except for size() and iterator(). Example: public E[] toArray() { E[] result = new E[size()]; Iterator e = iterator(); for (int i = 0; e.hasNext(); i++) { result[i] = e.next(); } return result; }

15 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 15 The Iterator Interface  We recall the Iterator interface:  Interface Collection extends interface Iterable which has only one method:  Any class that implements interface Iterable can be used in a “for each” loop. Therefore, all collections can. Example: boolean hasNext() E next() void remove() Iterator iterator() for (Employee e : employeeList) {... } Don’t confuse Iterator and Iterable.

16 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 16 Add a New Collection Class  Add a new BoundedQueue class to the collections framework. The new class extends AbstractCollection It will work well with the existing classes of the framework. From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. Demo Bounded

17 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 17 The Set Interface Type  Interface Set adds no new methods! But it’s not a marker interface. (Why not?)  By definition, a set has these restrictions: Set collections don’t store duplicate objects. Set collections are unordered.  Having a separate Set interface allows a method to require a set object with its restrictions rather than another type of collection object. _ public interface Set extends Collection {}

18 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 18 The List Interface Type  A list is an ordered collection of objects, each of which is accessed with an integer index.  Interface List extends interface Collection and adds these methods: boolean add(int index, E obj) boolean addAll(int index, Collection c) E get(int index) int indexOf(E obj) int lastIndexOf(E obj) ListIterator listIterator() ListIterator listIterator(int index) E remove(int index) E set(int index, int E) List subList(int fromIndex, int toIndex)

19 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 19 The ListIterator Interface Type  Interface ListIterator extends interface Iterator and adds these methods: int nextIndex() int previousIndex() boolean hasPrevious() E previous() void add(E obj) void set(E obj)

20 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 20 List Class Diagram From: Object-Oriented Design & Patterns, John Wiley & Sons, 2006. Hey, what’s this for?

21 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 21 Lists and Binary Searches  Does it make sense to perform a binary search on an ArrayList object? a LinkedList object?  You can access each element of an array list directly using an index. It takes the same amount of time no matter which element. Therefore, it makes perfectly good sense to perform a binary search on an ArrayList object.  You can access an element of a linked list only by “chasing pointers” from the head of the list down to the element you want. Different elements have different access times depending on how far away they are from the head. Therefore, it does not make sense to perform a binary search on a LinkedList object.

22 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 22 The RandomAccess Interface  RandomAccess is a marker interface.  Class ArrayList implements RandomAccess, but not LinkedList. Therefore, before doing a binary search, you can test for RandomAccess : if (list instanceof RandomAccess) { // do a binary search } else { // do a linear search }

23 SJSU Dept. of Computer Science Fall 2013: December 3 CS 151: Object-Oriented Design © R. Mak 23 Views  A view is a collection object. Its class implements one of the interfaces of the collections framework. It permits only restricted access to the collection.  A view allows you to access the objects in the collection in a certain way. You can limit the view (restricted access). Or you can enhance the collection. Example: A list has a richer interface than a plain old array! String[] strings = {"Kenya", "Thailand", "Portugal"}; List view = Arrays.asList(strings); anotherCollection.addAll(view); Don’t get confused with the view in model-view-controller.


Download ppt "CS 151: Object-Oriented Design December 3 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