AlexLecture_AdaptorPattern

AlexLecture_AdaptorPattern - Adapter pattern Adaptors 2 1...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Adapter pattern Adaptors 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Problem: How to reuse behavior Suppose we want to design a GUI toolkit with widgets, such as Button , Slider , etc. Problem: Pressing the button should cause some operation to be invoked on some other object But Button must know the class of this other object in order to invoke that operation So how do we make Button reusable? 3 Running example Suppose we are given the class: class DocManager { public: ... void printDocument(); void saveDocument(); ... }; which is responsible for opening, closing, saving, and printing text documents 4
Background image of page 2
3 Running example (continued) Now suppose we want to build a word-processing application: Graphical user interface that allows, among other things, users to save the file by clicking a “Save” button and to print the file by clicking a “Print” button. Details of saving and printing are handled by the class DocManager Key problem: How to design class Button so that its instances can collaborate with instances of class DocManager 5 Running example (continued) print : Button mgr : DocManager target Observe: Two instances and one link: • “print button” object of class Button • “document manager” object of class DocManager • Button press should invoke printDocument() operation 6 Question: How might one design classes Button and DocManager to support this link?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 (Bad) design for class Button class Button { protected: DocManager* target; oid monitorMo se() { void monitorMouse() { if( /* mouse click */ ) { target->printDocument(); } } 7 }; Question: Why is this a bad solution? Problem Observe: To invoke printDocument() operation, Button needed to know the class ( DocManager ) of the target object.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/14/2009 for the course CSE 335 taught by Professor Kraemer during the Fall '08 term at Michigan State University.

Page1 / 12

AlexLecture_AdaptorPattern - Adapter pattern Adaptors 2 1...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online