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.

Slides:



Advertisements
Similar presentations
Feb 14, principles 1&2 Focus on users and their tasks –Identify the users –Clarify the purpose (goal software helps users achieve) –What advantage does.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Graphics without NGP The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns that are.
Design Patterns.
Design Patterns and Graphical User Interfaces Horstmann ,
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
CS 151: Object-Oriented Design October 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
GoF: Document Editor Example Rebecca Miller-Webster.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Design. Practices Principles Patterns What are the characteristics of good design? What are good solutions to common design problems? How do we go about.
Copyright © Curt Hill More Widgets In Abstract Window Toolbox.
Chapter 14- More Swing, Better looking applications.
Decorator Design Pattern Rick Mercer CSC 335: Object-Oriented Programming and Design.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
1 CSE 331 Composite Layouts; Decorators slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
S.Ducasse Stéphane Ducasse 1 Adapter.
Frame Windows Application program, not applet Construct and show frame JFrame frame = new JFrame(); *** frame.show(); *** Set default close operation..
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
The Decorator Pattern Decorators in Java I/O classes SE-2811 Dr. Mark L. Hornick 1.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Graphical User Interfaces
Slide design: Dr. Mark L. Hornick
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Introduction to Design Patterns
Lecture 27 Creating Custom GUIs
University of Central Florida COP 3330 Object Oriented Programming
Top 5 Principles of Effective Web Design. The fantastic factor concerning about web design is that there are such a big amount of ways to approach an.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Design Patterns in Swing and AWT
Design Patterns in Swing and AWT
Object Oriented Design Patterns - Structural Patterns
Decorator Pattern Richard Gesick.
Composite Pattern Context:
Responsive Framework.
GUI building with the AWT
CS 350 – Software Design Principles and Strategies – Chapter 14
Design pattern Lecture 9.
Strategy Design Pattern
Lesson 19 Organizing and Enhancing Worksheets
Outline More text components Tool bars Sliders Scrollbars Lists Tables
Chapter 17 Creating User Interfaces
Object Oriented Design & Analysis
GUI building with the AWT
Decorator Pattern.
Chapter 17 Creating User Interfaces
Activity 1 - Chapter 5 -.
A picture's worth a thousand words
Graphical User Interface
Presentation transcript:

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 class can be used the same as the base class. Solution: Create an interface for the base class. The base class implements the interface. Create a decorator that implements the interface and wraps the plain class, "decorating" its behavior. Component + paint( g: Graphics ) ...other methods... 1 JScrollPane createHorizontalScrollBar createVerticalScrollBar Button +paint( g: Graphics ) ... TextComponent +paint( g: Graphics ) ...

08/15/09 Decorator Example Purpose: create a TextArea with scrollbars so that text will scroll when larger than the viewport. // create TextArea with 5 rows, 40 columns JTextArea textArea = new JTextArea( 5, 40 ); // decorate with JScrollPane to add scrollbars JScrollPane pane = new JScrollPane( textArea ); pane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED ); // Add the decorator to the contentpane. // Don't add the textArea! contentPane.add( pane );

Advantage of Using Decorators (1) 08/15/09 Advantage of Using Decorators (1) We can write a behavior one time and apply it to many different kinds of objects. Example: a JScrollPane can be applied to any component, not just JTextArea.

Advantage of Using Decorators (2) 08/15/09 Advantage of Using Decorators (2) Improves the cohesion of objects, by not adding responsibility that isn't part of the object's main purpose. Example: the purpose of a TextArea is to display text! Not to manage scrolling.

Advantage of Using Decorators (3) 08/15/09 Advantage of Using Decorators (3) New decorators can be added in the future, extending the behavior of the class. Example: a zoom decorator to zoom a component. Open-Closed Principle A class should be open for extension but closed for modification.

Disadvantage of Decorators 08/15/09 Disadvantage of Decorators Lots of pass-through methods: Any method the decorator doesn't "decorate" itself, it must pass to the decorated object.