More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.

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

Chapter 4 (Horstmann’s Book) Interface Types and Polymorphism: Graphics, Timer, Animation Hwajung Lee.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Patterns & GUI Programming Part 2. 2 Creating a Custom Layout Manager Layout manager determines how components are arranged/displayed in a container.
Graphics Programming with Inheritance Template pattern (Chap 6, se SceneEditor)
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
More design patterns The ADAPTER Pattern Actions and the COMMAND Pattern Actions and the COMMAND Pattern The FACTORY METHOD Pattern The PROXY Pattern The.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
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
Design Patterns David Talby. This Lecture n The rest of the pack u Working over a network F Proxy, State, Chain of Responsibility u Working with external.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
CS 4240: Introduction to Design Patterns Readings: Chap. 5 of Design Patterns Explained Also, Chap. 6 and 7 Also, on-line info on Composite pattern (and.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
11 Games and Content Session 4.1. Session Overview  Show how games are made up of program code and content  Find out about the content management system.
Design Patterns and Graphical User Interfaces Horstmann ,
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
Computer Science 313 – Advanced Programming Topics.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Polymorphism and interfaces Horstmann ch 4. Outline Interface Polymorphism Function object Anonymous class User Interface Action Scope of variables (Large)
In the name of Allah The Proxy Pattern Elham moazzen.
Active-HDL Interfaces Building VHPI Applications C Compilation Course 9.
Dreamweaver MX. 2 Overview of Templates n Forms enable you to collect data from ______. n A form contains ________ such as text fields, radio buttons,
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Computer Science 209 The Proxy Pattern. Delayed Instantiation if (obj == null) obj = It ’ s expensive to load an image If the user never looks at an image,
By Shishir Kumar Contact:
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
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.
Chapter 5 Introduction to Defining Classes
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
More Patterns CS 124. More Basic Patterns Patterns you’ve already seen (without knowing it) Observer / Listener Wrapper Composite Decorator / Filter Patterns.
Week 5, Day 3: Decorator Decorators Muddiest Point Tomorrow: Quiz on lab reading: web.msoe.edu/hasker/se2811/labs/5/ SE-2811 Slide design:
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Broker Design Patterns: Adapters and Proxy.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
Software Design and Architecture Muhammad Nasir Structural Design Patterns
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Examples (D. Schmidt et al)
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Common Design Patterns
Design Patterns Lecture part 2.
Design Patterns with C# (and Food!)
Software Engineering Lecture 7 - Design Patterns
JAVA IO.
Adapter Pattern Context:
Computer Science 209 The Adapter Pattern.
Slide design: Dr. Mark L. Hornick
Software Design Lecture : 38.
Presentation transcript:

More Design Patterns Horstmann ch.10.1,10.4

Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns –Iterator –Observer –Strategy –Template method –Visitor

Adapters Cable adapter: adapts plug to foreign wall outlet OO Programming; Want to adapt class to foreign interface type Example: Add CarIcon to container Problem: Containers take components, not icons Solution: Create an adapter that adapts Icon to Component

Adapter Have Icon Want Component

Adapter public class IconAdapter extends JComponent { private Icon icon; public IconAdapter(Icon i) { icon = i; } public void paintComponent(Graphics g) { icon.paintIcon(this, g, 0, 0); } public Dimension getPreferredSize() { return new Dimension(icon.getIconWidth(), icon.getIconHeight()); } }

The ADAPTER Pattern Context You want to use an existing class (adaptee) without modifying it. The context in which you want to use the class requires target interface that is different from that of the adaptee. The target interface and the adaptee interface are conceptually related. Solution Define an adapter class that implements the target interface. The adapter class holds a reference to the adaptee. It translates target methods to adaptee methods. The client wraps the adaptee into an adapter class object.

The ADAPTER Pattern

Icon paintComponent() JComponent paintIcon() JFrame IconAdapter

The ADAPTER Pattern In stream library Input streams read bytes Readers read characters Non-ASCII encoding: multiple bytes per char System.in is a stream What if you want to read characters? Adapt stream to reader InputStreamReader Reader r1 = new InputStreamReader(System.in); Reader r2 = new InputStreamReader(System.in,"ISO ");

The ADAPTER Pattern InputStream read() Reader read() InputStreamReader

QUIZ We have list, but we want stack. Can we make adapter? 1.No, it is not possible 2.Yes, it is possible, but UML diagram is wrong 3.Yes, it is possible and UML diagram is correct 4.I don’t know

Proxies Proxy: a person who is authorized to act on another persons behalf Example: Delay instantiation of object Expensive to load image Not necessary to load image that user doesn't look at Proxy defers loading until user clicks on tab

Image Loading Simple approach: load image files from web address JTabbedPane tabbedPane = new JTabbedPane(); for (int i=100; i<200; i++) { URL url = new URL(" JLabel label = new JLabel( new ImageIcon(url) ); tabbedPane.add(i, label); } All 100 pictures are loaded before the first is displayed May be slow May overflow memory

Deferred Image Loading Simple image loading: JLabel label = new JLabel( new ImageIcon(url) ); Using proxy: JLabel label = new JLabel( new ImageProxy(url) ); paintIcon loads image if not previously loaded public void paintIcon(Component c, Graphics g, int x, int y) { if (image == null) image = new ImageIcon(url); image.paintIcon(c, g, x, y); } Picture is fetched only if/when needed

The PROXY Pattern Context A class (the real subject) provides a service that is specified by an interface type (the subject type) There is a need to modify the service in order to make it more versatile. Neither the client nor the real subject should be affected by the modification. Solution Define a proxy class that implements the subject interface type. The proxy holds a reference to the real subject, or otherwise knows how to locate it. The client uses a proxy object. Each proxy method invokes the same method on the real subject and provides the necessary modifications.

The PROXY Pattern

Icon ImageIconImageProxy paintIcon() JLabel

QUIZ We want to offer an undo-last-operation for queues without messing with the code of existing queue classes. Which pattern is appropriate? 1.Adapter 2.Proxy 3.Decorator 4.Composite 5.None of the above 6.I don’t know

QUIZ Which design pattern is used here? 1.Adapter 2.Composite 3.Decorator 4.Proxy 5.None of the above 6.I don’t know