Download presentation
Presentation is loading. Please wait.
1
Design Patterns in JDK Collections Christine Bouamalay SBC Services, San Ramon, CA
2
Design Constraints for a Standard Data Structures Library ● Used directly or indirectly by every programmer for everything within its scope ● Primitive in the mathematical sense: individual components designed to perform only a single role ● Invaluable and affordable to essentially every student and professional programmer
3
Design Constraints for a Standard Data Structures Library (con.) ● Convenient, efficient, and reasonably safe for common use ● Complete at what they do ● Supportive of commonly-accepted programming styles Bjarne Stroustrup, The C++ Programming Language (Special Third Ed.), Addison-Wesley, 2000.
4
Forces between contradictory constraints ● Used directly or indirectly by every programmer for everything within its scope ● Primitive in the mathematical sense: individual components designed to perform only a single role ● Invaluable and affordable to essentially every student and professional programmer ● Convenient, efficient, and reasonably safe for common use ● Complete at what they do ● Supportive of commonly- accepted programming styles
5
Tradeoffs between Design Goals: How elaborate a Hierarchy? Initial: Standalone Vector (Stack?!), Hashtable JDK 1.2 Framework Interfaces, Abstract Classes Application-level Classes JDK 1.5 Framework A bit broader and deeper Abstract Queue, Priority Queue Rejected Purer Alternatives Could avoid “OperationNot Supported” Cost: Greatly increased footprint and complexity
6
The Java 2 Collections Bruce Eckel, Thinking in Java, 2 nd Ed.
7
Java 2 Collections, Simplified Bruce Eckel, Thinking in Java, 2 nd Ed.
8
UpdatableCollection Doug Lea, “Overview of the Collections Classes”
9
Design Patterns: Resolving Tensions between Data Structures Library Design Goals Iterator Template Method Support for Concurrency
10
Delegating Traversals Application-Code Traversals Iterator Traversals
11
GOF Iterator
12
Iterators Enable “Extrafunctionality” Iterators Iterators with Extrafunctionality
13
Enumeration vs. Iterator ● Elements elements() ● boolean hasMoreElements() ● Object nextElement() ● Iterator iterator() ● boolean hasNext() ● Object next() ● remove()
14
GOF “Implementation Issues” ● Who controls the iterator? ● Who defines the iterator? Iterators may need privileged access. ● Using polymorphic iterators ● Additional iterator operations? ● How robust is the iterator?
15
Who controls the Iterator? External vs. Internal Iterators Internal Iterator External Iterator
16
JDK 1.5 foreach Prior to JDK 1.5: public boolean containsAll (Collection c) { for (Iterator itr = c.iterator; iter.hasNext(); ) { if (!contains(iter.next()) { return false; } return true; } JDK 1.5: public boolean containsAll (Collection c) { for (Object o : c) { if (!contains(o) { return false; } return true; }
17
Who defines the iterator? Iterators may need privileged access. public abstract class AbstractList implements List {.... private class Itr implements Iterator {... } private class ListItr implements ListIterator {..} } Dual Hierarchy: Collection Iterator LinkedHashMap Collection ArrayList LinkedHashIterator ListIterator
18
Additional iterator operations? Iterator next() hasNext() remove() List Iterator previous() hasPrevious() nextIndex() previousIndex() set() add()
19
Using Polymorphic Iterators ● Iterator Collection.iterator() Factory method public interface Iterable Iterator iterator(); } public interface Collection extends Iterable { Iterator iterator();..... } ● No reset()... Just acquire a new iterator()! ● Garbage collection targets providing better support for cheap, short-lived, new objects
20
How robust is the Iterator? public abstract class AbstractList implements List {.... private class Itr implements Iterator { int expectedModCount = modCount;..... final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }.... }
21
Template Method
22
AbstractList depends on polymorphic Iterator operations public abstract class AbstractList implements List {.... public boolean addAll(int index, Collection c) { boolean modified = false; Iterator e = c.iterator(); while (e.hasNext()) { add(index++, e.next()); modified = true; } return modified; }... }
23
Collections depend on polymorphic equals(), compareTo() public abstract class AbstractList implements List {.... public int IndexOf(Object o) {.... ListIterator.... }... public int lastIndexOf(Object o) {.... ListIterator.... }... } HashMap, HashSet Arrays.sort.... equals()........ compareTo()...
24
Support for Concurrency Vector, Hashtable made ALL methods synchronized JDK 1.2 Decorator for synchronized, unmodifiable collections: Map ckts = Collections.SynchornizedMap( new HashMap ); java.util.concurrency: Application-level Tools: Concurrent HashMap, ConvurrentLinkedQueue Futures Executor Core Mechanisms Available: Exchanger SynchronousQueue CyclicBarrier Semaphore
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.