Design Patterns V Structural Design Patterns, 2

Similar documents
Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Lecture 20: Design Patterns II

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

COSC 3351 Software Design. Design Patterns Structural Patterns (II) Edgar Gabriel. Spring Decorator

SDC Design patterns GoF

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Design Patterns IV Structural Design Patterns, 1

Design Patterns. An introduction

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

COURSE 4 DESIGN PATTERNS

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Design Patterns Reid Holmes

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

SOFTWARE PATTERNS. Joseph Bonello

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns in Python (Part 2)

Design Patterns Lecture 2

Object-Oriented Oriented Programming

Proxy Design Pattern

DESIGN PATTERNS Course 4

26.1 Introduction Programming Preliminaries... 2

Design Patterns Reid Holmes

Topics in Object-Oriented Design Patterns

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Introduction to Software Engineering: Object Design I Reuse & Patterns

Using Design Patterns in Java Application Development

Design Patterns in C++

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

CISC 322 Software Architecture

Tuesday, October 4. Announcements

Design patterns. Jef De Smedt Beta VZW

Structure Patters: Bridge and Flyweight. CSCI 3132 Summer

Summary of the course lectures

DESIGN PATTERN - INTERVIEW QUESTIONS

Object-oriented Software Design Patterns

A Reconnaissance on Design Patterns

Composite Pattern. IV.4 Structural Pattern

Design Patterns: Structural and Behavioural

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Object Oriented Paradigm

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design patterns generic models

An Introduction to Patterns

Coordination Patterns

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Brief Note on Design Pattern

Object-Oriented Design

Software Design COSC 4353/6353 D R. R A J S I N G H

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Review Software Engineering October, 7, Adrian Iftene

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

Chapter 8, Design Patterns Visitor

INSTITUTE OF AERONAUTICAL ENGINEERING

Object-Oriented Concepts and Design Principles

Adapter Pattern Structural

Design Patterns Revisited

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Design Patterns. Softwaretechnik. Matthias Keil. Albert-Ludwigs-Universität Freiburg

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Software Engineering I (02161)

Adapter pattern. Acknowledgement: Freeman & Freeman

Design Patterns. GoF design patterns catalog

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design for change. You should avoid

Application Architectures, Design Patterns

Design Pattern Detection

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Design Patterns. Software Engineering. Sergio Feo-Arenis slides by: Matthias Keil

The Design Patterns Matrix From Analysis to Implementation

Design Patterns Design patterns advantages:

Applying the Observer Design Pattern

Design Patterns. Decorator. Oliver Haase

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

1 Software Architecture

Component ConcreateComponent Decorator ConcreateDecoratorA ConcreteDecoratorB

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Design Process Overview. At Each Level of Abstraction. Design Phases. Design Phases James M. Bieman

Second Midterm Review

Transcription:

Structural Design Patterns, 2 COMP2110/2510 Software Design Software Design for SE September 17, 2008 Department of Computer Science The Australian National University 19.1

1 2 Formal 3 Formal 4 Formal 5 Rest of Structural DPs 6 Structural DPs: pitted against each other 19.2

GoF Structural Design Patterns ADAPTER BRIDGE COMPOSITE DECORATOR FAÇADE FLYWEIGHT PROXY 19.3

Design Pattern Intent: Decouple an abstraction from its implementation so that the two can vary independently. Also known as: HANDLE/BODY When an abstraction can have one of several possible implementations, the usual way to accommodate them is to use inheritance. An abstract class defines the interface to the abstraction, and concrete subclasses implement it in different ways. But this approach isn t always flexible enough. Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend, and reuse abstractions and implementations independently. Use the pattern if: you want to avoid a permanent binding between an abstraction and its implementation, eg, when the implementation must be selected or switched at run-time both the abstractions and their implementations should be extensible by subclassing BRIDGE lets you combine the different abstractions and implementations and extend them independently you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled 19.4

example [the following example is due to Robert C. Martin as discussed in his book Agile Software Development ] Consider a task of writing a software support for different hardware modems which must conform to the required interface (operations like dial(), hangup(), send() and receive()). Consider also, that apart from the hardware implementation variability, there are different communication policies some modems do not require dial and hung-up operations since they are (old fashion) dedicated modems (which connect to dedicated connection, and only require turning them on not a software operation!). The ADAPTER pattern can take care of accommodating different modem communication policies (dial-up and dedicated) to the uniform interface with which the client works. But how to deal with proliferation of hardware types and communication policies: every time a new hardware is added two new classes are required, and every new connection type results into as many new classes as there are types of hardware modems. A better solution separate the hardware hierarchy and communication type hierarchy (the latter determines the user interface) and connect them with a BRIDGE. <<interface>> ModemConnectionController ModemImplementation {abstract} <<delegates>> dial() hangup() send() receive() -implementation implementation.dialimpl() etc SiscoModem dialimpl() etc dialimpl() DedModemController dial() etc DialModemController dial() etc TexasInstrumentsModem dialimpl() dial and hangup are implemented to simulate connection state. send and receive delegate to their respective implementations all methods delegate to their respective implementations 19.5

Formal 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. This interface doesn t have to correspond exactly to Abstraction s interface; in fact the two interfaces can be quite different. Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives ConcreteImplementor implements the Implementor interface and defines its concrete implementation Collaborators Abstraction forwards client requests to its Implementor object 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. It s even possible for an object to change its implementation at run-time (eg, following a run-time policy). The decoupling eliminates compile-time dependencies on the implementation (binary compatibility between different versions of a class library). (+) Improved extensibility: You can extend the Abstraction and Implementor hierarchies independently. (+) Hiding implementation details from clients. 19.6

