26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 21 Collections 1229<br />

public static method header<br />

Collection unmodifiableCollection( Collection c )<br />

List unmodifiableList( List aList )<br />

Set unmodifiableSet( Set s )<br />

SortedSet unmodifiableSortedSet( SortedSet s )<br />

Map unmodifiableMap( Map m )<br />

SortedMap unmodifiableSortedMap( SortedMap m )<br />

Fig. Fig. 21.15 21.15 Unmodifiable wrapper methods.<br />

Software Engineering Observation 21.9<br />

You can use an unmodifiable wrapper <strong>to</strong> create a collection that offers read-only access <strong>to</strong><br />

others, while allowing read–write access <strong>to</strong> yourself. You do this simply by giving others a<br />

reference <strong>to</strong> the unmodifiable wrapper while you also retain a reference <strong>to</strong> the wrapped collection<br />

itself. 21.9<br />

21.11 Abstract Implementations<br />

The collections framework provides various abstract implementations (i.e., “bare bones”<br />

implementations of collection interfaces from which the programmer can quickly “flesh<br />

out” complete cus<strong>to</strong>mized implementations). These abstract implementations are a thin<br />

Collection implementation called an AbstractCollection, a thin List implementation<br />

with random-access backing called an AbstractList, a thin Map implementation<br />

called an AbstractMap, a thin List implementation with sequential-access<br />

backing called an AbstractSequentialList and a thin Set implementation called<br />

an AbstractSet.<br />

To write a cus<strong>to</strong>m implementation, begin by selecting as a base the abstract-implementation<br />

class that best meets your needs. Next, implement each of the class’s abstract<br />

methods. Then, if your collection is <strong>to</strong> be modifiable, override any concrete methods that<br />

prevent modification.<br />

21.12 (Optional) Discovering Design Patterns: Design Patterns<br />

Used in Package java.util<br />

In this section, we use the material on data structures and collections discussed in Chapters<br />

19, 20 and 21 <strong>to</strong> identify classes from package java.util that use design patterns. This<br />

section concludes our treatment of design patterns.<br />

21.12.1 Creational Design Patterns<br />

We conclude the discussion of creational design patterns by discussing the Pro<strong>to</strong>type design<br />

pattern.<br />

Pro<strong>to</strong>type<br />

Sometimes, a system must make a copy of an object but will not know that object’s class<br />

until run time. For example, consider the drawing program design of Exercise 9.28—class-<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/12/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!