Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
-
Upload
julian-carroll -
Category
Documents
-
view
212 -
download
0
Transcript of Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
![Page 1: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/1.jpg)
Computer Science 209
Introduction to Design Patterns:
Iterator
Composite
Decorator
![Page 2: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/2.jpg)
Patterns and Design Problems
• A design pattern gives advice about solving a problem in software design
• Different problems often have features in common
• A design pattern captures a common way of solving problems in a set of rules
![Page 3: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/3.jpg)
Example: Visiting a Set of Items in Sequence
StringTokenizer tokens = new StringTokenizer("The cat is on the mat");while (tokens.hasMoreTokens()) System.out.println(tokens.nextToken());
Iterator<String> iter = aSet.iterator();while (iter.hasNext()) System.out.println(iter.next ());
Scanner reader = new Scanner(blah blah blah);while (reader.hasNext()){ String data = reader.nextLine(); System.out.println(data);}
![Page 4: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/4.jpg)
The Iterator Pattern
• The iterator pattern captures the common elements of this set of problems and gives advice, in the form of a set of rules, for solving them
• Each pattern has – a short name – a brief description of the context– a lengthy description of the problem– a prescription for a solution
Proven advice in a standard format
![Page 5: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/5.jpg)
The Iterator Pattern: Context
• An aggregate contains elements
• Clients need to access the elements
• The aggregate should not expose its internal structure
• There may be multiple clients that need simultaneous access
![Page 6: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/6.jpg)
The Iterator Pattern: Solution
• Define an iterator class that fetches one element at a time
• Each iterator object tracks the position of the next element
• Because there might be multiple aggregate and iterator classes, it is best to have a single iterator interface
![Page 7: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/7.jpg)
The Context of the Composite Pattern
• I want to combine primitive objects into compound objects
• I want clients to treat these compounds as primitives
![Page 8: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/8.jpg)
Solution of the Composite Pattern
• Define an interface as an abstraction for the primitive objects
• A composite object contains a collection of the primitives
• Both primitives and composites implement the interface
• Composite applies method to primitives and combines results
![Page 9: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/9.jpg)
The Composite Pattern
Composite
<<interface>>Primitive
Leaf
method()
method()
*
The composite calls method() for each leaf and combines the results.
![Page 10: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/10.jpg)
Example: GUI Components
• Some components, like buttons and fields, are primitives
• Other components, like frames and panels, are containers (composites)
• A container computes its preferred size by combining all the preferred sizes of its components
![Page 11: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/11.jpg)
The Context of the Decorator Pattern
• I want to enhance the behavior of a class, called the component class
• A decorated component can be used in the same way as a plain component
• The component does not want the responsibility of the decoration
• There may be any number of decorations
![Page 12: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/12.jpg)
Solution of the Decorator Pattern
• Define an interface as an abstraction for the component
• Component classes and decorator classes implement this interface
• A decorator object manages the component object that it decorates
• The decorator applies a method to a component and combines the result with the effect of the decoration
![Page 13: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/13.jpg)
The Decorator Pattern
Decorator
<<interface>>Component
ConcreteComponent
method()
method()
1
The decorator calls method() for the component and augments the results.
![Page 14: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/14.jpg)
Example: Scroll Panes
• A scroll pane can decorate several components, such as text areas and list boxes
• The scroll pane’s paint method paints both the decorated component and the scroll bars
![Page 15: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/15.jpg)
Using JScrollPane
JTextArea outputArea = new JTextArea();
JScrollPane outputPane = new JScrollPane(outputArea);
container.add(outputPane);
// Reference outputArea later in the program
![Page 16: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/16.jpg)
Example: File Streams
• java.io includes many classes for manipulating different kinds of I/O streams
• Start with a basic input or output stream and decorate it until you get the type of stream you want
![Page 17: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/17.jpg)
Using Input StreamsFileInputStream stream = new FileInputStream("anyfile.txt");
InputStreamReader iStrReader = new InputStreamReader(stream); // Now read characters from iStrReader or
BufferedReader reader = new BufferedReader(iStrReader);
// Now read lines of text from reader or
StreamTokenizer nizer = new StreamTokenizer(reader);
// Now read tokens (words) from nizer
Each decoration adds a new layer of functionality
![Page 18: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/18.jpg)
Example: A ScannerScanner reader = new Scanner(new File("anyfile.txt")); // Now read data from the file
The same pattern is applied in similar situations
Scanner reader = new Scanner(System.in); // Now read data from the keyboard
Scanner reader = new Scanner("any file many other words text"); // Now read data from the string
![Page 19: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/19.jpg)
Where Do Patterns Come From?
• Software best practices
• Catalogued and discussed in the Gang of Four book, Design Patterns
• Web sites abound
![Page 20: Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.](https://reader036.fdocuments.net/reader036/viewer/2022072014/56649eb35503460f94bb9bde/html5/thumbnails/20.jpg)
How Do I Spot the Need for One?
• Remember a place where a pattern is put to use and see the similarity of the current situation to that place
• Look at the intent of a pattern to see whether it applies in the current situation
• Review the context and solution parts of each potential pattern in detail