Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.

Slides:



Advertisements
Similar presentations
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Advertisements

Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 5: Patterns and GUI Programming CPSC 2100 Software Design and Development 1.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
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.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Object-Oriented Analysis and Design
Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Hwajung Lee.
Design patterns Observer,Strategi, Composite,Template (Chap 5, 6)
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
Graphics without NGP The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns that are.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
ITEC324 Principle of CS III Chapter 5 (Horstmann’s Book) Patterns and GUI Programming Modified from slides by Dr. Hwajung Lee.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns and Graphical User Interfaces Horstmann ,
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Context : –Often complex structures are built with container and primitive objects. Container.
Computer Science 209 Introduction to Design Patterns: Iterator Composite Decorator.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
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.
GoF: Document Editor Example Rebecca Miller-Webster.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSC 480 Software Engineering Design With Patterns.
Chapter 9 Putting together a complete system. This chapter discusses n Designing a complete system. n Overview of the design and implementation process.
OO Methodology Elaboration Iteration 2 - Design Patterns -
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 5150 Software Engineering Lecture 16 Program Design 3.
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.
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. Pattern A pattern is a description of a problem and its solution that you can apply to many programming situation.
Chapter 5 Patterns and GUI Programming -Part 2-. STRATEGY Pattern Layout Managers What if we need to specifies pixel position of components when  User.
Pattern and GUI Programming
Conception OBJET GRASP Patterns
Observer Design Pattern
Chap 7. Building Java Graphical User Interfaces
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Design Patterns in Swing and AWT
Design Patterns in Swing and AWT
GoF Design Patterns (Ch. 26)
Model-View-Controller Patterns and Frameworks
Composite Pattern Context:
CSC 480 Software Engineering
Behavioral Patterns Part-I introduction UNIT-VI
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.
The iterator and memento patterns
GoF Design Patterns (Ch. 26)
Strategy Design Pattern
Introduction to Design Patterns
Advanced ProgramMING Practices
Advanced ProgramMING Practices
CSC 480 Software Engineering
Activity 1 - Chapter 5 -.
Presentation transcript:

Chapter 5 Patterns and GUI Programming -Part 2-

COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add a container to another container Container should be a component Composite design pattern  Composite method typically invoke component methods  E.g. Container.getPreferredSize invokes getPreferredSize of components

COMPOSITE Pattern Containers and Components The COMPOSITE pattern teaches how to combine several objects into an object that has the same behavior as its parts.

COMPOSITE Pattern Context 1. Primitive objects can be combined to composite objects 2. Clients treat a composite object as a primitive object Solution 1. Define an interface type that is an abstraction for the primitive objects 2. Composite object contains a collection of primitive objects 3. Both primitive classes and composite classes implement that interface type. 4. When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results.

COMPOSITE Pattern

Name in Design PatternActual Name PrimitiveComponent CompositeContainer or a subclass such as JPanel LeafA component that has no children such as JButton or JTextArea method()A method of the Component interface such as getPreferredSize

DECORATOR Pattern Scroll Bars Scroll bars can surround component JTextArea area = new JTextArea(10, 25); JScrollPane pane = new JScrollPane(area); JScrollPane is again a component

DECORATOR Pattern Scroll Bars The DECORATOR pattern teaches how to form a class that adds functionality to another class while keeping its interface.

DECORATOR Pattern Context 1. Component objects can be decorated (visually or behaviorally enhanced) 2. The decorated object can be used in the same way as the undecorated object 3. The component class does not want to take on the responsibility of the decoration 4. There may be an open-ended set of possible decorations

DECORATOR Pattern Solution 1. Define an interface type that is an abstraction for the component 2. Concrete component classes implement this interface type. 3. Decorator classes also implement this interface type. 4. A decorator object manages the component object that it decorates 5. When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

DECORATOR Pattern

Name in Design PatternActual Name Component ConcreteComponentJTextArea DecoratorJScrollPane method()A method of the Component interface. For example, the paint method paints a part of the decorated component and the scroll bars.

How to Recognize Patterns Look at the intent of the pattern (ex1) COMPOSITE pattern: to group component into a whole (ex2) DECORATOR pattern: to decorate a component (ex3) STRATEGYpattern: to wrap an algorithm into a class. Remember common uses (e.g. STRATEGY for layout managers) Not everything that is strategic is an example of STRATEGY pattern Use context and solution as “litmus test”

Litmus Test Can add border to Swing component Border b = new EtchedBorder() component.setBorder(b); Is it an example of DECORATOR?

