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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 10 API Review; Where Next.
Advertisements

Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Chapter 10 Introduction to Arrays
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Plab – Tirgul 12 Design Patterns
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Software Engineering I Object-Oriented Design Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science.
Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Hwajung Lee.
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 16 Slide 1 User interface design.
More OOP Design Patterns
Domain Modeling (with Objects). Motivation Programming classes teach – What an object is – How to create objects What is missing – Finding/determining.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
ITEC324 Principle of CS III Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Modified from slides by Dr. Hwajung Lee.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Design Patterns and Graphical User Interfaces Horstmann ,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
IM103 week 11 Part 2 P532 Case Study part 2: the Airport GUI Learning objectives By the end of this lecture you should be able to:  use the JTabbedPane.
ASP.Net, Web Forms and Web Controls 1 Outline Session Tracking Cookies Session Tracking with HttpSessionState.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Session 30 Final Review. Final Details Wednesday, December 14 at 8 AM Wright 5 (same classroom) Final will be comprehensive Open book Open notes Test.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Chapter 5 Patterns and GUI Programming -Part 1-. Pattern A pattern is a description of a problem and its solution that you can apply to many programming.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Pattern and GUI Programming
Software Design Refinement Using Design Patterns
Design Patterns Lecture part 2.
Objectives You should be able to describe: Interactive Keyboard Input
Chapter 11 Object-Oriented Design
Software Design and Architecture
Instructor: Dr. Hany H. Ammar
Iterator and Composite Design Patterns
JAVA IO.
08/15/09 Decorator Pattern Context: We want to enhance the behavior of a class, and there may be many (open-ended) ways of enhancing the class. The enhanced.
UNIT-III Structural Design Patterns
Adapter Pattern Context:
13. Composite Pattern SE2811 Software Component Design
Computer Science 209 The Adapter Pattern.
13. Composite Pattern SE2811 Software Component Design
Activity 1 - Chapter 5 -.
Presentation transcript:

Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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