UMD CMSC 433 - Iterators and Design Patterns (45 pages)

Previewing pages 1, 2, 3, 21, 22, 23, 43, 44, 45 of 45 page document View the full content.
View Full Document

Iterators and Design Patterns



Previewing pages 1, 2, 3, 21, 22, 23, 43, 44, 45 of actual document.

View the full content.
View Full Document
View Full Document

Iterators and Design Patterns

96 views

Other


Pages:
45
School:
University of Maryland, College Park
Course:
Cmsc 433 - Programming Language Technologies and Paradigms
Programming Language Technologies and Paradigms Documents

Unformatted text preview:

Administrivia CMSC 433 Programming Language Technologies and Paradigms Spring 2005 Other resources Thinking in Patterns with Java Link from the class web page Gamma Helm Johnson Vlissides Design Patterns Addison Wesley 1995 Freeman et al Head First Design Patterns O Reilly 2004 Iterators and Design Patterns February 15 2005 1 2 Example The Queue Class Inner Classes class Queue Element class Entry Java inner class Element elt Entry next Entry Element i elt i next null Classes can be nested inside other classes These are called inner classes private Entry theQueue Within a class that contains an inner class you can use the inner class just like any other class void enqueue Element e if theQueue null theQueue new Entry e else Entry last theQueue while last next null last last next last next new Entry e 3 4 Example The Queue Class cont d Referring to Outer Class class Queue Element Element dequeue throws EmptyQueueException if theQueue null throw new EmptyQueueException Element e theQueue elt theQueue theQueue next return e class Queue Element int numEntries class Entry Element elt Entry next Entry Element i elt i next null numEntries Each inner object has an implicit reference to the outer object whose method created it Can refer to fields directly or use outer class name 5 Other Features of Inner Classes 6 Anonymous Inner Classes Outside of the outer class use outer inner notation to refer to type of inner class E g Queue Entry An inner class marked static does not have a reference to outer class Can t refer to instance variables of outer class Must also use outer inner notation to refer to inner class new Thread public void run try Thread sleep 1000 60 20 System out println System exit 1 catch Exception e start Create anonymous subclass of thread and invoke method on it Question Can Queue Element be made static 7 8 Compiling Inner Classes Iteration The JVM doesn t know about inner classes Goal Loop through all objects in an aggregate Compiled away similar to generics



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Iterators and Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Iterators and Design Patterns and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?