1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures...

36
1 Frameworks Part 2
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    0

Transcript of 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures...

Page 1: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

1

Frameworks

Part 2

Page 2: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 3: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 4: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 5: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

5

Collections Framework

Page 6: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 7: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 8: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 9: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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 leftundefined by the abstract class. Which pattern is in play here?

Page 10: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 11: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 12: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 13: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 14: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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)

Page 15: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

15

ListIterator interface

• Subtype of Iterator

• Adds:– support for indexing– ability to move both forward and backward

through list

Page 16: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

16

ListIterator interface

• Methods include: – int nextIndex()– int previousIndex()– boolean hasPrevious()– Object previous()– void add(Object obj)– void set(Object obj)

Page 17: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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)

Page 18: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 19: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

19

Optional operations

• API documentation tags some methods as optional operations

• Default implementations throw UnsupportedOperationException

• Collection methods add() and remove() are examples

Page 20: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 21: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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”

Page 22: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 23: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 24: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

24

User interfaceToolbar 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

Page 25: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

25

Division of responsibility

• Drawing shapes of nodes & edges: application

• Hit testing (element hit by mouse click): application

• Drawing toolbar: framework

• Mouse listening: framework

Page 26: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 27: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 28: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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.

Page 29: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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.

Page 30: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

30

Prototype pattern

Page 31: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

31

Prototype pattern applied to graph editor

• Name in pattern:– Prototype

– ConcretePrototype1

– Creator

• Name in graph editor:– Node

– CircleNode

– Instance of GraphPanel that handles mouse operation for adding new nodes

Page 32: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 33: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 34: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 35: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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

Page 36: 1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.

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