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

Post on 19-Dec-2015

222 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

Ch4: Software Architecture and Design

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.

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:

4

Specification inheritance and example

GraphicalObject / \ \ / \ \ Ball Wall Hole

Data-Oriented

Menu / \ / \PrintMenu SaveMenu

Function-Oriented

5

Construction inheritance and example

Dictionary / \ / \SymbolTable HashTable

LinkedList / \ \ / \ \Queue Set Stack

6

Variance inheritance and example

PointingDevice / \ \ / \ \TouchPad Mouse Tablet

7

Combination inheritance and example

Faculty Student \ / \ / TeachingAsst

MeatItem ProduceItem \ / \ / DeliItem

8

Generalization inheritance and example

Window | |ColoredWindow

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

9

Extension inheritance and example

Set | |StringSet

StringSet class has additional string-related operations

10

Limitation inheritance and example

DEQueue / \ / \ Queue Stack / \LIFO FIFO

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

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)

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 ...

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 --

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

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?

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

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.

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.

19

Substitutability

Principle of substitutability:

Implications of substitutability:

Example:

20

Important implementation concepts

Message passing:

Automatic variables:

Dynamic variables:

21

Important implementation concepts (contd..)

Lifetime:

Scope:

Immutable values:

Typing of variables:

Strongly typed languages (Ada95 and Java)

22

Important implementation concepts (contd..)

Static binding:

Dynamic binding:

Early binding vs. late binding

top related