DOC PREVIEW
CU-Boulder CSCI 5448 - FACADE & ADAPTER

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© Kenneth M. Anderson, 2011FACADE & ADAPTERCSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGNLECTURE 7 — 02/01/20111Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Goals of the LectureIntroduce two design patternsFacadeAdapterCompare and contrast the two patterns2Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Facade (I)“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.”Design Patterns, Gang of Four, 1995There can be significant benefit in wrapping a complex subsystem with a simplified interfaceIf you don’t need the advanced functionality or fine-grained control of the former, the latter makes life easy3Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Client Facade4Facade Pattern: StructureWednesday, February 2, 2011© Kenneth M. Anderson, 2011Facade (II)Facade works best when you are accessing a subset of the subsystem’s functionalityYo u c a n a l s o a d d n e w f e a t u r e s by a d d i n g i t t o t h e Facade (not the subsystem); still get simpler interfaceFacade not only reduces the number of methods you are dealing with but also the number of classesImagine having to pull Employees out of Divisions that come from Companies that you pull from a DatabaseA Facade in this situation can fetch Employees directly5Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example (Without a Facade)6Database CompanyDivisionEmployeeClientWithout a Facade, Client contacts the Database to retrieve Company objects. It then retrieves Division objects from them and finally gains access to Employee objects.It uses four classes.Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example (With a Facade)7With a Facade, the Client is shielded from most of the classes. It uses the Database Facade to retrieve Employee objects directly.EmployeeClientfind(name): EmployeeDatabase FacadeWednesday, February 2, 2011© Kenneth M. Anderson, 2011Facade Example (I)Imagine a library of classes with a complex interface and/or complex interrelationshipsHome Theater SystemAmplifier, DvdPlayer, Projector, CdPlayer, Tuner, Screen, PopcornPopper (!), and TheatreLightseach with its own interface and interclass dependencies8Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Facade Example (II)Imagine steps for “watch movie”turn on popper, make popcorn, dim lights, screen down, projector on, set projector to DVD, amplifier on, set amplifier to DVD, DVD on, etc.Now imagine resetting everything after the movie is done, or configuring the system to play a CD, or play a video game, etc.9Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Facade Pattern: DefinitionFor this example, we can place high level methods like “watch movie”, “reset system”, “play cd” in a facade object and encode all of the steps for each high level service in the facade; DemoClient code is simplified and dependencies are reducedA facade not only simplifies an interface, it decouples a client from a subsystem of componentsIndeed, Facade lets us encapsulate subsystems, hiding them from the rest of the system10Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Adapters in the Real WorldOur next pattern provides steps for converting an incompatible interface with an existing system into a different interface that isReal World Example: AC Power AdaptersElectronic products made for the USA cannot be used directly with outlets found in most other parts of the worldTo u s e , y o u n e e d a n AC p o we r a d a p t e r11Wednesday, February 2, 2011© Kenneth M. Anderson, 2011OO Adapters (I)Pre-Condition: You are maintaining an existing system that makes use of a third-party class library from vendor AStimulus: Vendor A goes belly up and corporate policy does not allow you to make use of an unsupported class library.Response: Vendor B provides a similar class library but its interface is completely different from the interface provided by vendor AAssumptions: Yo u d o n ’ t w a n t t o c h a n g e yo u r c o d e , a n d yo u c a n ’ t change vendor B’s code.Solution?: Write new code that adapts vendor B’s interface to the interface expected by your original code12Wednesday, February 2, 2011© Kenneth M. Anderson, 2011ExistingSystemVendorBClassLibraryInterface MismatchNeed AdapterAdapterCreate AdapterAnd then...13OO Adapters (II)Wednesday, February 2, 2011© Kenneth M. Anderson, 2011VendorBClassLibraryOO Adapters (III)AdapterExistingSystem...plug it inBenefit: Existing system and new vendor library do not change, new code is isolated within the adapter.14Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example: A turkey amongst ducks! (I)If it walks like a duck and quacks like a duck, then it must be a duck!15Or...Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example: A turkey amongst ducks! (II)If it walks like a duck and quacks like a duck, then it must might be a duck turkey wrapped with a duck adapter… (!)16Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example: A turkey amongst ducks! (III)Recall the Duck simulator from last lecture?public interface Duck {1 public void quack();2 public void fly();3}45public class MallardDuck implements Duck {67 public void quack() {8 System.out.println("Quack");9 }10 11 public void fly() {12 System.out.println("I'm flying");13 }14}151617Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example: A turkey amongst ducks! (IV)An interloper wants to invade the simulatorpublic interface Turkey {1 public void gobble();2 public void fly();3}45public class WildTurkey implements Turkey {67 public void gobble() {8 System.out.println("Gobble Gobble");9 }10 11 public void fly() {12 System.out.println("I'm flying a short distance");13 }14 15}161718Wednesday, February 2, 2011© Kenneth M. Anderson, 2011Example: A turkey amongst ducks! (V)Write an adapter, that makes a turkey look like a duckpublic class TurkeyAdapter implements Duck {12 private Turkey turkey;3 4 public TurkeyAdapter(Turkey turkey) {5 this.turkey = turkey;6 }7 8 public void quack() {9 turkey.gobble();10 }11 12 public void fly() {13 for (int i = 0; i < 5; i++) {14 turkey.fly();15 }16 }17 18}19201. Adapter implements target interface (Duck).2. Adaptee (turkey) is passed via constructor and stored internally3. Calls by client code are delegated to the


View Full Document

CU-Boulder CSCI 5448 - FACADE & ADAPTER

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download FACADE & ADAPTER
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view FACADE & ADAPTER and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view FACADE & ADAPTER 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?