CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
Chapter 22 Object-Oriented Design
Slide 7E.1 Copyright © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. An Introduction to Object-Oriented Systems Analysis and Design with.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Façade Design Pattern (1) –A structural design pattern.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
ADAPTER PATTERN BY Sravanthi Karumanchi. Structure Pattern Structure patterns are concerned with how classes and objects are composed to form large structures.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
CS 350 – Software Design An Introduction to Design Patterns – Chapter 5 A proposition behind design patterns is that quality of software systems can be.
SOFTWARE DESIGN AND ARCHITECTURE
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Computer Science 313 – Advanced Programming Topics.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 0 CSE3311 – Software Design Adapter Pattern.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Test Isolation and Mocking Technion – Institute of Technology Author: Gal Lalouche © 1 Author: Gal Lalouche - Technion 2015 ©
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.
More Design Patterns From: Shalloway & Trott, Design Patterns Explained, 2 nd ed.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 350 – Software Design A Problem That Cries Out for Flexible Code – Chapter 3 Imagine you want to extract information from a CAD system to feed an expert.
Structural Patterns C h a p t e r 4 – P a g e 55 StructuralPatterns Design patterns that describe how classes and objects can be combined to form larger.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
CS 350 – Software Design The Facade Pattern – Chapter 6 Many design patterns are catalogued in the “Gang of Four” text. I find their definitions not to.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Module 11: Polymorhism #1 2000/01Scientific Computing in OOCourse code 3C59 Module 11: Polymorphism and virtual methods In this module we will cover Polymorphism.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: Brief Examples
Unit II-Chapter No. : 5- design Patterns
Objects First with Java A Practical Introduction using BlueJ
Inheritance Based on slides by Ethan Apter
Design Patterns Lecture part 2.
By SmartBoard team Adapter pattern.
Software Design and Architecture
UNIT 3 – LESSON 5 Creating Functions.
Adapter Design Pattern
CS 350 – Software Design The Facade Pattern – Chapter 6
Adapter Pattern 1.
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Design Patterns - Structural Patterns
DESIGN PATTERNS : State Pattern
CS 350 – Software Design Principles and Strategies – Chapter 14
Objects First with Java A Practical Introduction using BlueJ
Lecture taken from “Design Patterns Explained” by Addison Wesley
CS 350 – Software Design Singleton – Chapter 21
Structural Patterns: Adapter and Bridge
Introduction to Design Patterns
Adapter Design Pattern
The Adapter Pattern.
Objects First with Java A Practical Introduction using BlueJ
Presentation transcript:

CS 350 – Software Design The Adapter Pattern – Chapter 7 Gang of Four Definition: Convert the interface of a class into another interface that the client expects. Adapter lets classes work together that could not otherwise because of incompatible interfaces. Let’s look at a simple example: Imagine you’ve been given the following requirements: Create classes for points, lines, and squares that have the behavior display. Create classes for points, lines, and squares that have the behavior display. The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. So although the system has points, lines, and squares, the client objects only think they have shapes.

CS 350 – Software Design The Adapter Pattern – Chapter 7 This is a problem that cries out for polymorphism. At your level of programming knowledge, it shouldn’t be hard to imagine how to develop such a program (given that you have an object to draw points on the screen). We need to create a shape class and have the concrete classes of point, line, and square derive from shape as in the following figure:

CS 350 – Software Design The Adapter Pattern – Chapter 7 We define a series of behaviors (methods) that all Shapes will have in common in the Shape class and then override their behavior in the concrete classes of Point, Line, and Square. A Shape must be able to: –Set it’s location –Get it’s Location –Display itself –Fill itself –Set it’s color –Undisplay itself A more detailed UML specification is shown to the right:

CS 350 – Software Design The Adapter Pattern – Chapter 7 This works great. We should be very happy with ourselves, shouldn’t we? We created an intuitive solution, that is easy to understand and implement. What could be wrong? Why do you sense I will find something wrong with out elegant solution? What happens if I ask you to draw a circle? How many of you know the mathematics involved in drawing a circle? Could you write the class in 10 minutes, because that’s all the time I am going to give you? If you were slick, you might realize that circle code probably has been written over and over again. Why not lift code from another place? Forget the legal issues. Can you do it? Sure, but the interface of the code you find probably doesn’t match your interface.

CS 350 – Software Design The Adapter Pattern – Chapter 7 So what do you do if you have a great class, but the wrong interface? One solution is to take it apart and use the internals and write a new class that subscribes to your interface. This solution is risky. You could introduce errors. You need to learn more about the existing code, which may not be as trivial as this example. A better solution is to encapsulate the object with another wrapper-like class. In essence ADAPT the existing class to your interface with another class.

CS 350 – Software Design The Adapter Pattern – Chapter 7 So if you have an XXCircle class that handles all the functionality you require but has the following interface: displayIt(); displayIt(); fillIt(); fillIt(); undisplayIt(); undisplayIt();

CS 350 – Software Design The Adapter Pattern – Chapter 7 Create a Circle object that encapsulates XXCircle by making an XXCircle object an attribute of the Circle class.

CS 350 – Software Design The Adapter Pattern – Chapter 7 Some sample code to help you see how the adapter works: Class Circle extends Shape { … private XXCircle myXXCircle; private XXCircle myXXCircle; … public Circle () { public Circle () { myXXCircle = new XXCircle(); myXXCircle = new XXCircle(); } void public display() { void public display() { myXXCircle.displayIt(); myXXCircle.displayIt(); } …}

CS 350 – Software Design The Adapter Pattern – Chapter 7 Key Features of the Adapter Pattern: Intent: Match an existing object beyond your control to a particular interface Problem: A system has the right data and behavior, but the wrong interface. Solution: Provides a wrapper with the desired interface. P & C: Adapters Target (the class it derives from). Allows the Client to use the Adaptee as if it were a type of Target. Consequences: Allows for preexisting objects to fit into new class structures. Implementation: Contain the existing class in another class. Have the containing class match the required interface and call the methods of the contained class.

CS 350 – Software Design The Adapter Pattern – Chapter 7 Differences between Adapter and Façade These patterns are often confused. Here’s a simple chart to show you the differences: FaçadeAdapter Are there preexisting classes?YesYes Is there an interface we MUST design to?NoYes Does an object need to behave polymorphically?NoProbably Is a simpler interface needed?YesNo