Design patterns (part 2) CSE 331 University of Washington.

Slides:



Advertisements
Similar presentations
B2PDF b2pdf is the new and innovative release of our powerful command line tool for PDF customization b2pdf is a robust stand alone PDF file generation.
Advertisements

Introduction to Java 2 Programming
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)
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION DESIGN PATTERNS II Autumn 2011.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Communication in Distributed Systems –Part 2
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Abstract Classes and Interfaces
More OOP 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.
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.
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.
CSCI 6962: Server-side Design and Programming Web Services.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CS3773 Software Engineering Lecture 04 UML Class Diagram.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
SCALABLE EVOLUTION OF HIGHLY AVAILABLE SYSTEMS BY ABHISHEK ASOKAN 8/6/2004.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
Structural Design Patterns
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.
CSE 331 Software Design & Implementation Hal Perkins Winter 2012 Design Patterns Part 2 (Slides by Mike Ernst and David Notkin) 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Object Oriented Programming
CS 210 Final Review November 28, CS 210 Adapter Pattern.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns, cont. Based on material by Michael Ernst, University of Washington.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Team Member Assessments Preliminary Round Second and Third can Influence Grade CSE 403, Winter 2011, Alverson, Brun.
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.
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.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
S.Ducasse Stéphane Ducasse 1 Decorator.
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.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Object Oriented Programming Lecture 3: Things OO.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 21 Design Patterns 2.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Abstract Factory Pattern
CSE 403 Design Patterns (Part 2)
The Object-Oriented Thought Process Chapter 1
Structural Design Patterns
Behavioral Design Patterns
CSE 331 Software Design & Implementation
Abstract Factory Pattern
Factory Method, Abstract Factory, and More
Design Patterns A Case Study: Designing a Document Editor
CSE 331 Software Design and Implementation
Jim Fawcett CSE776 – Design Patterns Summer 2003
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Design Patterns - Structural Patterns
CSE 331 Software Design and Implementation
Structural Patterns: Adapter and Bridge
CSE 331 Software Design & Implementation
Presentation transcript:

Design patterns (part 2) CSE 331 University of Washington

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

Structural patterns: Wrappers The 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

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

Adapter example: scaling rectangles interface Rectangle { // grow or shrink this by the given factor void scale(float factor); float getWidth(); float area(); } class myClass { void myMethod(Rectangle r) {... r.scale(2);... } } Goal: be able to use this class instead: class NonScaleableRectangle { // not a Rectangle void setWidth(float width) {... } void setHeight(float height) {... } // no scale method }

Adapting scaled rectangles via subclassing class ScaleableRectangle1 extends NonScaleableRectangle implements Rectangle { void scale(float factor) { setWidth(factor * getWidth()); setHeight(factor * getHeight()); } }

Adapting scaled rectangles via delegation Delegation: forward requests to another object class ScaleableRectangle2 implements Rectangle { NonScaleableRectangle r; ScaleableRectangle2(NonScaleableRectangle r) { this.r = r; } void scale(float factor) { setWidth(factor * r.getWidth()); setHeight(factor * r.getHeight()); } float getWidth() { return r.getWidth(); } float circumference() { return r.circumference(); } }

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

Types of adapter Goal of adapter: connect incompatible interfaces Adapter with delegation Different interfaces Client Implementation Adapter with subclassing Implementation Client Adaptor Adapter with subclassing: no extension is permitted Client Adaptor Implementation Implementation Subclass

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

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 { }

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)

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

Composite pattern Composite permits a client to manipulate either an atomic unit or a collection of units in the same way Good for dealing with part-whole relationships

Composite example: Bicycle Bicycle - Wheel Skewer Hub Spokes Nipples Rim Tape Tube Tire - Frame - Drivetrain -...

Methods on components class BicycleComponent { int weight(); float cost(); } class Skewer extends BicycleComponent { float price; float cost() { return price; } } class Wheel extends BicycleComponent { float assemblyCost; Skewer skewer; Hub hub; float cost() { return assemblyCost + skewer.cost() + hub.cost() +...; } }

Composite example: Libraries Library Section (for a given genre) Shelf Volume Page Column Word Letter interface Text { String getText(); } class Page implements Text { String getText() {... return the concatenation of the column texts... } }

Next time: Traversing composites Goal: perform operations on all parts of a composite