lecture #2 on object-oriented modeling glp - génie logiciel, programmatio… · 2 class diagrams...

Post on 21-May-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Outline

Lecture #2 on Object-Oriented Modeling

Thierry Geraud

EPITA Research and Development Laboratory (LRDE)

2006

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 1 / 38

Outline

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 2 / 38

Outline

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 2 / 38

Outline

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 2 / 38

Outline

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 2 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

Inheritance

Consider that:

inheritance is a key concept of object-orientation

a lot of different kinds of inheritance existwith various meanings/semantics (read Meyer for details)

the most prominent form is:“class inheritance” related to “sub-typing”

mapping the “is a” relationship

“polymorphism of methods” and “abstract data types”are also key concepts of object-orientationand rely on inheritance

see also:http://en.wikipedia.org/wiki/Inheritance_

(object-oriented_programming)Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 3 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

Inheritance and Polymorphism

Two points of view

type theory:it defines the notion of “sub-typing”and “sub-classing” reflects (tries to reflect) this notion

semantics:verifying “is a” is mandatory to get class inheritancehowever it is not enough to get proper inheritancehaving various semantics explain the different kinds ofinheritance

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 4 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 5 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

UML?

The Unified Modeling Language:

is an object modeling and specification languageboth textual and graphical

is the language we should talk and understand

comes from:James Rumbaugh (OMT)Grady Booch (Booch method)Ivar Jacobson (Objectory)Richard Soley (Object Management Group, OMG)

see also:http:

//en.wikipedia.org/wiki/Unified_Modeling_LanguageThierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 6 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

UML...

This lecture does not aim at teaching UML!

Resources from the Internet

the “official” site:http://www.uml.org/

from Cetus (the OO portal):http://www.cetus-links.org/oo_uml.html

at a glance (one single page):Allen Holub’s UML Quick Referencehttp://www.holub.com/goodies/uml/index.html

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 7 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Books on UML

“The Unified Modeling Language User Guide,” by GradyBooch, James Rumbaugh, and Ivar Jacobson, 2nd ed.,Addison-Wesley Professional, 2005.

“The Unified Modeling Language Reference Manual,” byJames Rumbaugh, Ivar Jacobson, and Grady Booch, 2nded., Addison-Wesley Professional, 2004.

“The Complete UML Training Course,” by Grady Booch,James Rumbaugh, and Ivar Jacobson, Prentice Hall, 2000.

and many more...

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 8 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 9 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Different Ways of Describing a Program

three different ways

FUNCTIONAL point of view:what is it doing?for instance, its list of main tasksor of more “atomic” functionalities...

STATIC point of view:what’s in it?for instance, a list of classesand the methods of every class, etc.

DYNAMIC point of view:what happens?when you click on this button, then...

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 10 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

One Program, Three Axes

consider that

a program is in a space

this space has three axes:functionalstaticdynamic

describing one axis is not sufficient

you have to describe the three of them!

all the axes are tightly linked altogether

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 11 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

One Program, One major axis?

to specify (and/or describe) a program

there is no major axis!

however

OO modeling is very “static-oriented”... ...because one modelsclass hierarchies!

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 12 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

An Important Static Diagram

entity/relationship diagram → UML class diagram

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 13 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 14 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

A Class in UML (1/3)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 15 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

A Class in UML (2/3)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 16 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Relationships between Classes in UML (1/3)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 17 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Relationships between Classes in UML (2/3)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 18 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Relationships between Classes in UML (3/3)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 19 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Inheritance Tree and Object in UML

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 20 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Misc

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 21 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 22 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

NotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

Hints

First

draw entities and their relationshipssplit your program into several diagrams

so that one diagram shows one ideaso that each diagram is comprehensive

Then (and only then)

show public stuff

Afterwards (and on specific separate diagrams)

depict details

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 23 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

Exercise

< live! >

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 24 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 25 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Variable and Type

C translationint i; i : intint i = 3; i : int = 3

there is no ambiguity when we write:

variable : type = definition

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 26 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Procedure

C translationint f(float); f : float -> intint f(float arg); f : (arg : float) -> int

the variable (entity; here a procedure) is named f

the type of f is float -> intread: “takes float and gives int”

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 27 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Type Definition

C translationtypedef def t; type t = def

Consider that t is just an alias (a name) for the type def

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 28 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Enumeration

Ctypedef enum { monday, /*...*/ sunday } day;

translationtype day = enum monday, .., sunday

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 29 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Structure (1/2)

Cstruct bar { bool b; double d; };

translationtype bar = { b : bool, d : double }

The “{ ... } ” notation means that we group a set of fields.

Each field has a name (here b and d) and a type (resp.bool and double ).

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 30 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Structure (2/2)

a struct is a “product type”

with:

type day = enum monday, .., sundaytype month = enum january, .., decembertype ydate = { d : day, m : month }

the values of ydate are:(monday, january)(tuesday, january)...(sunday, december)

we have:

card(ydate ) = card(day ) × card(month )

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 31 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Union—or variant (1/2)

Cunion bar { bool b; double d; };

translationtype bar = [ b : bool | d : double ]

The “[ ... ] ” notation means that we have exactly one ofthe fields.

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 32 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Union—or variant (2/2)

we have a “sum type”

with:

type ydate = [ d : day, m : month ]

the values of ydate are:monday...sundayjanuary...december)

we have:

card(ydate ) = card(day ) + card(month )

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 33 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Method

Cstruct bar { int i; int f(float); };

translationtype bar = { i : int, f : float -> int }

The “{ ... } ” notation means that we group a set of fields.

Each field has a name (here i and d) and a type (resp.int and double ).

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 34 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

About fields

In:type bar = { i : int, f : float -> int };

we have two fields:

τ1 which is “i : int ”

and τ2 which is “f : float -> int ”

we can write:

bar = { τ1, τ2 }

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 35 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Outline

1 IntroductionNotationHow to Describe a Program?A Quick Tour of Class DiagramsHints

2 Class Diagrams in UML

3 Practising Inheritance

4 Inheritance and typingA Language to Rewrite C/C++ CodeSubstitution

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 36 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

The Cornerstone (1/2)

consider the function foo:

foo : t -> void

and the variable v:

v : t’

what the relationship between the couple of types t and t’should be so that the following call is valid?

foo(v)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 37 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

The Cornerstone (2/2)

we want:

t’ to be included in t

t to be substituted by t’

t’ to be a sub-type of t

t’ <: t

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 38 / 38

IntroductionClass Diagrams in UML

Practising InheritanceInheritance and typing

A Language to Rewrite C/C++ CodeSubstitution

Simple Test

with:

type t = { i : int, f : int -> float }foo : (arg : t) -> float =

arg.f(arg.i)end

is the following program correct?

type t’ = { i : int, f : int -> float, b : bool }var : t’ // = initializationres : float = foo(var)

Thierry G eraud Lecture #2 on Object-Oriented Modeling EPITA-LRDE 2006 39 / 38

top related