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

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
02 - Structural Design Patterns – 1 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.
Flyweight An Object Structural Design Pattern JM Imbrescia.
Chapter 8, Object Design Introduction to Design Patterns
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0More on Structural Patterns - Page L7-1 PS95&96-MEF-L14-1 Dr. M.E. Fayad Creationa.
Algorithm Programming Structural Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
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.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
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.
SDP Structural Pattern. SDP-2 Structural Patterns Concerned with how classes and objects are composed to form large structures Class Patterns use.
Structural Design Patterns
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 V More Structural Patterns.
02 - Structural Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Design Patterns Structural Patterns. Adapter Convert the interface of a class into another interface clients expect Adapter lets classes work together.
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.
Proxy Design Pattern By:Diksha Agarwal.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
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.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Flyweight 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:
Design Patterns: Structural Design Patterns General and reusable solutions to common problems in software design Software University
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Adapter.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Software Design and Architecture Muhammad Nasir Structural Design Patterns
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Software Design and Architecture
Decorator Design Pattern
More Design Patterns 1.
Chapter 8, Object Design Introduction to Design Patterns
Design Patterns Satya Puvvada Satya Puvvada.
More Design Patterns 1.
Decorator Intent Also known as Wrapper Example: a Text Window
Design Patterns A Case Study: Designing a Document Editor
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
Object Oriented Design Patterns - Structural Patterns
Flyweight Pattern 1.
BRIDGE PATTERN.
Structural Patterns: Adapter and Bridge
Decorator Pattern.
Software Design Lecture : 38.
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

AP 04/02 Structural Patterns Describe how classes and objects are composed to form larger structures Structural class patterns use inheritance to compose interfaces or implementations Structural object patterns describe ways to compose objects to realize new functionality

AP 04/02 ADAPTER (Class, Object Structural) Intent: –Convert the interface of a class into another interface clients expect. –Adapter lets classes work together that could not otherwise because of incompatible interfaces. Motivation: –Sometimes a toolkit class that‘s designed for reuse is not reusable because ist interface does not match the domain-specific interface an application requires

AP 04/02 Linie TextShape DrawingEditor Shape BoundingBox() CreateManipulator() BoundingBox() CreateManipulator() BoundingBox() CreateManipulator() TextView GetExtent() return text ->GetExtent() return new TextManipulator ADAPTER - Motivation

AP 04/02 Applicability Use the Adapter pattern when –you want to use an existing class, and its interface does not match the one you need. –you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don´t necessarily have compatible interfaces. (object adapter only) –you need to use several existing subclasses, but it´s impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

AP 04/02 client Target Adapter SpecificRequest() Request() Adaptee SpecificRequest() (implementation) Class adapter uses multiple inheritance to adapt one interface to another client Target Adapter adaptee->SpecificRequest() Request() Adaptee SpecificRequest() Object adapter relies on composition adaptee ADAPTER - Structure

AP 04/02 Participants and Collaborations Participants: Target (Shape) –Defines the domain-specific interface that client uses Client (DrawingEditor) –Collaborates with objects conforming to the Target interface Adaptee (TextView) –Defines existing interface that needs adapting Adapter (TextShape) –Adapts the interface of Adaptee to the Target interface Collaborations: –Clients call operations on an Adapter instance. In turn, the adapter calls Adapter operations that carry out the request.

AP 04/02 BRIDGE (Object Structural) Intent: –Decouple an abstraction from its implementation so that the two can vary independently Motivation: –Inheritance helps when an abstraction can have multiple possible implementations but is sometimes not flexible enough –The bridge patterns puts an abstraction and its implementation in separate class hierarchies –Example: There is one class hierarchy for Window interfaces (Window, IconWindow, TransientWindow) and a separate hierarchy for platform- specific windows implementations (with WindowImp as root)

AP 04/02 Window XWindowPMWindow Window XWindow PMWindow IconWindow XIconWindow PMIconWindow BRIDGE - Motivation

