Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.

Similar presentations


Presentation on theme: "Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator."— Presentation transcript:

1 Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator

2 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

3 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 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); }

4 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

5 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

6 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

7 The Context of the Composite Pattern I want to combine primitive objects into compound objects I want clients to treat these compounds as primitives

8 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

9 The Composite Pattern Composite > Primitive Leaf method() * The composite calls method() for each leaf and combines the results.

10 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

11 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

12 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

13 The Decorator Pattern Decorator > Component Concrete Component method() 1 The decorator calls method() for the component and augments the results.

14 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

15 Using JScrollPane JTextArea outputArea = new JTextArea(); JScrollPane outputPane = new JScrollPane(outputArea); container.add(outputPane); // Reference outputArea later in the program

16 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

17 Using Input Streams FileInputStream 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

18 Example: A Scanner Scanner 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

19 Where Do Patterns Come From? Software best practices Catalogued and discussed in the Gang of Four book, Design Patterns Web sites abound

20 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


Download ppt "Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator."

Similar presentations


Ads by Google