Adapter, Bridge, and Façade



Similar documents
Design Patterns. Advanced Software Paradigms (A. Bellaachia) 1

Comp151. Definitions & Declarations

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Database Toolkit: Portable and Cost Effective Software

Structural Design Patterns Used in Data Structures Implementation

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

C++ INTERVIEW QUESTIONS

13 Classes & Objects with Constructors/Destructors

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Construction Principles and Design Patterns. Flyweight, Bridge, Builder

CSI33 Data Structures

Basics of C++ and object orientation in OpenFOAM

Passing 1D arrays to functions.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

An Incomplete C++ Primer. University of Wyoming MA 5310

Programming Languages

Object-Oriented Programming

An Open Framework for Reverse Engineering Graph Data Visualization. Alexandru C. Telea Eindhoven University of Technology The Netherlands.

[Refer Slide Time: 05:10]

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

Basics of I/O Streams and File I/O

Habanero Extreme Scale Software Research Project

Embedded/Real-Time Software Development with PathMATE and IBM Rational Systems Developer

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Java Interview Questions and Answers

ios Design Patterns Jackie Myrose CSCI 5448 Fall 2012

Design with Reuse. Building software from reusable components. Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 14 Slide 1

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

WEB ORIENTED APPLICATIONS GENERATOR

Software Design. Design (I) Software Design Data Design. Relationships between the Analysis Model and the Design Model

Inheritance in Programming Languages

Brent A. Perdue. July 15, 2009

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Common Beginner C++ Programming Mistakes

RUP Design. Purpose of Analysis & Design. Analysis & Design Workflow. Define Candidate Architecture. Create Initial Architecture Sketch

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Programming and Software Development (PSD)

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

A deeper look at Inline functions

Conditions & Boolean Expressions

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Common Data Structures

JMulTi/JStatCom - A Data Analysis Toolkit for End-users and Developers

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Managing Variability in Software Architectures 1 Felix Bachmann*

Sources: On the Web: Slides will be available on:

Collections and iterators

1. Polymorphism in C++...2

OpenACC Basics Directive-based GPGPU Programming

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Programming with Data Structures

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

El Dorado Union High School District Educational Services

The While Loop. Objectives. Textbook. WHILE Loops

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

CMSC 202H. ArrayList, Multidimensional Arrays

CSC230 Getting Starting in C. Tyler Bletsch

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Class 16: Function Parameters and Polymorphism

URI and UUID. Identifying things on the Web.

JavaPOS TM FAQ. What is an FAQ? What is JavaPOS?

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

The C Programming Language course syllabus associate level

Assignment # 2: Design Patterns and GUIs

Monitoring of Tritium release at PTC.

Abstract

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Objects for lexical analysis

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Ch 7-1. Object-Oriented Programming and Classes

The Use of Hardware Abstraction Layers in Automated Calibration Software

Introduction to Data Structures

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Core J2EE Patterns, Frameworks and Micro Architectures

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

CS 241 Data Organization Coding Standards

Twin A Design Pattern for Modeling Multiple Inheritance

Using EDA Databases: Milkyway & OpenAccess

Built-in Contract Testing in Model-driven, Component- Based Development

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Sequential Program Execution

Compiler I: Syntax Analysis Human Thought

Design Patterns for Complex Event Processing

ELEG3924 Microprocessor Ch.7 Programming In C

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

A Software and Hardware Architecture for a Modular, Portable, Extensible Reliability. Availability and Serviceability System

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Transcription:

CHAPTER 5 Adapter, Bridge, and Façade Objectives The objectives of this chapter are to identify the following: Complete the exercise in class design. Introduce the adapter, bridge, and façade patterns. Create basic UML diagrams for these design patterns. Software Development Methods 1

Class Exercise After getting into groups of 3-4, please draw a class diagram, including relationships and multiplicity, of the following scenario: A university library system wants a system that meets the following criteria: library holdings include books, periodicals, and videos. books and videos circulate, but periodicals don t. patrons can check out materials that circulate. patrons may be undergraduate students, graduate students, or faculty. patrons may perform searches on items by author or title. undergraduates may check out 10 items, graduate students may check out 30 items, and faculty may check out an unlimited number of items. the system can report all of the items checked out by a particular patron. the system can report on all items that are overdue. Your diagram should include all relevant classes including those for supporting whatever design patterns you choose. Bridge Pattern Use the Bridge pattern to: Avoid binding an abstraction to its implementation. We ll see what this means when we look more closely at and extend the SortAlgorithm class from last week. Allow both abstractions and implementations to be extensible by subclassing. Make changes to an implementation that doesn t impact the client. This is just another way of saying that we have programmed to an interface and not to an implementation. Share an implementation across multiple objects and hide this fact from the client. 2 Software Development Methods

