190 likes | 352 Views
פרק 6 DIP. דוגמאות נוספות. Dependency Inversion Principle (DIP) עיקרון שינוי(היפוך) תלות. מודול בשכבה מסויימת לא יהיה תלוי ישירות בשכבה נמוכה יותר, התלות בניהם תהיה באמצעות הפשטה. הגדרות לתיכון גרוע. The TNTWI-WHDI Criterion
E N D
פרק 6DIP דוגמאות נוספות תיכון תוכנה ד"ר ר' גלנט / י' לויאן
Dependency Inversion Principle (DIP) עיקרון שינוי(היפוך) תלות מודול בשכבה מסויימת לא יהיה תלוי ישירות בשכבה נמוכה יותר, התלות בניהם תהיה באמצעות הפשטה. תיכון תוכנה ד"ר ר' גלנט / י' לויאן
הגדרות לתיכון גרוע • The TNTWI-WHDI Criterion -“Why’d you do it that way?” “That’s not the way I would have done it” • Rigidity - קשיחות. שינוי במקום מסויים ישפיע על יותר מדי מקומות נוספים. • Fragility - שבירות. שינוי במקום מסויים יגרום לליקויים במקומות בלתי צפויים. • Immobility – אי-ניידות. קשה להעביר מיישום מסויים ליישום אחר בגלל שהוא 'תפור' עבור היישום הראשון. תיכון תוכנה ד"ר ר' גלנט / י' לויאן
outputDevice copy c c read Keyboard write Printer דוגמא: copyייצוג ב Structure Chartשל תוכנית פרוצדואלית בלי DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: copyמימוש בלי DIP outputDevice copy //The Copy Program void copy() { int c; while ((c = readKeyboard()) != EOF) writePrinter(c); } // The “Enhanced” Copy Program enum outputDevice {PRINTER, DISK}; void copy(outputDevice dev) { int c; while ((c = readKeyboard()) != EOF) if (dev == PRINTER) writePrinter(c); else writeDisk(c); } c c read Keyboard write Printer // Copy using stdio.h #include <stdio.h> #include Copy.h void copy() { int c; while((c = getchar()) != EOF) putchar(c); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: copyייצוג ב OMDשל תוכנית מונחית עצמים(לפני השימוש ב –DIP ) תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: copy(OMD לאחר השימוש בDIP ) תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: copy(תוכנית לאחר השימוש בDIP ) // The OO Copy Program class IReader { public: virtual int read() = 0; }; class IWriter { public: virtual void write(char) = 0; }; class Copier { public: void copy() { int c while((c=itsIReader->read()) != EOF) itsIWriter->write(c); } }; תיכון תוכנה ד"ר ר' גלנט / י' לויאן
ארכיטקטורה בשכבות "מבנה טוב של ארכיטקטורת תוכנה הוא: ארכיטקטורה שכבתית כאשר כל שכבה מספקת קבוצה קוהרנטית של שירותים (על ידי ממשקים מוגדרים היטב)" [Grady Booch, Object Solutions, Addison Wesley, 1996, p. 54.] תיכון תוכנה ד"ר ר' גלנט / י' לויאן
ארכיטקטורה שכבתית Package Structure תיכון תוכנה ד"ר ר' גלנט / י' לויאן
ארכיטקטורה שכבתית בלי DIP Class Structure תיכון תוכנה ד"ר ר' גלנט / י' לויאן
ארכיטקטורה שכבתית עם DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: 'כפתור ונורה' Collaboration Diagram OMD לפני השימוש ב-DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: 'כפתור ונורה'מימוש לפני השימוש ב-DIP // file Button.cpp --------------- #include “Button.h” #include “Lamp.h” void Button:: detect() { bool isButtonOn = getPhysicalState(); if (isButtonOn) itsLamp->turnOn(); else itsLamp->turnOff(); }; //file “Button.h” #ifndef Button_H #define Button_H class Lamp; class Button { public : Button (Lamp& l); void detect(); bool getPhysicalState(); protected: Lamp * itsLamp; }; #endif // file Lamp.h #ifndef Lamp_H #define Lamp_H class Lamp { public: void turnOn(); void turnOff(); }; #endif תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: 'כפתור ונורה'OMDעם השימוש ב-DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: 'כפתור ונורה'מימוש עם השימוש ב-DIP // file: IButtonServer.h #ifndef IButtonServer_H #define IButtonServer_H class IButtonServer { public: virtual void turnOn() = 0; virtual void turnOff() = 0; }; #endif // file Button.h #ifndef IButtonH #define IButtonH class IButtonServer; class Button { public: Button(IButtonServer& aButtonServer); void detect(); virtual bool getState() = 0; protected: IButtonServer * itsIButtonServer; }; #endif // file Button.cpp #include “Button.h” #include “IButtonServer.h” Button::Button(IButtonServer& ButtonServer): itsIButtonServer ((ButtonServer *) & aButtonServer){ } void Button:detect() { bool isButtonOn = tState() if (isButtonOn) itsIButtonServer->turnOn(); else itsIButtonServer->turnOff(); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן
דוגמא: 'כפתור ונורה'מימוש עם השימוש ב-DIP (המשך) //File Lamp.h #ifndef Lamp_H #define Lamp_H #include “IButtonServer.h” class Lamp : public IButtonServer { public: virtual void turnOn(); virtual void turnOff(); }; #endif // File ButtonImplement.h #ifndef ButtonImplement_H #define ButtonImplement_H #include “Button.h” class ButtonImplement : public Button { public: ButtonImplementation (IButtonServer& aButtonServer) virtual bool getState(); }; #endif // File ButtonImplement.cpp #include “ButtonImplement.h” ButtonImplement::ButtonImplement (IButtonServer& aButtonServer) : Button ((ButtonServer *) &aButtonServer) {} תיכון תוכנה ד"ר ר' גלנט / י' לויאן
תרשים OMD עם object adapter נדרש כאשר יש תוכנת צד שלישי שאינה תומכת בממשק של ה Server תיכון תוכנה ד"ר ר' גלנט / י' לויאן
מימוש עם adapter //File LampAdapter.h #ifndef LampAdapter_H #define LampAdapter_H #include “IButtonServer.h” class Lamp; class LampAdapter : public IButtonServer { public: LampAdapter (Lamp & aLamp); virtual void turnOn(); virtual void turnOff(); Lamp * itsLamp; }; #endif //File LampAdapter.cpp #include “LampAdapter.h” #include “Lamp.h” LampAdapter::LampAdapter (Lamp & aLamp) : itsLamp( & aLamp) {} LampAdapter::turnOn() {itsLamp->on();} LampAdapter::turnOff() {itsLamp->off();} //File Lamp.h #ifndef Lamp_H #define Lamp_H #include “LampAdapter.h” class Lamp { public: Lamp (); virtual void on(); virtual void off(); }; #endif //File Lamp.cpp #include “Lamp.h” Lamp::Lamp () { new LampAdapter( *this); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן