1 / 14

Design Patterns…Part 2

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

lhaag
Download Presentation

Design Patterns…Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns…Part 2 Elizabeth Duea CS 5667 October 22, 2003

  2. 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

  3. More examples • Creational Pattern Example - Singleton (in class Monday) • Structural Pattern Example - Decorator • Behavioral Pattern Example - Iterator - Observer

  4. 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.

  5. 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…

  6. 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.

  7. 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()); } }

  8. 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…

  9. 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()); } } }

  10. 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…

  11. 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); } }

  12. 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"; } }

  13. 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()); } } ~

  14. Homework • On Monday we played with an example of the Singleton design pattern. • The assignment was to write a small program using this pattern

More Related