Litmus Test Component objects can be decorated (visually or behaviorally enhanced) The decorated object can be used in the same way as the undecorated object The component class does not want to take on the responsibility of the decoration There may be an open-ended set of possible decorations PASS FAIL--the component class has setBorder method

Quick Review on Patterns

ITERATOR Pattern The ITERATOR pattern teaches how to access the elements of an aggregate object.

ITERATOR Pattern Context 1. An object (which we’ll call the aggregate) contains other objects (which we’ll call elements). 2. Clients (that is, methods that use the aggregate) need access to the elements. 3. The aggregate should not expose its internal structure. 4. There may be multiple clients that need simultaneous access.

ITERATOR Pattern Solution 1. Define an iterator that fetches one element at a time. 2. Each iterator object needs to keep track of the position of the next element to fetch. 3. If there are several variations of the aggregate and iterator classes, it is best if they implement common interface type. Then the client only needs to know the interface types, not the concrete classes.

ITERATOR Pattern

OBSERVER Pattern The OBSERVER pattern teaches how an object can tell other objects about events. Context 1. An object (which we’ll call the subject) is source of events (such as “my data has changed”). 2. One or more objects (called the observer ) want to know when an event occurs.

OBSERVER Pattern Solution 1. Define an observer interface type. Observer classes must implement this interface type. 2. The subject maintains a collection of observer objects. 3. The subject class supplies methods for attaching observers. 4. Whenever an event occurs, the subject notifies all observers.

OBSERVER Pattern

STRATEGY Pattern The STRATEGY pattern teaches how to supply variants of an algorithm

STRATEGY Pattern Context 1. A class (called context class) can benefit from different variants for an algorithm 2. Clients of the context class sometimes want to supply custom versions of the algorithm Solution 1. Define an interface type that is an abstraction for the algorithm. We’ll call this interface type the strategy. 2. Concrete strategy classes implement the strategy interface type. Each strategy class implements a version of the algorithm. 3. The client supplies a concrete strategy object to the context class. 4. Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object.

STRATEGY Pattern

COMPOSITE Pattern Containers and Components The COMPOSITE pattern teaches how to combine several objects into an object that has the same behavior as its parts.

COMPOSITE Pattern Context 1. Primitive objects can be combined to composite objects 2. Clients treat a composite object as a primitive object Solution 1. Define an interface type that is an abstraction for the primitive objects 2. Composite object contains a collection of primitive objects 3. Both primitive classes and composite classes implement that interface type. 4. When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results.

COMPOSITE Pattern

DECORATOR Pattern Scroll Bars The DECORATOR pattern teaches how to form a class that adds functionality to another class while keeping its interface.

DECORATOR Pattern Context 1. Component objects can be decorated (visually or behaviorally enhanced) 2. The decorated object can be used in the same way as the undecorated object 3. The component class does not want to take on the responsibility of the decoration 4. There may be an open-ended set of possible decorations

DECORATOR Pattern Solution 1. Define an interface type that is an abstraction for the component 2. Concrete component classes implement this interface type. 3. Decorator classes also implement this interface type. 4. A decorator object manages the component object that it decorates 5. When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

DECORATOR Pattern

Review End~!

Putting Patterns to Work Invoice contains line items Line item has description, price Interface type LineItem: Ch5/invoice/LineItem.java Ch5/invoice/LineItem.java Product is a concrete class that implements this interface: Ch5/invoice/Product.java Ch5/invoice/Product.java

Bundles Bundle = set of related items with description+price (ex) stereo system with tuner, amplifier, CD player + speakers A bundle has line items A bundle is a line item  Therefore, COMPOSITE pattern Ch5/invoice/Bundle.java (look at getPrice) Ch5/invoice/Bundle.java

Bundles

Discounted Items Store may give discount for an item Discounted item is again an item DECORATOR pattern Ch5/invoice/DiscountedItem.java (look at getPrice) Ch5/invoice/DiscountedItem.java

Discounted Items

Model/View Separation GUI has commands to add items to invoice GUI displays invoice Yet, we want to decouple input from display Display wants to know when invoice is modified Display doesn't care which command modified invoice  OBSERVER pattern

Observing the Invoice

Change Listeners Use standard ChangeListener interface type public interface ChangeListener { void stateChanged(ChangeEvent event); } Invoice collects ArrayList of change listeners When the invoice changes, it notifies all listeners: ChangeEvent event = new ChangeEvent(this); for (ChangeListener listener : listeners) listener.stateChanged(event);