AP 04/02 Window IconWindow Windowimp DrawText () DrawRect() bridge DevDrawText () DevDrawLine() Imp ->DevDrawLine() DrawBorder() TransientWindow DrawCloseBox() DrawText () DrawRect() XWindowimp DevDrawTest() DevDrawLine() PMWindowimp DevDrawTest() DevDrawLine() XDrawLinie()XDrawString() BRIDGE - Motivation

AP 04/02 Applicability Use the Bridge pattern when: –you want to avoid a permanent binding between an abstraction and its implementation. (when the implementation must be selected or switched at run-time) –both the abstractions and their implementations should be extensible by subclassing. Bridge pattern lets you combine the different abstractions and implementations and extend them independently. –(C++) you want to hide the implementation of an abstraction completely from clients. In C++ the representation of a class is visible in the class interface. –You want to share an implementation among multiple objects (perhaps using reference counting), and this fact should bi hidden from the client.

AP 04/02 Abstraction ConcreteImplementor A Operation() OperationImp() Implementor OperationImp() client RefinedAbstraction imp ->OperationImp(); ConcreteImplementor B OperationImp() imp BRIDGE - Structure

AP 04/02 Participants and Collaborations Participants: Abstraction (Window) –Defines the abstraction’s interface –Maintains a reference to an object of type implementor RefinedAbstraction (IconWindow) –Extends the interface defined by Abstraction Implementor (WindowImp) –Defines interface for implementation class –Not necessarily identical to Abstraction’s interface –Typically provides primitive operations, Abstraction defines higher-level ops. ConcreteImplementor (XWindowImp, PMWindowImp) –Implements the Implementor interface, defines concrete implementation Collaborations: –Abstraction forwards client requests to its Implementor object.

AP 04/02 COMPOSITE (Object Structural) Intent: –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat individual objects and compositions of objects uniformly. Motivation: –Apps often allow grouping of objects into more complex structures –Single implementation could define classes for graphical primitives (Text, Lines) plus other classes that act as containers for primitives –But: code that uses these classes must treat primitive objects and containers differently (even if user treats them identically)

AP 04/02 Linie Picture Graphic Draw() Add(Graphic) Remove(Graphic) GetChild(int) Draw() graphic foall g in graphic g.Draw() add g to list of graphic Rectangle Draw() Test Draw() Add(Graphic) Remove(Graphic) GetChild(int) COMPOSITE - Motivation

AP 04/02 aPicture aLine aRectangle aTextaLineaRectangle A typical composite object structure of recursively composed Graphic objects. COMPOSITE - Motivation

AP 04/02 Applicability Use the Composite pattern when –You want to represent part-whole hierarchies of objects. –You wants clients to be able to ignore the difference between compositions of objects and individual objects. –Clients will treat all objects in the composite structure uniformly.

AP 04/02 Client Composite children foall g in children g.Operation() Leaf Operation() Component Operation() Add(Component) Remove(Component) GetChild(int) Operation() Add(Component) Remove(Component) GetChild(int) COMPOSITE - Structure

AP 04/02 aComposite aLeaf aCompositeaLeaf A typical Composite object structure might look like this: COMPOSITE - Structure

AP 04/02 Participants Component (Graphic) –Declares interface for objects in the composition –Implements default behavior for the interface common to all classes –Declares interface for accessing and managing child components –(optional) defines interface for accessing component’s parent Leaf (rectangle, Line, Text, etc.) –Represents leaf objects in the composition - has no children –Defines behavior for primitive objects in the composition Composite (Picture) –Defines behavior for components having children –Stores child components –Implements child-relate operations in the Component interface Client –Manipulates objects through Component interface

AP 04/02 Collaborations Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.

AP 04/02 DECORATOR (Object Structural) Intent: –Attach additional responsibilities to an object dynamically. –Decorators provide a flexible alternative to subclassing for extending functionality. Motivation: –Sometimes we want to add responsibilities to individual objects, not an entire class –Inheritance is an inflexible (static) solution to the problem. Clients cannot control the way how an object‘s functionality is extended –Enclosing the object into another object that adds the functionality is the more flexible approach - the decorator

