22
Ch4: Software Architecture and Design

Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance Specialization: Generalization General parent class customized

  • View
    232

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

Ch4: Software Architecture and Design

Page 2: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

2

Specialization and generalization inheritance

Specialization:

Generalization General parent class customized to include more

data, more methods or both. Child class special case of parent class. Good form of inheritance.

Page 3: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

3

Specialization and generalization inheritance - Example

class PItem : public Item { protected: food_state Environ; int Days; void ItemSpecificPrint(); public: virtual void CreateNewItem(); virtual void PrintItem(); virtual void PrintPerish(); virtual void UpdateItem(); void PrintDaysFoodState();};

class DeliItem : public PItem {protected: float Weight; float CostPerLb; void ItemSpecificPrint(); public: virtual void CreateNewItem(); virtual void PrintItem(); virtual void PrintPerish(); virtual void UpdateItem(); void PrintWeightandCost(); };

Item / \ / \ NonPItem PerishItem / \ \ / \ \ DeliItem DairyItem ProduceItem

GeneralizationSpecialization

Food state: shelf, expiration dataWeight & cost:

Page 4: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

4

Specification inheritance and example

GraphicalObject / \ \ / \ \ Ball Wall Hole

Data-Oriented

Menu / \ / \PrintMenu SaveMenu

Function-Oriented

Page 5: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

5

Construction inheritance and example

Dictionary / \ / \SymbolTable HashTable

LinkedList / \ \ / \ \Queue Set Stack

Page 6: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

6

Variance inheritance and example

PointingDevice / \ \ / \ \TouchPad Mouse Tablet

Page 7: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

7

Combination inheritance and example

Faculty Student \ / \ / TeachingAsst

MeatItem ProduceItem \ / \ / DeliItem

Page 8: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

8

Generalization inheritance and example

Window | |ColoredWindow

Window displays white background.ColoredWindow allows the background tobe other than white.

Page 9: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

9

Extension inheritance and example

Set | |StringSet

StringSet class has additional string-related operations

Page 10: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

10

Limitation inheritance and example

DEQueue / \ / \ Queue Stack / \LIFO FIFO

Key issues:•Going from a bi-directional queueto limited abstractions•Limiting functionality

Page 11: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

11

Overloading

Ability to define two or more methods with the same names and different signatures:

OO design and programming allows us to define our own types (classes)

Page 12: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

12

Overloading – Stack example

class stack { private: char* st, top; int size; public: void stack(int x) {top = st = new char[x]; size = x;} void stack() {top = st = new char[100]; size = 100;} // push, pop, top, etc., Omitted};

main(){ stack S1(10); // Creates a char Stack with 10 Slots stack S2; // Default, no Parameter Supplied // Creates a char Stack with 100 Slots // Size of S2 Might not be Known to User!}

20 15 10 5S1

20 15 10 5S2 … etc ...

Page 13: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

13

Overloading in HTSS

class Item { private: int UPC, OnShelf, InStock, ROLimit; // Etc... As Previously Givenpublic: Item(); // The Constructor // Etc... Others as Previously Given

Item operator--(){ this->OnShelf--; this->InStock--; } };main(){ Item I1; Status s; s = I1.Create_New_Item(123, OJ, 10, 30, ...); I1--; // Reduces I1.OnShelf and I1.InStock // Now Contain 9 and 29 Respectively }

Overloads the operator --

Page 14: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

14

Polymorphism: Definition

Variables P: Person; F: Faculty; S: Student;

//Supertype can Hold Instance of//Subtype, but NOT REVERSE!

P = F; // Treat Faculty as PersonP = S; // Treat Student as Person

P

F S

Page 15: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

15

Polymorphism/dispatching: illustration

PersonNameSSN

EmployeeDept.Yrs.

StudentDormGPA

FacultyRank

DeanSchool

UndergradYear

GradProgramDegree

Suppose, we want to define print_info() for each class?What is true for all persons?

Page 16: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

16

Polymorphism/dispatching: illustration

Print_Info methods defined as follows:Person::Print_Info()

{Prints Name and SSN}

Employee::Print_Info()

{Calls Person::Print_Info();

Prints Dept and Yrs; }

Student::Print_Info()

{Calls Person::Print_Info();

Prints Dorm and GPA; }

Faculty::Print_Info()

{Calls Employee::Print_Info();

Prints Rank; }

Print_Info Also for Dean, UnderGrad, Grad

Page 17: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

17

Polymorphism/dispatching: illustration

Person p = new Person(); p.print_info();Faculty f = new faculty(); f.print_info();Person fp = f; fp.print_info();

The print_info() method invoked on fp depends on the run time type ofthe object fp (faculty) and not on the compile time type of the object.

Run time type of object fp is faculty, not person.

Page 18: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

18

Polymorphism and dispatching: definition

Polymorphism via dispatching:

Benefits of polymorphism/dispatching:

Polymorphism/dispatching incurs cost or overhead both at compile and runtime Efficiency is lost, cannot determine the method at

compile time.

Page 19: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

19

Substitutability

Principle of substitutability:

Implications of substitutability:

Example:

Page 20: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

20

Important implementation concepts

Message passing:

Automatic variables:

Dynamic variables:

Page 21: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

21

Important implementation concepts (contd..)

Lifetime:

Scope:

Immutable values:

Typing of variables:

Strongly typed languages (Ada95 and Java)

Page 22: Ch4: Software Architecture and Design. 1 Specialization and generalization inheritance  Specialization:  Generalization  General parent class customized

22

Important implementation concepts (contd..)

Static binding:

Dynamic binding:

Early binding vs. late binding