Change Listeners Display adds itself as a change listener to the invoice Display updates itself when invoice object changes state final Invoice invoice = new Invoice(); final JTextArea textArea = new JTextArea(20, 40); ChangeListener listener = new ChangeListener() { public void stateChanged(ChangeEvent event) { String formattedInvoice = …; textArea.setText(formattedInvoice); } }; invoice.addChangeListener(listener);

Iterating Through Invoice Items Invoice collect line items Clients need to iterate over line items Don't want to expose ArrayList May change (e.g. if storing invoices in database)  ITERATOR pattern

Iterators

Use standard Iterator interface type public interface Iterator { boolean hasNext(); E next(); void remove(); }  remove is "optional operation" (see ch. 8) implement to throw UnsupportedOperationException  implement hasNext/next manually to show inner workings Ch5/invoice/Invoice.java

Formatting Invoices Simple format: dump into text area  May not be good enough OR Invoice on a Web page?  E.g. HTML tags for display in browser  Want to allow for multiple formatting algorithms  STRATEGY pattern

Formatting Invoices

ch5/invoice/InvoiceFormatter.java ch5/invoice/SimpleFormatter.java ch5/invoice/Invoice.java ch5/invoice/InvoiceTester.java ch5/invoice/InvoiceTester.java

Formatting Invoices

Recap of Standardized Patterns

ITERATOR Pattern The ITERATOR pattern teaches how to access the elements of an aggregate object.

ITERATOR Pattern Context 1. An object (which we’ll call the aggregate) contains other objects (which we’ll call elements). 2. Clients (that is, methods that use the aggregate) need access to the elements. 3. The aggregate should not expose its internal structure. 4. There may be multiple clients that need simultaneous access.

ITERATOR Pattern Solution 1. Define an iterator that fetches one element at a time. 2. Each iterator object needs to keep track of the position of the next element to fetch. 3. If there are several variations of the aggregate and iterator classes, it is best if they implement common interface type. Then the client only needs to know the interface types, not the concrete classes.

ITERATOR Pattern

OBSERVER Pattern The OBSERVER pattern teaches how an object can tell other objects about events. Context 1. An object (which we’ll call the subject) is source of events (such as “my data has changed”). 2. One or more objects (called the observer ) want to know when an event occurs.

OBSERVER Pattern Solution 1. Define an observer interface type. Observer classes must implement this interface type. 2. The subject maintains a collection of observer objects. 3. The subject class supplies methods for attaching observers. 4. Whenever an event occurs, the subject notifies all observers.

OBSERVER Pattern

STRATEGY Pattern The STRATEGY pattern teaches how to supply variants of an algorithm

STRATEGY Pattern Context 1. A class (called context class) can benefit from different variants for an algorithm 2. Clients of the context class sometimes want to supply custom versions of the algorithm Solution 1. Define an interface type that is an abstraction for the algorithm. We’ll call this interface type the strategy. 2. Concrete strategy classes implement the strategy interface type. Each strategy class implements a version of the algorithm. 3. The client supplies a concrete strategy object to the context class. 4. Whenever the algorithm needs to be executed, the context class calls the appropriate methods of the strategy object.

STRATEGY Pattern

COMPOSITE Pattern Containers and Components The COMPOSITE pattern teaches how to combine several objects into an object that has the same behavior as its parts.

COMPOSITE Pattern Context 1. Primitive objects can be combined to composite objects 2. Clients treat a composite object as a primitive object Solution 1. Define an interface type that is an abstraction for the primitive objects 2. Composite object contains a collection of primitive objects 3. Both primitive classes and composite classes implement that interface type. 4. When implementing a method from the interface type, the composite class applies the method to its primitive objects and combines the results.

COMPOSITE Pattern

DECORATOR Pattern Scroll Bars The DECORATOR pattern teaches how to form a class that adds functionality to another class while keeping its interface.

DECORATOR Pattern Context 1. Component objects can be decorated (visually or behaviorally enhanced) 2. The decorated object can be used in the same way as the undecorated object 3. The component class does not want to take on the responsibility of the decoration 4. There may be an open-ended set of possible decorations

DECORATOR Pattern Solution 1. Define an interface type that is an abstraction for the component 2. Concrete component classes implement this interface type. 3. Decorator classes also implement this interface type. 4. A decorator object manages the component object that it decorates 5. When implementing a method from the component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.

DECORATOR Pattern