AP 04/02 aTextView aScrollDecorator aBorderDecorator Some allplications would benefit from using objects to modes every aspect of their functionality, but a naive design approach would be prohibitively expensive. For example, most document edi- tors modularize their text formatting and editing facilities to some extent. However, they invariably stop short of using objects to represent each character and graphical element in the document. Doing so would promote flexibility at the finest level in the application. Text and graphics could be treated uniformly with aBorderDecorator aScrollDecorator aTextView component Use composition to create a boredered, scrollable text view DECORATOR - Motivation

AP 04/02 TextView Decorator VisualComponent Draw() ScrollDecorator Draw() ScrollTo() compnent -> Draw() Decorator :: Draw(); DrawBorder(); BorderDecorator Draw() DrawBorder() component scrollPosition borderWidth ScrollDecorator and BorderDecorator are subclasses of Decorator, an abstract class for visual components that decorate other visuals. DECORATOR - Motivation

AP 04/02 Applicability Use Decorator –To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects. –For responsibilities that can be withdrawn. –When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.

AP 04/02 ConcreteComponent Decorator Component Operation() ConcreteDecoratorA Operatio() component -> Operation() Decorator :: Operation(); AddedBehavior(); ConcreteDecoratorB Operation() AddedBehavior() addedState component DECORATOR - Structure

AP 04/02 Participants and Collaborations Participants: Component (VisualComponent) –Defines interface for objects that can have responsibilities added to them dynamically ConcreteComponent (TextView) –Defines an object to which additional responsibilities can be attached Decorator –Maintains a reference to a Component object and defines interface that conforms to Component’s interface ConcreteDecorator (BorderDecorator, ScrollDecorator) –Adds responsibilities to the component Collaborations: –Decorator forwards requests to its Component object. –It may optionally perform additional operations before and after forwarding the request.

AP 04/02 FACADE (Object Structural) Intent: –Provide a unified interface to a set of interfaces in a subsystem. –Facade defines a higher-level interface that makes the subsystem easier to use. Motivation: –Structuring a system into subsystems helps reduce complexity. –Minimize communication and dependencies between subsystems. –Facade may provide a single, simplified interface to the more general facilities of a subsystem.

AP 04/02 client classes Subsystem classes Facade FACADE - Motivation

AP 04/02 Compiler subsystem classes Compiler Stream BytecodeStream CodeCenerator StackMachineCodeGeneratorRISCCodeGenerator ScannerToken Parser Symbol ProgramNodeBuilderProgramNode StatementNode ExpressionNode FACADE - Motivation

AP 04/02 Applicability Use the Facade pattern: to provide a simple interface to a complex subsystem. –Subsystems often get more complex as they evolve. when there are many dependencies between clients and the implementation classes of an abstraction. –Introduce a facade to decouple the subsystems from clients and other subsystems, thereby promoting subsystem independence and portability. to layer subsystems. –Use facade to define an entry point to each subsystem level. –Minimize subsystem inter-dependencies

AP 04/02 subsystem classes Facade FACADE - Structure

AP 04/02 Participants and Collaborations Participants: Facade (Compiler) –Knows which subsystem classes may handle a request –Delegates client requests to appropriate subsystem objects Subsystem classes (Scanner, Parser, ProgramNode) –Implement subsystem functionality –Have no knowledge of the facade (i.e.; keep no references to it) Collaborations: –Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). –The facade may have to translate its interface to subsystem interfaces. –Clients do not have to access subsystem objects directly.

AP 04/02 FLYWEIGHT (Object Structural) Intent: –Use sharing to support large numbers of small objects efficiently. Motivation: –Some applications could benefit from using objects throughout their design, but a naïve implementation would be prohibitevly expensive