Builder Usage Additional BRIDGE considerations: you want avoid proliferation of classes (when the interface is determined by one system aspect, and implementation is tied up to another) you want to share an implementation among multiple objects, and this fact should be hidden from the client (eg, String/StringRep class pair by J. Coplien, Advanced C++, 1992) An ABSTRACT FACTORY is used to create and configure a particular BRIDGE The intentional difference between BRIDGE and ADAPTER: the ADAPTER is meant to make unrelated classes to work together, and thus, it s usually introduced into a system at later stage when participating classes are already designed (or brought in); the BRIDGE is used from the very beginning in the design to allow abstraction and implementation vary independently. 19.7

Design Pattern Intent: Provide a unified interface to a set of interfaces in a subsystem. FAÇADE defines a higher-level interface that makes the subsystem easier to use. Use the pattern if: you want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don t need to customize it. A façade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the façade there are many dependencies between clients and the implementation classes of an abstraction. Introduce a façade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability you want to layer your subsystems. Use a façade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other only through façades 19.8

Example: Compiler as Consider a programming environment that gives different applications access to its compiler subsystem. This subsystem contains classes such as Scanner, Parser, ProgramNode, BytecodeStream, and ProgramNodeBuilder that implement the compiler. Some specialized applications might need to access these classes directly. But most clients generally don t care about details like parsing and code generation; they merely want to compile some code powerful but low-level interfaces in the compiler subsystem only complicate their task. To provide a higher-level interface that can shield clients from these classes, the compiler subsystem also includes a Compiler class a unified interface to the compiler s functionality. The Compiler class acts as a façade: It offers clients a single, simple interface to the compiler subsystem. It glues together the classes that implement compiler functionality without hiding them completely. The compiler façade makes life easier for most programmers without hiding the lower-level functionality from the few that need it. 19.9

Formal Participants knows which subsystem classes are responsible for a request delegates client requests to appropriate subsystem objects Subs ystem classes implement subsystem functionality handle work assigned by the object have no knowledge of the façade; that is, they keep no references to it Collaborators: Clients communicate with the subsystem by sending requests to, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the façade may have to do work of its own to translate its interface to subsystem interfaces. If clients use the façade they don t have a direct access to its subsystem objects. 19.10

FAÇADE benefits and caveats It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use. It promotes weak coupling between the subsystem and its clients. Often the components in a subsystem are strongly coupled. Weak coupling lets you vary the components of the subsystem without affecting its clients. s help layer a system and the dependencies between objects. They can eliminate complex or circular dependencies. This can be an important consequence when the client and the subsystem are implemented independently. Reducing compilation dependencies is vital in large software systems. You want to save time by minimizing recompilation when subsystem classes change. Reducing compilation dependencies with façades can limit the recompilation needed for a small change in an important subsystem. A facade can also simplify porting systems to other platforms, because it s less likely that building one subsystem requires building all others. It doesn t prevent applications from using subsystem classes if they need to. Thus you can choose between ease of use and generality. 19.11

Design Pattern Intent: Provide a surrogate or placeholder for another object to control access to it Example: represent an image (or a flash video, or any other web-page component with significant memory footprint) quickly for page formatting purposes before the component itself becomes available. And a possible object diagram of a proxy structure at run-time: 19.12

Formal Participants maintains a reference that lets the proxy access the real subject provides an interface identical to Subject s so that a proxy can by substituted for the real subject controls access to the real subject may be responsible for creating and deleting the real subject Subject defines the common interface for RealSubject and so that a can be used anywhere a RealSubject is expected RealSubject (may coincide with Subject) defines the real object that the proxy represents Collaborators forwards requests to RealSubject when appropriate, depending on the kind of proxy Consequences A remote proxy can hide the fact that an object resides in a different address space (eg, mounted file system) A virtual proxy can perform optimizations such as creating an object on demand Allows additional housekeeping tasks when an object is accessed (when proxies are protection proxies and smart references) Copy-on-write optimization 19.13

Flyweight Design Pattern Intent: Use sharing to support large numbers of fine-grained objects efficiently Applications: An application uses a large number of objects Storage costs are high because of the sheer quantity of objects Most object states can be externalizes (effectively removed as object attribute), so multiple groups of objects may be replaced by relatively few shared objects The application doesn t depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects For example, in Java String objects are managed via FLYWEIGHT all String literals are placed in the class pool. Redundant literals are impossible Java keeps only one copy. 19.14

of Structural DPs ADAPTER and BRIDGE have some common attributes both promote flexibility by providing a level of indirection to another object, and both involve forwarding requests to this object from an interface other than its own. The difference is in intent: the first becomes necessary after you discover that two incompatible classes should work together, the second is applied before the design to allow abstraction to have several implementations, and their potential independent evolution If DECORATOR is designed to let you add responsibilities to objects without subclassing (dynamic embellishment of the object), COMPOSITE s focus is on structuring classes so that many related objects can be treated uniformly, and multiple objects can be treated as one. Its focus is not on embellishment but on representation. Like DECORATOR, PROXY composes an object and provides an identical interface to clients. But PROXY is not concerned with attaching or detaching properties dynamically, and it s not designed for recursive composition. Its intent is to control access to an object by providing a stand-in for a subject when it s inconvenient or undesirable to access the subject directly because, eg, it lives on a remote machine, has restricted access, or is persistent. ADAPTER provides a different interface to the object it adapts. In contrast, PROXY provides the same interface as its subject. However, a proxy used for access protection might refuse to perform an operation that the subject will perform, so its interface may be effectively a subset of the subject s. 19.15