Presentation is loading. Please wait.

Presentation is loading. Please wait.

K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract.

Similar presentations


Presentation on theme: "K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract."— Presentation transcript:

1 K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract classes –Separation of concepts Separating/encapsulating cross-cutting operations [Visitors] Mixin classes Reusable strategies and object-oriented frameworks –Reusable collaborations Software architecture and larger design issues Software process issues

2 K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract classes –Separation of concepts Separating/encapsulating cross-cutting operations [Visitors] Mixin classes Reusable strategies and object-oriented frameworks –Reusable collaborations Software architecture and larger design issues Software process issues

3 K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 6: Reusable, extensible collaborations Topics: – “Reusable units” of behavior – Interface classes – Readings from Gamma book: Observer and Adapter patterns – Homework 4 available on web

4 K. Stirewalt CSE 335: Software Design Problem: How to reuse behavior Suppose we want to design a GUI toolkit with widgets, such as Button, Slider, etc. Problem: –Class Button not very interesting by itself –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?

5 K. Stirewalt CSE 335: Software Design 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

6 K. Stirewalt CSE 335: Software Design Running example (continued) Now suppose we want to build a word-processing application: –Graphical user interface that allows, among other things, for 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

7 K. Stirewalt CSE 335: Software Design Running example (continued) print : Buttonmgr : DocManager Observe: Two instances and one link: “print button” of class Button “document manager” object of class DocManager Button press should invoke printDocument() operation Question: How might one design classes Button and DocManager to support this link? target

8 K. Stirewalt CSE 335: Software Design (Bad) design for class Button class Button { protected: DocManager* target; void monitorMouse() { … if( /* mouse click */ ) { target->printDocument(); } … } … }; Question: Why is this a bad solution?

9 K. Stirewalt CSE 335: Software Design Problem Observe: To invoke printDocument() operation, Button needed to know the class ( DocManager ) of the target object. However: –Button should not care that target is a DocManager –Not requesting information from target –More like sending a message to say: “Hey, I’ve been pressed!. Go do something!” Question: How can we design Button to send messages to objects of arbitrary classes?

10 K. Stirewalt CSE 335: Software Design Solution: Interface class class ButtonListener { public: virtual void buttonPressed( const string& )=0; }; Declares the messages an object must be able to handle to collaborate with a Button Defn: abstract class w/ nothing but pure virtual functions

11 K. Stirewalt CSE 335: Software Design Better design of class Button class Button { public: Button( const string& lab ) : label(lab), target(0) {} void setListener( ButtonListener* l ) { target=l; } protected: string label; ButtonListener* target; void monitorMouse() { … if( /* mouse click */ ) { if(target) target->buttonPressed(label); } … } }; Collaborator must register interest in press events Collaborator: any object that implements the ButtonListener interface

12 K. Stirewalt CSE 335: Software Design Question Now that class Button has been designed to send messages to instances of the interface class ButtonListener, how do we configure an application in which a button sends messages to a document manager?

13 K. Stirewalt CSE 335: Software Design Naive attempt at configuration... DocManager dm(...); Button print( “ Print ” ); print.setListener(&dm);... Question: Why does this statement fail to compile?

14 K. Stirewalt CSE 335: Software Design Adapter classes Multiple inheritance: Used to “glue” two (or more) existing classes to form a new class Useful for adapting a class (adaptee) to make its instances conform to some target interface: –New (adapter) class inherits publicly from both the adaptee class and the target interface class –Adapter class provides methods for target operations –These methods invoke adaptee operations –Instances of new class conform to both target and adaptee classes!

15 K. Stirewalt CSE 335: Software Design Adapter class in running example class MyDocManager : public DocManager, public ButtonListener { public: void buttonPressed( const string& s ) { if(s == “ print ” ) DocManager::printDocument(); } }; Button > ButtonListener DocManager MyDocManager target

16 K. Stirewalt CSE 335: Software Design Correct configuration int main(void) { Button printButton( “ print ” ); MyDocManager docMgr( … ); printButton.setListener(&docMgr); … return FL::run(); // GUI-toolkit event loop }

17 K. Stirewalt CSE 335: Software Design Qualities of this design Class Button very reusable We can understand the Button — ButtonListener collaboration with little knowledge of Button and no knowledge of DocManager –Example of separation of concerns Clear mechanism for adapting arbitrary class to implement the ButtonListener interface Program structured differently from programs that “compute a function”

18 K. Stirewalt CSE 335: Software Design Abstraction Defn: Process by which we identify the important aspects of a phenomenon and ignore its details Example: –STL class vector vs bounded sequence –The latter is an abstraction Two popular abstractions over objects: –Classes: groups set of objects with same characteristic –Collaborations: groups set of sets of objects that collaborate by sending messages back and forth to achieve some goal or purpose


Download ppt "K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract."

Similar presentations


Ads by Google