ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns V More Structural Patterns.

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Advertisements

Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Flyweight: Structural Pattern Prepared by Galina Walters.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
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.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Imagine that you need to create a system to represent all of the cars in a city. You need to store the details about each car ( model, and year) and the.
Flyweight An Object Structural Design Pattern JM Imbrescia.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Algorithm Programming Structural Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Proxy Design Pattern (1) –A structural design pattern.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Client/Server Software Architectures Yonglei Tao.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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.
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:
Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla Design Pattern.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
In the name of Allah The Proxy Pattern Elham moazzen.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
Structural Design Patterns
Structural Pattern: Bridge When the abstract interface and the concrete implementation have been set up as parallel class hierarchies, it becomes difficult.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
02 - Structural Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Design Patterns Structural Patterns. Adapter Convert the interface of a class into another interface clients expect Adapter lets classes work together.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
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.
Processes and Virtual Memory
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
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.
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:
CS 5150 Software Engineering Lecture 16 Program Design 3.
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
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.
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
Flyweight Design Pattern
Design Patterns Satya Puvvada Satya Puvvada.
Decorator Intent Also known as Wrapper Example: a Text Window
Flyweight Pattern 1.
BRIDGE PATTERN.
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Software Design Lecture : 38.
Proxy Pattern Definition of “Proxy”: Authority or power to act for another Original Gang of Four pattern, much used Stands in for a “real object” (similar.
Software Design Lecture 10.
Presentation transcript:

ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns V More Structural Patterns

ECE450 - Software Engineering II2 Difficulties opening large documents Consider a document editor that can embed graphical objects in a document –Some graphical objects (say, large images) can be expensive to create... –...but opening a document should be fast! We shouldn’t create all the expensive objects at once when the document is opened –...and we need data from every image (e.g. height and width) from the start! So we have a dilemma: we don’t want to create all expensive objects at once, but we need their information anyway! Alternatives?

ECE450 - Software Engineering II3 The Proxy pattern Proxy: Provide a surrogate or placeholder for another object to control access to it –In our example, the DocumentEditor loads ImageProxy instead –and if we need to Draw() the image, the ImageProxy will load it.

ECE450 - Software Engineering II4 Applicability Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Several possible scenarios: –Remote Proxy: Provide a local representative for an object in a different address space –Virtual Proxy: Create expensive objects on demand (as in our example) –Protection Proxy: Control access to the original object (useful when objects should have different access rights) –Smart Reference: A replacement for “bare” pointers that perform additional actions when objects are accessed: Counting the number of references to the real object so that it can be freed automatically when there are no more references Checking that the real object is locked before it’s accessed to ensure that no other object can change it...

ECE450 - Software Engineering II5 Participants Proxy –Maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same –Provides an interface identical to Subject’s so that a proxy can be substituted for the real subject –Controls access to the real subject and perhaps creates/deletes it –Other responsibilities, depending on the kind of proxy... Remote proxies are responsible for encoding a request and its arguments, and for sending the encoded request to the real subject in a different address space Virtual proxies may cache additional information about the real subject so that they can postpone accessing it. Protection proxies check that the caller has the access permissions required to perform a request

ECE450 - Software Engineering II6 Participants (cont) Subject –Defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected RealSubject –Defines the real object that the proxy represents

ECE450 - Software Engineering II7 Consequences Depending on the type of Proxy, the level of indirection that it introduces leads to different advantages: –A remote proxy can hide the fact that an object resides in a different address space –A virtual proxy can perform optimizations such as creating an object on demand –Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed Another optimization that the Proxy pattern can hide from the client is copy-on-write: –Copying a large object might be expensive, so when the client asks for a copy, we can just copy a proxy. –We postpone the actual copy of the object until the copy is modified Note that although decorators can have similar implementations as proxies, decorators have a different purpose. Decorators add one or more responsibilities to an object, whereas a proxy controls access to an object

ECE450 - Software Engineering II8 Next problem, but still with document editors... Object-oriented document editors typically use objects to represent embedded elements like tables and figures. –However, they stop short of using an object for each character in the document Even though doing so would promote flexibility at the finest levels in the application

ECE450 - Software Engineering II9 Lots of little objects Is it practical to make each character an object? –We could easily have hundreds of thousands of character objects! Each of them representing a character, its location, font, size, style, etc.

ECE450 - Software Engineering II10 But... If we share objects from a pool, handling each character as an object becomes practical –Each “flyweight” object stores a character code, but its coordinate position in the document and its typographic style can be determined from the text layout algorithms The character code is intrinsic state, while the other information is extrinsic Intrinsic state consists of information that is independent of the object’s context, thereby making it shareable Extrinsic state depends on and varies with the context and therefore can’t be shared

ECE450 - Software Engineering II11 Class structure of example This is how the class structure of our example looks like –Glyph is the abstract class for graphical objects, some of which may be “flyweights” –A “flyweight” representing the letter “a” only stores the corresponding character code; it doesn’t need to store its location or font Clients supply the context-dependent information that the flyweight needs to draw itself

ECE450 - Software Engineering II12 The Flyweight pattern Use sharing to support large numbers of fine-grained objects efficiently Applicability: Use 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)

