1 / 19

פרק 6 DIP

פרק 6 DIP. דוגמאות נוספות. Dependency Inversion Principle (DIP) עיקרון שינוי(היפוך) תלות. מודול בשכבה מסויימת לא יהיה תלוי ישירות בשכבה נמוכה יותר, התלות בניהם תהיה באמצעות הפשטה. הגדרות לתיכון גרוע. The TNTWI-WHDI Criterion

hayes-bauer
Télécharger la présentation

פרק 6 DIP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. פרק 6DIP דוגמאות נוספות תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  2. Dependency Inversion Principle (DIP) עיקרון שינוי(היפוך) תלות מודול בשכבה מסויימת לא יהיה תלוי ישירות בשכבה נמוכה יותר, התלות בניהם תהיה באמצעות הפשטה. תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  3. הגדרות לתיכון גרוע • The TNTWI-WHDI Criterion -“Why’d you do it that way?” “That’s not the way I would have done it” • Rigidity - קשיחות. שינוי במקום מסויים ישפיע על יותר מדי מקומות נוספים. • Fragility - שבירות. שינוי במקום מסויים יגרום לליקויים במקומות בלתי צפויים. • Immobility – אי-ניידות. קשה להעביר מיישום מסויים ליישום אחר בגלל שהוא 'תפור' עבור היישום הראשון. תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  4. outputDevice copy c c read Keyboard write Printer דוגמא: copyייצוג ב Structure Chartשל תוכנית פרוצדואלית בלי DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  5. דוגמא: 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); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  6. דוגמא: copyייצוג ב OMDשל תוכנית מונחית עצמים(לפני השימוש ב –DIP ) תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  7. דוגמא: copy(OMD לאחר השימוש בDIP ) תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  8. דוגמא: 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); } }; תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  9. ארכיטקטורה בשכבות "מבנה טוב של ארכיטקטורת תוכנה הוא: ארכיטקטורה שכבתית כאשר כל שכבה מספקת קבוצה קוהרנטית של שירותים (על ידי ממשקים מוגדרים היטב)" [Grady Booch, Object Solutions, Addison Wesley, 1996, p. 54.] תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  10. ארכיטקטורה שכבתית Package Structure תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  11. ארכיטקטורה שכבתית בלי DIP Class Structure תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  12. ארכיטקטורה שכבתית עם DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  13. דוגמא: 'כפתור ונורה' Collaboration Diagram OMD לפני השימוש ב-DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  14. דוגמא: 'כפתור ונורה'מימוש לפני השימוש ב-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 תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  15. דוגמא: 'כפתור ונורה'OMDעם השימוש ב-DIP תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  16. דוגמא: 'כפתור ונורה'מימוש עם השימוש ב-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(); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  17. דוגמא: 'כפתור ונורה'מימוש עם השימוש ב-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) {} תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  18. תרשים OMD עם object adapter נדרש כאשר יש תוכנת צד שלישי שאינה תומכת בממשק של ה Server תיכון תוכנה ד"ר ר' גלנט / י' לויאן

  19. מימוש עם 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); } תיכון תוכנה ד"ר ר' גלנט / י' לויאן

More Related