AlexLecture_ObserverPattern

AlexLecture_ObserverPattern - Observer Pattern Recall from...

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

View Full Document Right Arrow Icon
1 Observer Pattern Recall from last time… Reusable classes with dynamic behavior Example: Class Button Issue: How to design Button without knowing class of objects that are to receive “button pressed” message receive button pressed messages Solution: Invented an interface class that declares nothing but a buttonPressed operation Designed class Button with respect to this interface class Requires: – Receiver classes to implement the interface – Receiver objects (targets) register with the Button object 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 Adapter class in running example class MyDocManager : public DocManager, public ButtonListener { public: void buttonPressed( const string& s ) { if(s == “print”) DocManager::printDocument(); } }; Button <<interface>> ButtonListener DocManage target 3 ButtonListener DocManager MyDocManager Configuration code int main(void) { Button p(“print”); MyDocManager dm(…); ddLit (&d) p.addListener(&dm); return FL::run(); // GUI-toolkit event loop } 4
Background image of page 2
3 Qualities of our design Class Button very reusable We can understand the Button—ButtonListener collaboration with little knowledge of Button and no knowledge of the class of the target object Example of separation of concerns Clear mechanism for adapting arbitrary class to implement the ButtonListener interface 5 Exercise: using adapter pattern #include <iostream.h> typedef int Coordinate; typedef int Dimension; /*Legacy component*/ class LegacyRectangle { public: LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2 Coordinate y2 /*Desired interface */ class Rectangle { public: Rectangle(Coordinate x, Coordinate y, Coordinate x2, Coordinate y2) { x1_ = x1; y1_ = y1; x2_ = x2; y2_ = y2; cout << "(" << x1_ << "," << y1_ << ") => (" << x2_ << "," << y2_ << ")" << endl; } void oldDraw() { cout << "(" << x1_ << "," << y1_ << ") => (" << x2_ << "," << y2_ << ")" << endl; } 6 Dimension w, Dimension h): virtual void draw() = 0; }; private: Coordinate x1_; Coordinate y1_; Coordinate x2_; Coordinate y2_; };
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 Solution /*Adapter */ class RectangleAdapter: public Rectangle, public LegacyRectangle { public: RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h): LegacyRectangle(x, y, x + w, y + h){}; virtual void draw() { oldDraw(); } }; 7 void main() { Rectangle *r = new RectangleAdapter(120, 200, 60, 40); r->draw(); } Structure of Adaptor Pattern Structure 1: Multiple Inheritance - A class adapter uses multiple inheritance to adapt one interface to another: Structure 1: Encapsulation - An object adapter relies on object composition: 8
Background image of page 4
5 Which structure to choose?
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

AlexLecture_ObserverPattern - Observer Pattern Recall from...

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

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