Bridges have several participants: An Abstraction class which declares an interface an maintains a reference to an Implementor. A Refined Abstractor which extends the Abstraction interface. An Implementor defines the interface for implementation classes. This interface need have no relationship or similarity to the Abstraction interface. A Concrete Implementor that implements the Implementor interface. The results we gain from implementing a Bridge pattern are: Decoupling of an interface and an implementation. This allows an abstraction to avoid binding itself permanently to a specific implementation. An implementation might be derived and used at run-time rather than compile time which increases the maintainability and potential reuse of our classes. Improved extensibility. The abstraction and its implementation may be extended separately. Hidden implementations. The client need not know of what the implementation does behind the scenes. The client communicates with the abstraction and only the abstraction worries about its implementation. To illustrate this design pattern, consider some of the sorting algorithms that were discussed last week, specifically the Quick Sort algorithm. Recall that with Quick Sort you must choose a pivot element and perform sorting on each side of the pivot. It turns out that there are multiple ways of choosing that pivot element. As a general rule: Choose a simple pivot scheme when the array is small. Why? Choose a more complex pivot when the array is larger. Why? QuickSort Class Recall that last week we used the InsertionSort class. This week we ll focus on the QuickSort class. One implementation might appear as: class QuickSort : public SortAlgorithm { public: void sort(array & a, int lo, int hi); The Java equivalent might appear as: class QuickSort extends SortAlgorithm { public void sort(array a, int lo, int hi) {... Software Development Methods 3

If we were now to go ahead and code the QuickSort algorithm we might get something like: void QuickSort::sort(Array & a, int lo, int hi) { if (hi <= low) { int i = part(a, lo, hi); sort(a, lo, hi - 1); sort(a, lo + 1, hi) We will also need some kind of partition algorithm. This can easily be implemented as another class method: int QuickSort::part(Array & a, int l, int r) {... Notice the potential problem here. We have embedded the implementation of the partition logic as a method in the class. This means we have only one possible way to partition our data set. But since what we want is to be able to use different implementations of the partition depending on the characteristics of our data, we need to make some changes. We could: Subclass the QuickSort class and override the partition method. This would work but might lead to a series of different classes which differ only in their partitioning scheme. In this case, subclassing might be a case of using a sledgehammer to swat a fly. Use the Bridge pattern to replace the concrete implementation of the partition method with an object. Needless to say, this is the option we re going to use. Revised SortAlgorithm Class We first need to tweak the SortAlgorithm class a bit: 4 Software Development Methods

template <class Item> public class SortAlgorithm { protected: const Implementor * option; public: SortAlgorithm(const Implementor * op = 0); void setoption(const Implementor * op); bool lessthan(const Item & a, const Item & b) const { if (option) return option->lessthan(a, b); else return (a < b); The Java equivalent might appear as: public class SortAlgorithm { protected final Implementor option; public SortAlgorithm(Implementor op) { option = op; public void setoption(implementor op) { option = op; public boolean lessthan(object a, Object b) { if (option!= null) return (option.lessthan(a, b)); else return (a < b); Implementor Class The next class we ll build will be the implementor which is used as the base class to our implementation. Notice that this is similar to a Strategy pattern save that with a strategy, the entire strategy class is extended whereas with a Bridge pattern, the abstraction and its implementor may be extended separately. Our abstract implementor might appear as: template <class Item> class Implementor { public: virtual bool lessthan(const Item &, const Item &) const = 0; The Java equivalent might appear as: Software Development Methods 5

public abstract class Implementor { public boolean lessthan(object a, Object b) {... Concrete Implementor Now we can actually write the code for the lessthan method of the Implementor. A C++ version might appear as: template <class Item> class Pivot : public Implementor { protected: bool sortasc, ignorecase; public: Pivot(bool so = true, bool ic = true) : sortasc(so), ignorecase(ic) {... bool lessthan(const Item &, const Item &) const; The Java implementation might appear as: public class Pivot extends Implementor { protected boolean sortasc; protected boolean ignorecase; public Pivot(bool so, bool ic) { sortasc = so; ignorecase = ic; bool lessthan(object a, Object b) {... And now we can provide an implementation to the lessthan method which will be used by our sort algorithm to make its determinations: template <class Item> bool lessthan (const Item & a, const Item & b) const { if (sortasc) return (a < b); else return (b < a); 6 Software Development Methods

The corresponding Java code might appear as: boolean lessthan (Object a, Object b) { if (sortasc) return (a < b); else return (b < a); Now we can see how some client code might make use of these classes: int main(int argc, char * argv[]) { Sort * sort = new QuickSort(); Implementor * option = new Pivot(); ArrayList input; cin >> input; sort->setoption(option); sort->sort(input); cout << input; Thus when the sort algorithm s sort method is called, it will begin comparing elements using the implementation defined by the associated option implementor. Conceivably this means that the sort mechanism and option could be read from the command line and created a run time; you could sort the same data set different ways without forcing a recompilation of your program. It would also be feasible to make each Pivot or SortAlgorithm a Singleton since you may never need more than one such object at a time. Adapter Pattern Sometimes toolkits or other classes that are purchased off-the-shelf or that come with other products are not compatible with your own internal classes due to differences in the interface. Your options in this case are somewhat limited. You d like to take advantage of the libraries provided by your vendor, but you don t want your own classes to become dependent upon that vendor s specifications (e.g. what happens if you switch platforms). This is where the Adapter pattern can be useful. Use the Adapter pattern to: Use an existing class whose interface doesn t match what you need. Create a reusable class that cooperates with unrelated or unforeseen classes that may not have a compatible interface. Software Development Methods 7

Adapters have several participants: A Target which defines the domain-specific interface that the Client uses. A Client which collaborates with objects conforming to the Target interface. An Adaptee which defines an existing interface that needs to be adapted. An Adapter which adapts the Adaptee to the Target interface. Façade We have already seen an example of the façade pattern. A façade is simply a way to provide a simple interface to a complicated process. When you use the compile option of your compiler, it is a façade that hides a myriad of details behind the scenes. Use the façade pattern to: Provide a simple interface to a complex subsystem. Layer the various subsystems. Decouple clients from subsystems and subsystems from one another. This is a common idea behind APIs and layered architectures. Façades have two participants: A Façade which knows which subsystem classes are required to satisfy a given request and is able to delegate client requests to the appropriate subsystem. The Subsystem Classes which implement the bulk of the functionality. These classes can handle requests issued to them from the façade but yet have no knowledge of the existence of such a façade. Design Pattern Consequences The overall consequences of using the various design patterns may be summarized as follows: Clients only need to know about a general interface with which they will be interacting; they remain unaware of the concrete classes working behind the scenes. Flexibility is increased because there are no direct dependencies between a class interface and its implementation. 8 Software Development Methods