CSE 331 Software Design & Implementation Hal Perkins Winter 2012 Design Patterns Part 2 (Slides by Mike Ernst and David Notkin) 1.

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Slides by Alex Mariakakis with material from David Mailhot, Hal Perkins, Mike Ernst Section 9: Design Patterns.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION DESIGN PATTERNS II Autumn 2011.
Communication in Distributed Systems –Part 2
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Client/Server Software Architectures Yonglei Tao.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Design Patterns Part 3 (Slides by Mike Ernst and David Notkin) 1.
Design Patterns.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
Design Patterns: Structural Design Patterns
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Slides adapted from Alex Mariakakis, with material from David Mailhot, Hal Perkins, Mike Ernst Section 9: Design Patterns.
SCALABLE EVOLUTION OF HIGHLY AVAILABLE SYSTEMS BY ABHISHEK ASOKAN 8/6/2004.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Java Dynamic Proxy Bibliography: reflection/proxy.html
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Object Oriented Programming
Design patterns (part 2) CSE 331 University of Washington.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns, cont. Based on material by Michael Ernst, University of Washington.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Team Member Assessments Preliminary Round Second and Third can Influence Grade CSE 403, Winter 2011, Alverson, Brun.
Secure middleware patterns E.B.Fernandez. Middleware security Architectures have been studied and several patterns exist Security aspects have not been.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
S.Ducasse Stéphane Ducasse 1 Decorator.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 Design Patterns, Part 2 (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal.
Object Oriented Programming Lecture 3: Things OO.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 21 Design Patterns 2.
Section 9: Design Patterns
Chapter 10 Design Patterns.
CSE 403 Design Patterns (Part 2)
Structural Design Patterns
Behavioral Design Patterns
CSE 331 Software Design & Implementation
object oriented Principles of software design
Section 9: Design Patterns
Section 9: Design Patterns
CSE 331 Software Design and Implementation
Object Oriented Design Patterns - Structural Patterns
CSE 331 Software Design and Implementation
Section 9: Design Patterns
Section 9: Design Patterns
Structural Patterns: Adapter and Bridge
CSE 331 Software Design & Implementation
Decorator Pattern.
Section 9: Design Patterns
Adapter Pattern Jim Fawcett
Lecture 8 Object Oriented Programming (OOP)
Adapter Pattern Jim Fawcett
Presentation transcript:

CSE 331 Software Design & Implementation Hal Perkins Winter 2012 Design Patterns Part 2 (Slides by Mike Ernst and David Notkin) 1

Outline Introduction to design patterns Creational patterns (constructing objects)  Structural patterns (controlling heap layout) Behavioral patterns (affecting object semantics) 2

Structural patterns: Wrappers A wrapper translates between incompatible interfaces Wrappers are a thin veneer over an encapsulated class –modify the interface –extend behavior –restrict access The encapsulated class does most of the work PatternFunctionalityInterface Adaptersamedifferent Decoratordifferentsame Proxysame 3

Adapter Change an interface without changing functionality –rename a method –convert units –implement a method in terms of another Example: angles passed in radians vs. degrees 4

Adapter example: scaling rectangles We have this Rectangle interface interface Rectangle { // grow or shrink this by the given factor void scale(float factor);... float getWidth(); float area(); } Goal: we want to use instances of this class to “implement” Rectangle : class NonScaleableRectangle { // not a Rectangle void setWidth(float width) {... } void setHeight(float height) {... } // no scale method... } 5

Adaptor: Use subclassing class ScaleableRectangle1 extends NonScaleableRectangle implements Rectangle { void scale(float factor) { setWidth(factor * getWidth()); setHeight(factor * getHeight()); } 6

Adaptor: use delegation Delegation: forward requests to another object class ScaleableRectangle2 implements Rectangle { NonScaleableRectangle r; ScaleableRectangle2(w,h) { this.r = new NonScaleableRectangle(w,h); } void scale(float factor) { setWidth(factor * r.getWidth()); setHeight(factor * r.getHeight()); } float getWidth() { return r.getWidth(); } float circumference() { return r.circumference(); }... } 7

Subclassing vs. delegation Subclassing –automatically gives access to all methods of superclass –built into the language (syntax, efficiency) Delegation –permits cleaner removal of methods (compile-time checking) –wrappers can be added and removed dynamically –objects of arbitrary concrete classes can be wrapped –multiple wrappers can be composed Some wrappers have qualities of more than one of adapter, decorator, and proxy Delegation vs. composition –Differences are subtle –For CSE 331, consider them to be equivalent 8

Decorator Add functionality without changing the interface Add to existing methods to do something additional (while still preserving the previous specification) Not all subclassing is decoration 9

Decorator example: Bordered windows interface Window { // rectangle bounding the window Rectangle bounds(); // draw this on the specified screen void draw(Screen s);... } class WindowImpl implements Window {... } 10

Bordered window implementations Via subclasssing: class BorderedWindow1 extends WindowImpl { void draw(Screen s) { super.draw(s); bounds().draw(s); } } Via delegation: class BorderedWindow2 implements Window { Window innerWindow; BorderedWindow2(Window innerWindow) { this.innerWindow = innerWindow; } void draw(Screen s) { innerWindow.draw(s); innerWindow.bounds().draw(s); } } Delegation permits multiple borders on a window, or a window that is both bordered and shaded (or either one of those) 11

A decorator can remove functionality Remove functionality without changing the interface Example: UnmodifiableList –What does it do about methods like add and put? 12

Proxy Same interface and functionality as the wrapped class Control access to other objects –communication: manage network details when using a remote object –locking: serialize access by multiple clients –security: permit access only if proper credentials –creation: object might not yet exist (creation is expensive) hide latency when creating object avoid work if object is never used 13