Presentation is loading. Please wait.

Presentation is loading. Please wait.

אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 1 פרק 7 ISP דוגמא נוספת.

Similar presentations


Presentation on theme: "אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 1 פרק 7 ISP דוגמא נוספת."— Presentation transcript:

1 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 1 פרק 7 ISP דוגמא נוספת

2 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 2 Interface Segregation Principle עיקרון הפרדת ממשקים הקליינט יהיה חשוף אך ורק לממשק שהוא זקוק לו אסור להכריח קליינט להיות תלוי בממשק בו הוא לא משתמש

3 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 3 דוגמא : דלת ביטחון שלב א ' הגדרת ממשק Security Door – דלת ביטחון פשוטה שלב ב ' ממימוש של הממשק הנ " ל עם תוספת Timed Security Door – דלת שמתריעה כאשר היא פתוחה יותר מידי זמן. האתגר האיכותי : – להימנע מ " זיהום ממשקים " (interface pollution) ב Timed Door.

4 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 4 שלב א ' דלת ביטחון פשוטה Security Door למופעים של מחלקה Door יש היכולות הבאות : דלת יכולה להיות נעולה (locked). דלת יכולה להיות לא נעולה (unlocked). דלת יכולה לדעת אם היא פתוחה (open) או סגורה (closed)

5 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 5 שלב א ' ממשק של Security Door // file Door.h #ifndef Door_H #define Door_H class Door { public : virtual void lock()=0; virtual void unlock()=0; virtual bool isOpen()=0; }; #endif

6 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 6 שלב ב ' ממימוש של הממשק הנ " ל עם תוספת : Timed Security Door על מנת להתריעה כאשר היא פתוחה יותר מידי זמן צריך להגדיר מחלקה TimedDoor: – TimedDoor הוא בן (sub-Class) של Door. – לכן TimedDoor יממש lock(), unlock(), isOpen() – בנוסף TimedDoor יתקשר לאוביקט נוסף בשם Timer – הקישור בין TimedDoor יהיה לפי הפרוטוקול Timer-TimerClient:

7 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 7 הפרוטוקול Timer-TimerClient // file Timer.h #ifnder Timer_H #define Timer_H class TimerClient; class Timer { public: void register_(int timeout, const TimerClient & client) }; #endif // file TimerClient.h #ifndef TimerClient_H #define TimerClient_H class TimerClient { public: virtual void timeOut( ) = 0; }; #endif

8 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 8 פתרון מצוי : TimeClient עומד בראש היארכי הירושה +timeOut():void TimerClient > +lock():void +unlock():void +isOpen():bool +timeOut():void Door +lock():void +unlock():void +isOpen():bool +timeOut():void TimedDoor +register_(int timeout,TimerClient client):void Timer itsTimerClient *

9 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 9 הצעת שיפור ל TimedDoor נאפשר ביטול של timeouts שאינם רלוונטיות. תסריט שממחיש את הצורך בשיפור הנ " ל : – נניח שמשך ה timeout הוא 20 שניות. – ביום מסוים משעמם לפ ' רדי, הוא מחפש אתגר ומצליח לפתוח ולסגור את הדלת כל 7 שניות.

10 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 10 // file Timer.h #ifndef Timer_H #define Timer_H class TimerClient; class Timer { public: void register_ (int timeout, int timeOutId, TimerClient & client) ; }; #endif מימוש של השיפור : על " מי " זה ישפיע ? // file TimerClient.h #ifndef TimerClient_H #define TimerClient_H class TimerClient { public: virtual void timeOut(int timeOutId)=0;}; }; #endif +timeOut():void TimerClient > +lock():void +unlock():void +isOpen():bool +timeOut():void Door +lock():void +unlock():void +isOpen():bool +timeOut():void TimedDoor +register_(int timeout,int timeOutid,TimerClient client):void Timer itsTimerClient *

11 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 11 Interface Segregation Principle עיקרון הפרדת ממשקים הקליינט יהיה חשוף אך ורק לממשק שהוא זקוק לו אסור להכריח קליינט להיות תלוי בממשק בו הוא לא משתמש

12 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 12 שתי פתרונות לפי ISP Class Adapter Pattern – יתרון : אין תוספת אובייקטים הצורכים זיכרון וכו ' – חסרון : אי אפשר אם ה sub Class אינו מממש את הממשק timeOut() Object Adapter – יתרון : מאפשר שימוש ב sub Class שאינו מממש את הממשק timeOut() – חסרון : יש תוספת אובייקטים הצורכים זיכרון וכו '

13 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 13 Class Adapter Pattern Solution // file TimerClient.h class TimerClient { public : virtual void timeOut(int timeout, int timeOutId)=0; }; // file Door.h class Door { public : virtual void lock()=0; virtual void unlock()=0; virtual bool isOpen()=0; }; // file Timer.h class TimerClient; class Timer { public: void register_(int timeout, int timeOutId, const TimerClient & client) }; // file TimedDoor.h #include “Door.h”; #include “TimerClient.h”; class TimedDoor : public Door, public TimerClient { public : virtual void lock(); virtual void unlock(); virtual bool isOpen(); virtual void timeOut(int timeout, int timeOutId); }; // file Timer.cpp #include “Timer.h” #include “TimerClient.h” …

14 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 14 Object Adapter Pattern Solution: Delegation ( האצלה ) // file TimedDoor.h #include “Door.h” #include “TimedDoorAdapter.h” class Door : public Door { public : virtual void lock()=0; virtual void unlock()=0; virtual bool isOpen()=0; virtual void soundAlarm(int timeOutId)=0; }; //file TimedDoorAdapter.cpp #include “TimedDoorAdapter.h” #include “TimedDoor.h” void TimedDoorAdapter::timeOut(int timeOutId) { itsTimedDoor->soundAlarm(timeOutId); }


Download ppt "אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכויות שמורות 1 פרק 7 ISP דוגמא נוספת."

Similar presentations


Ads by Google