itsType) { case square: drawSquare((Square*)s); break; case circle: drawCircle((Circle*)s); break; }"> itsType) { case square: drawSquare((Square*)s); break; case circle: drawCircle((Circle*)s); break; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 2 Open Closed Principle (OCP) האסטרטגיה שעל פיה ניתן לתת מענה לדרישות משתנות דוגמאות למימוש

3 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 3 הפשטה / רב צורתיות ללא OO(ירושה) הדפסת 'צורה' 1 enum ShapeType {circle, square}; struct Point{…}; struct Shape { ShapeType itsType; }; struct Circle { ShapeType itsType; double itsRadius; Point itsCenter; }; struct Square { ShapeType itsType; double itsSide; Point itsTopLeft; }; 2 void drawSquare(Square* s){…}; void drawCircle(Circle* c){…}; void drawAllShapes(Shape* list[], int n) { int i; for (i=0; i<n; i++) { Shape *s = list[i]; switch (s->itsType) { case square: drawSquare((Square*)s); break; case circle: drawCircle((Circle*)s); break; }

4 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 4 רב צורתיות עם OO (ירושה ) הדפסת 'צורה' class Shape { public: virtual void draw() const = 0; }; class Square : public Shape { public: virtual void draw() const; }; void drawAllShapes(Shape* list[], int n) { int i; for (i=0; i<n; i++) list[i]->draw(); } class Circle : public Shape { public: virtual void draw() const; };

5 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 5 Run Time Type Identification (RTTI) (תוכנית שאינה עומדת ב-OCP) class Point{...}; class Shape {}; class Square : public Shape { private: Point itsTopLeft; double itsSide; friend void drawSquare(Square*); }; void drawAllShapes(Shape *line[],int n ) { for (int i=0;i<n;i++) { Circle* c = dynamic_cast (line[i]); Square* s = dynamic_cast (line[i]); if (c) drawCircle(c); else if (s) drawSquare(s); } class Circle : public Shape { private: Point itsCenter; double itsRadius; friend void drawCircle(Circle*); };

6 אביב תשס " ה JCT תיכון תוכנה ד " ר ר ' גלנט / י ' לויאןכל הזכורות שמורות 6 Run Time Type Identification (RTTI) ( תוכנית העומדת ב -OCP) class Shape { public: virtual void draw() const = 0; }; class Square : public Shape { public: virtual void draw() const; }; void drawSquaresOnly(Shape *line[],int size) { for (int i=0; i<size; i++) { Square* s = dynamic_cast (line[i]); if (s) s->draw(); }


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

Similar presentations


Ads by Google