1 / 36

Frameworks

Overview of the Collections framework in Java API, including the Collection, Set, SortedSet, and List interfaces, as well as concrete classes like HashSet, TreeSet, LinkedList, and ArrayList. Covers methods specified by the Collection interface and the use of AbstractCollection class. Discusses the addition of new classes to the framework, such as the Queue class, and the benefits of using the framework. Explores the Set interface and the List interface, including their methods and implementations.

carcuri
Download Presentation

Frameworks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Frameworks Part 2

  2. Collections Framework • Java API contains library of useful data structures • Collections library also serves as framework for adding new collection classes that can interact with existing classes

  3. Overview of collections framework • Collection: data structure that contains multiple objects (elements) • Collections framework specifies four interfaces: • Collection: general collection (bag) • Set: unordered collection of unique elements • SortedSet: ordered collection of unique elements • List: ordered collection of elements; can contain duplicates

  4. Concrete classes supplied by collections framework • 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

  5. Collections Framework

  6. Collection and Iterator • Collection and Iterator are the fundamental interfaces of the collections framework • A collection is any class that can store multiple elements; individual collection classes enforce different rules for how data are to be stored and located • An iterator is a mechanism for visiting all elements in a collection

  7. Methods specified by Collection interface boolean add(Object obj) boolean addAll(Collection c) void clear() boolean contains(Object obj) boolean containsAll(Collection c) boolean equals(Object obj) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Object obj) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object[] toArray() Object[] toArray(Object[] a

  8. AbstractCollection class • Collection is a hefty interface: client programmer must implement 15 methods • AbstractCollection class relieves client programmer of this burden; provides reasonable default implementations for almost all of these methods

  9. Example method from AbstractCollection public Object[] toArray() { Object[] result = new Object[size()]; Iterator e = iterator(); for (int i=0; e.hasNext(); i++) result[i] = e.next(); return result; } Note use of size() and iterator(); these are the two methods left undefined by the abstract class. Which pattern is in play here?

  10. Notes on AbstractCollection • Methods like toArray() implement the template method pattern; since they are implementations, not just specifications, they must appear in a class • Methods size() and iterator() are left undefined by AbstractCollection; most concrete collections derived from this class also override add() and remove()

  11. Adding new class to Collections framework • Text uses example of Queue class; originally developed in chapter 3 without connection to framework • Why use framework? • Framework provides a number of methods that work on all Collections; addAll(), for example, does bulk addition of all elements from one collection to another • If Queue is part of framework, such methods are automatically applicable to the new class

  12. Set Interface • Definition of Set: public interface Set extends Collection{} • No methods - why have it? • Set is conceptually a subtype of Collection; Set is unordered Collection without duplicates • Separate interface serves to tag objects with Set quality; algorithms can require Set objects as distinct from other Collections

  13. List interface • List: ordered collection of elements • Each list position accessible via integer index • Interface specifies several additional methods to general Collection type; most of these are concerned with index positions

  14. List interface methods boolean add(int index, Object obj) boolean addAll(int index, Collection c) Object get(int index) int indexOf(Object obj) int lastIndexOf(Object obj) ListIterator listIterator() ListIterator listIterator(int index) Object remove(int index) Object set(int index, int Object) List subList(int fromIndex, int toIndex)

  15. ListIterator interface • Subtype of Iterator • Adds: • support for indexing • ability to move both forward and backward through list

  16. ListIterator interface • Methods include: • int nextIndex() • int previousIndex() • boolean hasPrevious() • Object previous() • void add(Object obj) • void set(Object obj)

  17. List classes • ArrayList • LinkedList • provides “indexed” access to individual list elements, but it’s klunky and slow (must visit all predecessor elements to get to desired element) • highlights weakness in the framework design; should have provided separate interface types for indexed collections (arrays) and ordered collections (lists)

  18. RandomAccess interface • Version 1.4 of Java sdk has RandomAccess interface to fix design problem • Tagging interface (no methods) - can use to test whether or not indexed access is really appropriate for a particular object • ArrayList implements RandomAccess, but LinkedList does not

  19. Optional operations • API documentation tags some methods as optional operations • Default implementations throw UnsupportedOperationException • Collection methods add() and remove() are examples

  20. Views • View: object that implements an interface of collections framework, but permits only restricted access to data structure • Optional operations exist to support views • Built-in Java type array has no methods; can apply view to enhance array functionality

  21. Views • Example: asList method • asList turns an array into a view object: collection that implements List interface, so List methods can be applied to array elements • does not copy array elements; get and set methods of view object access original array • Can’t apply add() or remove() methods to view, because size of underlying array can’t be changed: this is why these methods are “optional”

  22. Graph editor framework • Problem domain: interactive editing of graphs • Graph: collection of nodes and edges arranged in certain shape • Examples of graphs: • class relationship diagrams • electronic circuit diagrams • flow charts

  23. Graph editor framework • Encapsulates aspects common to all graph editing applications • user interface • event handling • Application programmer extends graph editor framework, defines specific behavior for nodes and edges

  24. User interface • Toolbar includes: • grabber tool for selecting elements • buttons for each node/edge type • menus: • loading/saving diagram • deleting selected elements • Drawing area: • Mouse used for drawing; behavior depends on current tool: • if node, clicking empty space inserts new node • if grabber, clicking on element selects it; dragging operation moves selected node & connected edges • if edge, drag from one node to another inserts edge

  25. Division of responsibility • Drawing shapes of nodes & edges: application • Hit testing (element hit by mouse click): application • Drawing toolbar: framework • Mouse listening: framework

  26. Division of responsibility • Application programmer must inform framework about node & edge types of particular graph type • concrete graph gives framework prototype objects • toolbar queries graph for prototypes, adds buttons for each one • nodes & edges draw themselves in paintIcon method of button icon object • when user inserts new node or edge, object corresponding to selected tool is cloned & added to graph

  27. Prototype pattern • Teaches how a system can instantiate classes that are not known when system is built • Nature of node & edge types unknown when framework code is designed - use prototype pattern to solve this problem

  28. Prototype pattern • Context: • A system instantiates objects of classes that are not known when the system is built. • You do not want to require a separate class for each kind of object. • You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects.

  29. Prototype pattern • Solution: • Define a prototype interface type that is common to all created objects. • Supply a prototype object for each kind of object that the system creates. • Clone the prototype object whenever a new object of the given kind is required.

  30. Prototype pattern

  31. Name in pattern: Prototype ConcretePrototype1 Creator Name in graph editor: Node CircleNode Instance of GraphPanel that handles mouse operation for adding new nodes Prototype pattern applied to graph editor

  32. Framework classes • Node and Edge interfaces include methods: • draw(): used in painting graph • contains(): tests whether mouse click falls within range of an element • getBounds(): returns rectangle enclosing element • clone(): clones prototypes when inserting new elements

  33. Framework classes • Edge interface includes methods getStart() and getEnd() which return nodes connected by edge • Node interface method getConnectionPoint() returns optimal attachment point for edge • Edge method getConnectionPoints() yields 2 end points of edge; used by grabbers to mark currently selected edge • Framework also supplies AbstractEdge class for programmer convenience

  34. Framework classes • Graph class: • collects nodes and edges • has methods for adding, removing, finding and drawing nodes and edges • is abstract; subclasses must override methods that populate the toolbar: public abstract Node[] getNodePrototypes() public abstrac Edge[] getEdgePrototypes

  35. Framework classes • GraphFrame class manages the toolbar, menu bar, and graph panel • Toolbar class holds toggle buttons for node and edge icons • GraphPanel class displays graph, handles mouse events for editing commands

  36. Building Graph Editor Application • For each node and edge type, define class that implements Node or Edge • Define subclass of Graph with methods getNodePrototypes and getEdgePrototypes defined to supply prototype objects for graph elements • Supply a main() method

More Related