AP 04/02 apprenta charakter objects row objects column objects OO editors use objects to represent embedded elements like tables and figures But treating characters uniquely (as objects) seems to be too expensive FLYWEIGHT - Motivation

AP 04/02 column row apanretp apanretp a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m Logically - one object per character in the document Physically - one shared flyweight object per character FLYWEIGHT - Motivation

AP 04/02 Applicability The Flyweight pattern‘s effectiveness depends heavily on how and where it‘s used. Apply the Flyweight pattern when all of the following are true: An application uses a large number of objects. Storage costs are high because of the sheer quantity of objects. Most object state can be made extrinsic. Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed. The application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.

AP 04/02 If (flyweight[key] exists) { return existing flyweight; }else{ create new flyweight; add to pool of flyweights; return the new flyweight; } UnsharedConcreteFlyweight FlyweightFactory Operation(extrinsicState) Flyweight Operation(extrinsicState) GetFlyweight(key) flyweights client allState ConcreteFlyweight Operation(extrinsicState) intrinsicState FLYWEIGHT - Structure

AP 04/02 aClient aFlyweightFactory flyweights aConcreteFlyweight intrinsicState aConcreteFlyweight intrinsicState Flyweight pool FLYWEIGHT - Structure

AP 04/02 Participants Flyweight (Glyph) –Declares an interface through which flyweights can receive and act on extrinsic state ConcreteFlyweight (Character) –Implements Flyweight interface and adds storage for intrinsic state –Must be sharable –Any state it stores must be independent of concrete object‘s context FlyweightFactory –Creates and manages flyweight objects –Ensures that flyweights are shared properly Client –Maintains reference to flyweight(s) –Computes or stores the extrinsic state of flyweight(s)

AP 04/02 Collaborations State that a flyweight needs to function must be characterized as either intrinsic or extrinsic. –Intrinsic state is stored in the ConcreteFlyweight object; –extrinsic state is stored or computed by Client objects. –Clients pass this state to the flyweight when they invoke its operation. Clients should not instantiate ConcreteFlyweights directly. –Clients must obtain ConcreteFlyweights objects exclusively from the FlyweightFactory object to ensure they are shared properly.

AP 04/02 PROXY (Object Structural) Intent: –Provide a surrogate or placeholder to control access another object. Motivation: –One reason for controlling access to an object is defer the full cost of its creation and initialization until we actually need to use it. –Consider a document editor that can embed graphical objects into an document - creation of those objects (raster images) can be expensive but opening the document should still be fast. –An image proxy might act as stand-in for the real image.

AP 04/02 aTextDocument image aImageProxy fileName anImage data in memoryon disk PROXY - Motivation

AP 04/02 DocumentEditor Graphic Draw() GetExtent() Store() Load() Image imageImp extent Draw() GetExtent() Store() Load() ImageProxy fileName extent Draw() GetExtent() Store() Load() If (image ==0){ image = LoadImage(fileName); } image ->Draw() If (image ==0) { return extent; } else{ return image -> GetExtent(); } image PROXY - Motivation

AP 04/02 Applicability Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Common situations in which the Proxy pattern is applicable: 1.A remote proxy provides a local representative for an object in a different address space. NeXTSTEP uses the class NXProxy for this purpose. 2.A virtual proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy. 3.A protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights. (KernelProxies in the Choices OS) 4.A smart reference is a replacement for a bare pointer that performs additional actions when an object is a accessed.

AP 04/02 Client Subject Request()... RealSubject Request()... Proxy Request() realSubject ->Request()... realSubject PROXY - Structure aClient aProxy aRealSubject subject Real-subject

AP 04/02 Participants and Collaborations Participants: Proxy (ImageProxy) –Maintains reference to the real subject –Provides interface identical to the real subject –Controls access to subject; manages creation and deletion Subject (Graphic) –Defines common interface for RealSubject and Proxy RealSubject (Image) –Defines the real object that the proxy represents Collaborations: –Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy.