Upgrade to Pro — share decks privately, control downloads, hide ads and more …

АФТИ ООП 2013-2014. Лекция I/07

АФТИ ООП 2013-2014. Лекция I/07

Oleg Dashevskii

October 21, 2013
Tweet

More Decks by Oleg Dashevskii

Other Decks in Education

Transcript

  1. FLASHBACK • ООП: объектно-ориентированное проектирование. • Лучше проектировать пораньше, чтобы

    потом все не переделывать. • Один класс — одна зона ответственности (SRP). • Два инструмента: наследование и композиция — позволяют распределять функционал по нескольким классам.
  2. /* shape.h */ enum ShapeType { CIRCLE, SQUARE }; struct

    Shape { ! ShapeType type; }; /* circle.h */ struct Circle { ! ShapeType type; ! double radius; ! Point center;! }; void DrawCircle(struct Circle *); /* square.h */ struct Square { ! ShapeType type; ! double side; ! Point topLeft;! }; void DrawSquare(struct Square *); Процедурный style
  3. /* drawallshapes.c */ typedef struct Shape *ShapePointer; void DrawAllShapes(ShapePointer *list,

    int n) { ! int i; ! for (i = 0; i < n; ++i) { ! ! ShapePointer *s = list[i]; ! ! switch (s->type) { ! ! ! case SQUARE: DrawSquare((struct Square *)s); break; ! ! ! case CIRCLE: DrawCircle((struct Circle *)s); break; ! ! } ! } }
  4. class Shape { public: ! virtual void Draw() const =

    0; }; class Square : public Shape { public: ! // ... ! virtual void Draw() const; }; class Circle : public Shape { public: ! // ... ! virtual void Draw() const; }; void DrawAllShapes(Shape **list, int n) { ! for (int i = 0; i < n; ++i) ! ! list[n]->Draw(); } OO-style
  5. Текущее состояние системы + Абстракция = Система с новой возможностью

    Шаг второй Шаг третий Система с новой возможностью Фича’ + = Система с реализованной новой возможностью
  6. class Shape { public: ! virtual void Draw() const =

    0; ! virtual bool Precedes(const Shape &another) = 0; }; ЗАДАНИЕ ПОРЯДКА