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

Similar documents
Second Midterm Review

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

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

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Command. Comp-303 : Programming Techniques Lecture 22. Alexandre Denault Computer Science McGill University Winter 2004

Lecture 13: Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Topics in Object-Oriented Design Patterns

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

Design Patterns. An introduction

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

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

Design Patterns IV Structural Design Patterns, 1

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

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

SOFTWARE PATTERNS. Joseph Bonello

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

Using Design Patterns in Java Application Development

Tuesday, October 4. Announcements

Design Patterns Reid Holmes

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

Adapter Pattern Structural

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

Design Patterns. Gunnar Gotshalks A4-1

SDC Design patterns GoF

Reuse at Design Level: Design Patterns

Design Patterns V Structural Design Patterns, 2

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

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

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

An Introduction to Patterns

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

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

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

L23.1 Introduction... 2

An Introduction to Patterns

Object-Oriented Oriented Programming Adapter Pattern

CISC 322 Software Architecture

Software Engineering I (02161)

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

DESIGN PATTERN - INTERVIEW QUESTIONS

Design Patterns. Definition of a Design Pattern

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

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Lecture 20: Design Patterns II

The Design Patterns Matrix From Analysis to Implementation

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

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

Coordination Patterns

Object-Oriented Design

Object-Oriented Design

Review Software Engineering October, 7, Adrian Iftene

The Object Recursion Pattern

Design Patterns. CSC207 Winter 2017

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

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

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Software Engineering I (02161)

Design Patterns. CSC207 Fall 2017

Advanced Object Oriented PHP

Introduction to Software Engineering: Object Design I Reuse & Patterns

Crash course on design patterns

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

Applying the Decorator Design Pattern

Object Design II: Design Patterns

Design Patterns Reid Holmes

INTERNAL ASSESSMENT TEST III Answer Schema

VOL. 4, NO. 12, December 2014 ISSN ARPN Journal of Science and Technology All rights reserved.

Design Patterns. CSC207 Fall 2017

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

Design Patterns For Object Oriented Software Development Acm Press

The GoF Design Patterns Reference

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

As a programmer, you know how easy it can be to get lost in the details

Design Patterns in C++

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

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Summary of the course lectures

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Applying the Observer Design Pattern

administrivia today UML start design patterns Tuesday, September 28, 2010

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

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

COURSE 4 DESIGN PATTERNS

Design patterns. OOD Lecture 6

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

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

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

2.1 Design Patterns and Architecture (continued)

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

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

A Rapid Overview of UML

Design Patterns Revisited

Transcription:

Facade and Adapter Comp-303 : Programming Techniques Lecture 19 Alexandre Denault Computer Science McGill University Winter 2004 March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 1

Last lecture... Abstract Factories allow you to use families of objects in a generic way. Factory methods allow you to choose the appropriate subclass at runtime. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 2

Typical Situation I m building an inventory application with a database. The Client class uses the Database, Models and Elements classes. First, a Client object must open a connection to the Database. From the Database, it gets a Model. It then uses a Model to get an Element. Finally, it retrieves the much needed information from Element. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 3

Too many relations! Client A Database Client B Model Element March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 4

Facade Pattern Name : Facade Classification : 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. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 5

Motivation In our example, Client doesn t really need to know about Database, Model or Element. It only needs to retrieve information from Element. Thus, we can build a Facade. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 6

Motivation (cont.) Database Client A DatabaseFacade Model Client B Element March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 7

Applicability The Facade pattern should be used when...... you want to provide a simple interface to a complex subsystem.... there are many dependencies between clients and the implementation classes of an abstraction.... you want to layer your subsystems. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 8

Structure DatabaseFacade Subsystem A Subsystem D Subsystem B Subsystem C March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 9

Participants Facade : knows which subsystem classes are responsible for a request. delegates client requests to appropriate subsystem objects. Subsystem : implement subsystem functionality. handle work assigned by the Facade object. have no knowledge of the Facade ( no reference to it ). March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 10

Collaborations Clients communicate with the subsystem by sending the requests to Facade. Facade forwards the request to the appropriate subsystem. Thought Facade doesn t do any the of the work, it might need to translate the request. Clients that use Facade have no knowledge of the subsystem. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 11

