Structural Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015.

Slides:



Advertisements
Similar presentations
AP 04/02 Structural Patterns Describe how classes and objects are composed to form larger structures Structural class patterns use inheritance to compose.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Design Patterns David Talby. This Lecture Representing other objects Proxy, Adapter, Façade Re-routing method calls Chain of Responsibility Coding partial.
Structural Design Patterns Yaodong Bi May 22, 2015May 22, 2015May 22, 2015.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 14 Structural Design Patterns SWE 316: Software Design and Architecture.
1 © Wolfgang Pelz UML3 UML 3 Notations describe how to use reusable software. Package Component Deployment Node.
Chapter 8, Object Design Introduction to Design Patterns
Decorator Josh Voils and Ralph Rodkey. GoF Definition of Intent Attach additional responsibilities to an object dynamically Provide an alternative to.
Façade Pattern Jeff Schott CS590L Spring What is a façade? 1) The principal face or front of a building 2) A false, superficial, or artificial appearance.
1 CS 501 Spring 2008 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
1 CS 501 Spring 2007 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
ACM/JETT Workshop - August 4-5, 2005 UML Modeling using MagicDraw UML for Java Programmers.
© SERG Software Design (OOD Patterns) Object-Oriented Design Patterns Topics in Object-Oriented Design Patterns Compliments of Spiros Mancoridis Material.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Design Patterns.
Design Patterns and Graphical User Interfaces Horstmann ,
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
Design Patterns David Talby. This Lecture n Re-Routing Method Calls u Proxy, Chain of Responsibility n Working with external libraries u Adapter, Façade.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
Chapter 8 Structural Design Patterns. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
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.
Structural Design Patterns Yaodong Bi November 12, 2015November 12, 2015November 12, 2015.
Structural Design Patterns
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Introduction to Design Patterns.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
Chapter 8 Structural Design Patterns
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.
Design Patterns Solving problems with already known solutions Unit - 13.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
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.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
CS 5150 Software Engineering Lecture 16 Program Design 3.
Structural Design Patterns Yaodong Bi October 25, 2005.
Chapter Ten The Bridge Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
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.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1/23/2018 Design Patterns David Talby.
Chapter 8 Object Design: Reuse and Patterns.
Describe ways to assemble objects to implement a new functionality
Common Design Patterns
Design Patterns Lecture part 2.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Software Design & Documentation
Structural Design Patterns
Decorator Pattern Intent
Design Patterns אליהו חלסצ'י תכנות מתקדם תרגול מספר 10
UNIT-III Structural Design Patterns
Structural Patterns: Adapter and Bridge
Decorator.
Adapter
Decorator Pattern.
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

Structural Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015

Structural design patterns Façade Façade Decorator Decorator Composite Composite Proxy Proxy Adapter Adapter Bridge Bridge Flyweight Flyweight

Façade Design Purpose – –Provide a single and simple interface to a package of classes Design Pattern Summary – –Define a single and simple interface for clients to use the functionality of the package

Façade - examples A compiler package – –It normally contains many classes/subpackages like Scanner, Parser, etc. – –Most clients only want to compile their programs, i.e., they don’t care about functions of individual components in the package – –Use Façade to provide a simple default interface to most clients.

Client Use op1(), op2(), op3(), and op4() ClassB op1_b() op2_b() Facade op1() op2() op3() op4() ClassC op4_c() ClassA op3_a() Façade - Structure

Sequence Diagram ClientB:ClassB op1()op1_b() op4()op4_c() FacadeA:ClassAC:ClassC op3() op3_a() op2()op2_b()

Client Compiler.compile(“test.c”) Parser Compiler compile() ScannerProgramNodeBuilder Façade - Structure CodeGenerator