ECE450 - Software Engineering II13 Structure and participants Flyweight –Declares an interface through which flyweights can receive and act on extrinsic state ConcreteFlyweight –Implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight must be shareable.

ECE450 - Software Engineering II14 Structure and participants (cont) UnsharedConcreteFlyweight –Not all Flyweight subclasses need to be shared (e.g. rows and columns). The Flyweight interface enables sharing, it does not enforce it. FlyweightFactory –Creates and manages flyweight objects

ECE450 - Software Engineering II15 Structure and participants (cont) Client –Maintains a reference to flyweights –Computes or stores the extrinsic state of flyweights –Should not instantiate ConcreteFlyweights directly. They must ubtain ConcreteFlyweight objects exclusively from the FlyweightFactory object to ensure they are shared properly

ECE450 - Software Engineering II16 Consequences Flyweights may introduce run-time costs associated with transferring, finding, and/or computing extrinsic state –However, such costs are offset by space savings, which increase as more flyweights are shared Storage savings are a function of several factors: –The reduction in the total number of instances that comes from sharing –The amount of intrinsic state per object –Whether extrinsic state is computed or stored

ECE450 - Software Engineering II17 One more problem Consider the domain of thread scheduling. We have two types of thread schedulers (preemptive and time sliced), and two operating systems (Unix and Windows) –We define a class for each permutation of these two dimensions; we end up with four classes (preemptive-Unix, preemptive-Windows, etc.) –Adding platforms or approaches to thread scheduling increases considerably the number of classes we would need to code (and debug, and maintain...) The Bridge design pattern refactors this explosive inheritance into two orthogonal hierarchies: –One for platform-independent abstractions –One for platform-dependent implementations

ECE450 - Software Engineering II18 The Bridge pattern Decouple an abstraction from its implementation so that the two can vary independently The abstractions (e.g. approaches to thread scheduling) evolve over here... The implementations (e.g. operating systems) evolve over here...

ECE450 - Software Engineering II19 Applicability Use the Bridge pattern when... –You want to avoid a permanent binding between an abstraction and its implementation –Both the abstractions and their implementations should be extensible by subclassing (in this case, the pattern lets you extend them independently) –You have a proliferation of classes as shown in our example

ECE450 - Software Engineering II20 Structure and participants Abstraction –Defines the abstraction’s interface –Maintains a reference to an object of type Implementor RefinedAbstraction –Extends the interface defined by Abstraction Implementor –Defines the interface for implementation classes. The interface does not have to correspond exactly to Abstraction’s interface; in fact the two can be quite different ConcreteImplementor –Implements the Implementor interface

ECE450 - Software Engineering II21 Consequences Decoupling interface and implementation –An implementation is not bound permanently to an interface –The implementation of an abstraction can be configured at run-time –An object could even change its implementation at run-time! Improved extensibility –You can extend the Abstraction and Implementor hierarchies independently Hiding implementation details from clients