140 likes | 170 Views
Understand various design patterns including creational, structural, and behavioral patterns with examples like Singleton, Decorator, Iterator, Observer. Explore the concepts and applications in Java through sample codes.
E N D
Design Patterns…Part 2 Elizabeth Duea CS 5667 October 22, 2003
Recap… • Creational patterns – concern the process of object creation • Structural patterns – deal with the composition of classes or objects • Behavioral patterns – characterize the ways in which the classes or objects interact and distribute responsibility
More examples • Creational Pattern Example - Singleton (in class Monday) • Structural Pattern Example - Decorator • Behavioral Pattern Example - Iterator - Observer
We played with an example of the Singleton pattern in class on Monday. • In a few minutes, we will some of the homework that was turned in… • First we will talk about a few other patterns and then see the examples.
Decorator • Intent: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extended functionality • AKA: Wrapper • See handouts for more information • Now for the example…
Decorator adds responsibility and levels. For example: JAVA I/O • The InputStreamReader builds on the functionality of being able to read basic bytes, and provides the functionality of being able to read characters (In Java, characters are potentially 2 bytes). • The BufferedReaders takes the basic functionality of being able to read characters and provides the ability to read strings and also buffers the input for efficiency.
DecoratorExample.java import java.io.*; public class DecoratorExample { public static void main(String[] args) throws Throwable { BufferedReader in = new BufferedReader( // Decorates the InputStreamReader new InputStreamReader( // Decorates the InputStream System.in)); // an InputStream System.out.println("Please type a word or phrase"); System.out.println(in.readLine()); } }
Iterator • Intent: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation • AKA: Cursor • See handouts for more information • Now for the example…
IteratorExample.java import java.util.*; public class IteratorExample { public static void main(String[] args) { List list = new ArrayList(); for (int i = 0; i < 10; ++i) { list.add(new Integer(i)); } for (Iterator i = list.iterator(); i.hasNext(); ) { System.out.println(i.next()); } Set set = new HashSet(); for (int i = 0; i < 10; ++i) { set.add(new Integer(i)); } for (Iterator i = set.iterator(); i.hasNext(); ) { System.out.println(i.next()); } } }
Observer • Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically • AKA: Dependents, Publish-Subscribe • See handouts for more information • Now for the example…
ObserverExample.java public class ObserverExample { public static void main(String[] args) { Model model = new Model(); View1 view1 = new View1(); View2 view2 = new View2(); model.addObserver(view1); model.addObserver(view2); model.setValue(27); model.setValue(42); } }
Model.java import java.util.*; public class Model extends Observable { private int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; setChanged(); notifyObservers(); } public String toString() { return "Example Model"; } }
View1.java, View2.java import java.util.*; public class View1 implements Observer { public void update(Observable o, Object arg) { System.out.println("View 1 has been notified of a change in " + o); System.out.println("New value: " + ((Model) o).getValue()); } } import java.util.*; public class View2 implements Observer { public void update(Observable o, Object arg) { System.out.println("View 2 has been notified of a change in " + o); System.out.println("New value: " + ((Model) o).getValue()); } } ~
Homework • On Monday we played with an example of the Singleton design pattern. • The assignment was to write a small program using this pattern