Consequences It shields client from the many objects of the subsystem component. Thus, the subsystem is easier to use. It promotes weak coupling between the client and the subsystem. It doesn t prevent the client from using the subsystem. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 12

Implementation Facade by itself is pretty simple to implement. You must decide if the subsystem should be made of private or public classes. This depends on if you want the client to be able to use the subsystem directly. You can make the Facade an abstract class. Thus, you can subclass it and provide different Facades to different subsystem. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 13

Known Uses If a Facade is well built, the user shouldn t even know it s a Facade. Some subsystem in Java are good candidates for Facades (JDBC). March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 14

Related Patterns Abstract Factory Singleton Mediator March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 15

Something different The Facade is useful, but it doesn t help you when you need polymorphic behavior. For example, let s think of a drawing program like PaintBrush. Implementing objects to draw basic primitives like Line and Circle is not too complicated. We can make those subclasses of an abstract class name Shape. However, drawing text is a little more complicated. Many external libraries are available to draw text. However, how do we preserve our object hierarchy with Shape? March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 16

Adapter Pattern Name : Adapter Classification : Structural Also known as : Wrapper Intent : Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn t otherwise because of incompatible interfaces. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 17

Motivation We can create a class named TextShape which extends shape. This classes will use the external libraries to draw text. It can also provide additional functionalities not found in the external libraries. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 18

Motivation (cont.) Shape TextLib Line Circle TextShape refadaptee: TextLib March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 19

Applicability The Adapter pattern should be used 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.... you need to use several existing subclasses, but it s impractical to adapt their interface by subclassing every one. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 20

Structure Client Target request() Adaptee... Adapter refadaptee: Adaptee request() specialrequest() refadaptee.specialrequest() March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 21

Participants Target : defines the domain-specific interface that Client uses. Client : collaborates with objects conforming to the Target interface. Adaptee : defines an existing interface that needs adapting. Adapter : adapts the interfaces of Adaptee to the Target interface. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 22

Collaborations Clients sends a request to the Adapter. In turn, the Adapter sends that request (maybe after transforming it) to the Adaptee. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 23

Consequences Allows you to use the Adaptee (with the help of the Adapter) as if it were part of your hierarchy. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 24

Implementation The implementation of an adaptor is fairly straightforward. A single Adapter can work with many Adaptees. Another variation of the Adapter pattern is possible when you have multiple inheritance. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 25

Implementation (cont.) Client Target request() Adaptee specialrequest()... Adapter request() specialrequest() March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 26

Known Uses The Java API already has a great number of Adapters built-in. One the most commonly used it the WindowAdapter class. Like Facade, when an Adapter is well built, the user shouldn t even know he s using an Adapter. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 27

Related Patterns Bridge Decorator Proxy March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 28

Facade vs Adapter Both design patterns are very similar. They are often used as wrappers for more complex functions. However, Adapter is used when our wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, Facade is used when we want a easier/simpler interface to work with. March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 29

Summary Facade allows us to hide a complex subsystem, thus reduce coupling and make that subsystem easier to use. Adapter allows us to change the interface of a class, thus allowing us to that class in our hieratic (polymorphic behavior). March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 30

Tool of the day: SableVM SableVM is a portable bytecode interpreter written in C, and implementing the Java virtual machine specification, second edition. Its goals are to be reasonably small, fast, and efficient, as well as providing a well-designed and robust platform for conducting research. SableVM is licensed under the GNU Lesser General Public License (LGPL). It also makes use of a modified version of GNU Classpath. The initial development of SableVM was done as part of the Ph.D. research project of Etienne Gagnon. More information on SableVM is available at: http://www.sablevm.org/ March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 31

References These slides are inspired (i.e. copied) from these three books. Design Patterns, Elements of Reusable Object-Oriented Software; Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides; Addison Wesley; 1995 Java Design Patterns, a Tutorial; James W. Cooper Addison Wesly; 2000 Design Patterns Explained, A new Perspective on Object Oriented Design; Alan Shalloway, James R. Trott; Addison Wesley; 2002 March 23, 2004 Lecture 19 Comp 303 : Facade and Adapter Page 32