– Sample code Façade – Sample code Class Scanner { public Scanner(InputStream sourcecode) public Token scan() { … } public Token scan() { … }} Class Parser { public parse(Scanner s, ProgramNodeBudiler p) { … } public parse(Scanner s, ProgramNodeBudiler p) { … }} Class ProgramNodeBuilder { public ProgramNode newVariable(…) { … } public ProgramNode newAssignment(…) { … } public ProgramNode newReturnStmt(…) { … } } Class CodeGenerator { public void visitStatementNode(…) { … } public void visitExpressionNode(…) { … } } Class Compiler { public void compile(InputStream sc, public void compile(InputStream sc, OutputStream bytecode) { OutputStream bytecode) { Scanner sc = new Scanner(sc); Scanner sc = new Scanner(sc); ProgramNodeBuilder pnb = new ProgramNodeBuilder pnb = new ProgramNodeBuilder(); ProgramNodeBuilder(); Parser parser = new Parser(); Parser parser = new Parser(); parser.parse(sc, pnb); parser.parse(sc, pnb); IntelCodeGenerator cg = new IntelCodeGenerator cg = new IntelCodeGenerator(bytecode) IntelCodeGenerator(bytecode) ProgramNode nodetree = pnb.getRootNode(); ProgramNode nodetree = pnb.getRootNode(); parsetree.traverse(cg); parsetree.traverse(cg);}

framework BankCustomers Client BankCustomers doDeposit( int amt, Customer cust, Account acc ) getBankAccount( Customer cust, int accNum ) getBankCustomer( String custName ) BankCustomerBankAccount Customer getCustomerName() getNumAccounts() getPersonalNote() getAccount( int ) Account getAccountNum() deposit( int ) getBalance() 1..n AccountException CustomerException Façade - Examples

Façade - comments Façade can reduce the degree of dependency between packages – –Packages are dependent on each other only through their facades, not individual classes Use Façade to provide a simple default view of the package that is enough for most clients Façade does not try to encapsulate/hide the components in the package since there may be clients who need to access individual components in the package

Design Purpose – –Add responsibilities to an object at runtime. Design Pattern Summary – –Provide for a linked list of objects, each encapsulating responsibility. Decorator

The word processor example – –A text view may have a border and a scroll bar and maybe other bells and whistles attached to it – –How can those bells and whistles be added to the text view? – –Inheritance? Decorator - examples

Decorator – structure Client comp void operation() { // do actions of the decorator comp.operation(); // do actions of the decorator } Component operation() add(Component) remove(Component) getChild() ConcreteComp operation() Decorator Operation()

Decorator - example

Decorator – structure Client comp super.draw() this.drawBorder() VisualComponent draw() TextView draw() Decorator draw() ScrollDecorator draw() scrollTo() scrollPosition BorderDescrotor draw() drawBorder() borderWidth comp.draw()

:Decorator1 :Decorator2 :ConcreteComp Decorator – examples Client

Decorator - Sequence Diagram Client:Decorator1 operation() Component:Decorator2 :ConcreteComp operation() return

1 Decorator – examples :InputStreamReader System.in:InputStream : BufferedStreamReader :Reader

:InputStreamReader System.in:InputStream : BufferedStreamReader Decorator – examples

Decorator – key concept allows addition to and removal from objects at runtime

Decorator – sample code class VisualComponent { virtual void Draw(); virtual void Resize(); }; class Decorator : public VisualComponent { Decorator(VisualComponent*); void Decorator::Draw () { _component->Draw(); } VisualComponent* _component; }; class BorderDecorator : public Decorator { BorderDecorator(VisualComponent*, int borderWidth); void Draw(){ Decorator::Draw(); DrawBorder(_width); } private void DrawBorder(int); private int _width; }; Class Window { void SetContents (VisualComponent* contents) { //... } Window* window = new Window(); TextView* textView = new TextView; window->SetContents(textView); window->SetContents( new BorderDecorator( new ScrollDecorator(textView), 1 ) ); Window.draw();

Composite Design Purpose – –Represent a Tree of Objects Design Pattern Summary – –Use a Recursive Form in which the tree class aggregates and inherits from the base class for the objects.

leaf node non-leaf node Objects NonLeafNode Component “every object involved is a Component object” “non-leaf nodes have one or more components” Classes 1..n Composite - structure

NonLeafNode doIt() comp Component add( Component ) Remove(component) doIt() LeafNode doIt() TypeANonLeafNode doIt() TypeBNonLeafNode doIt() 1..n Client for all elements e in comp e.do I t() Composite - structure

:ClientN0:NonLeafNode N1:NonLeafNode L1:LeafNode Composite – A Class Diagram L2:LeafNode L3:LeafNode N2:NonLeafNode

:ClientN0:NonLeafNode doIt() N1:NonLeafNodeL1:LeafNode Composite – sequence diagram L2:LeafNode L3:LeafNode N2:NonLeafNode doIt()

Component Composite – examples Container Window ….. component 1..n Canvas Composite in java.awt

Proxy Design Purpose – –Avoid the unnecessary execution of expensive functionality in a manner transparent to clients. Design Pattern Summary – –Interpose a substitute class which accesses the expensive functionality only when required.

BaseActiveClass expensiveMethod() anotherMethod() RealActiveClass expensiveMethod() anotherMethod() Proxy expensiveMethod() anotherMethod() Client if ( realActiveObject == null ) // not loaded yet { realActiveObject = getRealActiveObject(); realActiveObject.expensiveMethod(); } else { realActiveObject.expensiveMethod(); } Instantiate with Proxy object Proxy – examples

expensiveMethod() Proxy – sequence diagram BaseActiveClass RealActiveClassProxy Client create() if needed

Graphics Display() Image display() bitmap ImageProxy display() fileName TexDoc graphics image if ( image == null ) { // not loaded yet image = new Image(fileName); } Image.display(); Instantiate with Proxy object Proxy – examples

Proxy – Sample code Class TextDoc { graphics g; TextDoc(ImageProxy ip) { g = ip; } void display() { g.display();}} Class ImageProxy implements Graphics { FileName fileName; Image image; ImageProxy(FileName fn) { fileName = fn; } display() { if (image == null) if (image == null) image = new Image(fileName); image = new Image(fileName); image.display(); image.display();}} Interface Graphics { display();} Class Image Implements Graphics { Bitmap bitmap; Image(FileName fn) { bitmap = readImage(fn); } display() { // draw the bitmap } readImage(FileName fn) { // read from the file(fn) // create a bitmap }}

Adapter Design Purpose – –Allow an application to use external functionality in a retargetable manner. Design Pattern Summary – –Write the application against an abstract version of the external class; introduce a subclass that aggregate the external class.

Adapter - examples Interact with legacy systems – –When you design a new system which has to interact with a legacy system, you may not want to the new system tightly coupled with (or dependent upon) the legacy system since the legacy system may be replaced in the future. Using 3 rd party systems – –You may want to be able to easily substitute the current 3 rd party system with another one.

Class Adapter

Object Adapter - Structure Client Target +request() Adapter +request() Adaptee +requestedMethod(); adaptee.requestedMethod() adaptee

Adapter – sequence diagram request(TargetIn):TargetOut ClientTargetAdapterAdaptee convert(TargetIn):AdapteeI n requestedMethod(AdapteeIn):AdapteeOut convert(AdapteeOut):TargetOutReturn TargetOut request(TargetIn):TargetOut

Object Adapter – sample code Interface Target { public TargetOut request(TargetIn); public TargetOut request(TargetIn);} Class Adaptee { … public AdapteeOut public AdapteeOut requestedMethod(AdapteeIn) requestedMethod(AdapteeIn) { // produce AdapteeOut; // produce AdapteeOut; return AdapteeOut; return AdapteeOut; } …} Class Adapter implements Target { private Adaptee adaptee; private Adaptee adaptee; public Adapter(Adaptee ad) { public Adapter(Adaptee ad) { adaptee = ad; adaptee = ad; } public TargetOut request(TargetIn ti) { public TargetOut request(TargetIn ti) { AdapteeIn ai = convert(ti); AdapteeIn ai = convert(ti); AdapteeOut ao = AdapteeOut ao = adaptee.requestedMethod(ai); adaptee.requestedMethod(ai); return convert(ao); return convert(ao); } private AdapteeIn convert(TargetIn ti) private AdapteeIn convert(TargetIn ti) { // convert TargetIn to AdapteeIn // convert TargetIn to AdapteeIn } private TargetOut convert(AdapteeOut ao) private TargetOut convert(AdapteeOut ao) { // convert AdapteeOut to TargetOut // convert AdapteeOut to TargetOut }}

Object Adapter - Example DrawingTool Shape +boundingBox() +createManipulator() TextShape +boundingBox() +createManipulator() TextView +getExtent(); text.getExtent() text return new TextManipulator()

Design for Adaption Pluggable Adapters Pluggable Adapters –A small set of operations is specified for adapter & adaptee to implement –Using Abstract Operations Client and target are the same entity Client and target are the same entity Adapter overrides abstract operations to delegate operations to adaptee Adapter overrides abstract operations to delegate operations to adaptee –Using Delegate Objects A delegate interface specifies operations the adapter needs to implement A delegate interface specifies operations the adapter needs to implement

Using Abstract Operations

Using Delegate Objects

Adapter - comments An adapter may have more than one adaptee – –There may not be a one-to-one correspondence between operations of Target and those of Adaptee – –So it is possible that an operation of Target is realized with two separate adaptee classes. The pattern decouples Client from adaptee. – –When a different adaptee is needed, we only need to change to another adapter and the client does not need to change at all.

Structural Patterns - Summary Facade provides an interface to collections of objects Decorator adds to objects at runtime Composite represents trees of objects Proxy avoids calling expensive operations unnecessarily Adapter decouples client from an existing system