fondamenti della programmazione: metodi evoluti prof ...nardelli/fondamenti-programmazione... ·...

Post on 16-Feb-2019

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico Nardelli

Esercitazione 2

versione originale: http://touch.ethz.ch

Today

We will revisit classes, features and objects. We will see how program execution starts. We will play a game.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 2

Static view

A program consists of a set of classes.

Features are declared in classes. They define operations on objects created from classes.

Queries answer questions. The answer is provided in a variable called Result.

Commands execute actions. They do not provide any result, so there is no variable called Result that we can use.

Another name for a class is type. Class and Type are not exactly the same, but they are close enough for now, and we will learn the difference later on. ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 3

Declaring the type of an object

The type of any object you use in your program must be declared somewhere.

Where can such declarations appear in a program?

in feature declarations • formal argument types • return type for queries

in the local clauses of routines

This is where you declare any objects that only the routine

needs and knows.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 4

Declaring the type of an object

class DEMO feature procedure_name (a1: T1; a2, a3: T2) -- Comment local l1: T3 do … end function_name (a1: T1; a2, a3: T2): T3 -- Comment do … end attribute_name: T3 -- Comment end

formal argument type

local variable type

return type

return type

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 5

formal argument type

formal argument type

formal argument type

Dynamic view

When the program is being executed (at

“runtime”) we have a set of objects (instances) created from the classes (types).

The creation of an object implies that a piece of memory is allocated in the computer to represent the object itself.

Objects interact with each other by calling features on each other.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 6

Static view vs. dynamic view

Queries (attributes and functions) have a result

type. When executing the query, you get an object of that type.

Routines have formal arguments of certain types. During the execution you pass objects of the same (or compatible) type as actual arguments to a routine call.

Local variables are declared in their own section, associating names with types. Invoking a local returns the current object of that type referred to by that variable.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 7

How it all starts

Root procedure may:

Create new objects Call features on them, which may create other

objects Etc.

Executing a system consists of - creating a root object, - which is an instance of a designated class from the system, called its root class, - using a designated creation procedure of that class, called its root procedure.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 8

Root object

Root procedure

obj1

obj2

r1

r2 create obj1.r1

create obj2.r2

Executing a system

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 9

Specifying the root

How to specify the root class and root creation procedure of a system? Use EiffelStudio Project -> Project Settings -> Target -> General -> Root

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 10

Who are Adam and Eve?

Who creates the first object? The runtime creates a so called root object. The root object creates other objects, which in

turn create other objects, etc. You define the type (class) of the root object in

the project settings Project -> Project Settings -> Target: project -> Root

How is the root object created? The runtime calls a creation procedure of the

root object You select this creation procedure of the root

object Project -> Project Settings -> Target: project -> Root

The application exits at the end of this creation procedure

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 11

Acrobat game

We will play a little game now. Everyone will be an object. There will be different roles.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 12

You are an acrobat

When you are asked to Clap, you will be given a number. Clap your hands that many times.

When you are asked to Twirl, you will be given a number. Turn completely around that many times.

When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 13

You are an ACROBAT – initial version class ACROBAT feature clap (n: INTEGER) do -- Clap `n’ times and adjust `count’. end twirl (n: INTEGER) do -- Twirl `n’ times and adjust `count’. end count: INTEGER -- Total # of times clapped or twirled. end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 14

You are an ACROBAT – adding invariants class ACROBAT feature clap (n: INTEGER) -- Clap `n’ times and adjust `count’. require n>0 do -- to be completed ensure count = old count + n end twirl (n: INTEGER) -- Twirl `n’ times and adjust `count’. require n>0 do -- to be completed ensure count = old count + n end count: INTEGER -- Total # of times clapped or twirled. end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 15

You are an ACROBAT – implementation

Open EiffelStudio, copy-paste the code,

and complete it !

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 16

I am the root object

I got created by the runtime. I am executing the first feature.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 17

I am a DIRECTOR

class DIRECTOR create prepare_and_play feature prepare_and_play do -- See following slides. end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 18

I am the root object (version 1) prepare_and_play local mario, luigi, piero: ACROBAT do create mario create luigi create piero mario.clap (3) luigi.clap (4) piero.clap (5) mario.twirl (3) luigi.twirl (4) piero.twirl (5) mario.count luigi.count piero.count end • N.B. Allow objects to give feedback to what happens to them by printing it. For example: print ("%Nmario.count = "); print(mario.count)

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 19

You are an acrobat with a copycat

You will have someone else as your Copycat. N.B. asymmetric relation!

When you are asked to Clap, you will be given a number. Clap your hands that many times. Pass the same instruction to your Copycat.

When you are asked to Twirl, you will be given a number. Turn completely around that many times. Pass the same instruction to your Copycat.

If you are asked for Count, ask your Copycat and answer with the number he tells you.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 20

You are an ACROBAT_WITH_COPYCAT – initial(1)

class ACROBAT_WITH_COPYCAT inherit ACROBAT redefine twirl, clap, count end create make feature copycat: ACROBAT make (p: ACROBAT) do -- Remember `p’ being the copycat. end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 21

You are an ACROBAT_WITH_COPYCAT – initial(2)

clap (n: INTEGER) do -- Clap `n’ times and forward to copycat. end twirl (n: INTEGER) do -- Twirl `n’ times and forward to copycat. end count: INTEGER do -- Ask copycat and return his answer. end end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 22

You are an ACROBAT_WITH_COPYCAT – invariants(1)

class ACROBAT_WITH_COPYCAT inherit ACROBAT redefine twirl, clap, count end create make feature copycat: ACROBAT make (p: ACROBAT) -- Remember `p’ being the copycat. require p /= Void do -- to be completed ensure copycat = p end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 23

You are an ACROBAT_WITH_COPYCAT – invariants(2)

clap (n: INTEGER) -- Clap `n’ times and forward to copycat. do -- to be completed end twirl (n: INTEGER) -- Twirl `n’ times and forward to copycat. do -- to be completed end count: INTEGER -- Ask copycat and return his answer. do -- to be completed end end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 24

You are an ACROBAT_WITH_COPYCAT – implementation

Open EiffelStudio, copy-paste the code,

and complete it !

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 25

Cannot redefine an attribute in descendants!

Not allowed by the language definition. Performance degradation: replacing a simple

memory access with a function call It might make things slower for incremental

compiler and slow down dynamic access to entities. It might break code in ancestors where attribute

is assigned a value to Choice might change in the future (even if unlikely)

SOLUTION Hide the attribute in the ancestor Expose a function which is inherited and redefined

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 26

You are an ACROBAT – new solution (1) class ACROBAT feature {NONE} internal_count: INTEGER -- Store value of a private counter. add_count (n: INTEGER) -- Change value of the private counter. require n > 0 do internal_count := internal_count + n ensure internal_count = old internal_count + n end feature count: INTEGER -- Total # of times clapped or twirled. do Result := internal_count end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 27

You are an ACROBAT – new solution (2) clap (n: INTEGER) -- Clap `n’ times and adjust `count’. require n>0 do add_count(n) ensure count = old count + n end twirl (n: INTEGER) -- Twirl `n’ times and adjust `count’. require n>0 do add_count(n) ensure count = old count + n end end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 28

You are an ACROBAT_WITH_COPYCAT – new sol. (1) class ACROBAT_WITH_COPYCAT inherit ACROBAT redefine twirl, clap, count end create make feature count: INTEGER -- Ask copycat and return his answer. do Result := copycat.count end copycat: ACROBAT make (p: ACROBAT) -- Remember `p’ being the copycat. require p /= Void do copycat := p ensure copycat = p end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 29

You are an ACROBAT_WITH_COPYCAT – new sol. (2) clap (n: INTEGER) -- Clap `n’ times and forward to copycat. require n>0 do add_count(n) copycat.clap(n) ensure count = old count + n end twirl (n: INTEGER) -- Twirl `n’ times and forward to copycat. require n>0 do add_count(n) copycat.twirl(n) ensure count = old count + n end end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 30

I am the root object (version 2) prepare_and_play local mario, luigi, piero: ACROBAT carla, daria: ACROBAT_WITH_COPYCAT do create mario create luigi create piero create carla.make (mario) create daria.make (luigi) carla.twirl (2) luigi.clap (7) piero.twirl (daria.count) piero.twirl (carla.count) carla.copycat.clap (carla.count) daria.clap (2) end N.B. Allow objects to give feedback to what happens to them by printing it. For example: print ("%Nmario.count = "); print(mario.count)

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 31

You are an author

When you are asked to Clap, you will be given a number. Clap your hands that many times. Say “Thank You.” Then take a bow (as dramatically as you like).

When you are asked to Twirl, you will be given a number. Turn completely around that many times. Say “Thank You.” Then take a bow (as dramatically as you like).

When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 32

You are an AUTHOR – initial class AUTHOR inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) do -- Clap `n’ times say thanks and bow. end twirl (n: INTEGER) do -- Twirl `n’ times say thanks and bow. end end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 33

You are an AUTHOR – invariants class AUTHOR inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) -- Clap `n’ times say thanks and bow. do -- to be completed. end twirl (n: INTEGER) -- Twirl `n’ times say thanks and bow. do -- to be completed. end end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 34

You are an AUTHOR – implementation

Open EiffelStudio, copy-paste the code,

and complete it !

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 35

I am the root object (version 3) prepare_and_play local mario, luigi, piero: ACROBAT carla, daria: ACROBAT_WITH_COPYCAT dante: AUTHOR do create mario create luigi create piero create carla.make (mario) create daria.make (luigi) create dante dante.clap (4) carla.twirl (2) luigi.clap (carlacount) piero.twirl (daria.count) carla.copycat.clap (carla.count) daria.clap (2) end N.B. Allow objects to give feedback to what happens to them by printing it. For example: print ("%Nmario.count = "); print(mario.count)

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 36

You are a curmudgeon

When given any instruction (Twirl or Clap), ignore it, get up and say (as dramatically as you can) “I REFUSE”, then sit down again

If you are asked for Count, always answer with 0.

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 37

You are a CURMUDGEON – initial class CURMUDGEON inherit ACROBAT redefine clap, twirl, count end feature clap (n: INTEGER) do -- Say “I refuse”. end twirl (n: INTEGER) do -- Say “I refuse”. end count do -- Return 0. end end ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 38

You are a CURMUDGEON – invariants class CURMUDGEON inherit ACROBAT redefine clap, twirl, count end feature clap (n: INTEGER) -- Say “I refuse”. do -- to be completed. end twirl (n: INTEGER) -- Say “I refuse”. do -- to be completed. end count: INTEGER -- Return 0 . do -- to be completed. ensure Result = 0 end end

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 39

You are a CURMUDGEON – implementation

Open EiffelStudio, copy-paste the code,

and complete it !

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 40

I am the root object (version 4) prepare_and_play local mario, luigi, piero: ACROBAT carla, daria: ACROBAT_WITH_COPYCAT dante: AUTHOR curmudgeon1: CURMUDGEON do create mario create luigi create piero create carla.make (mario) create daria.make (luigi) create dante create curmudgeon1 dante.clap (4) carla.twirl (2) curmudgeon1.clap (7) luigi.clap (curmudgeon1.count) piero.twirl (daria.count) carla.copycat.clap (carla.count) daria.clap (2) end N.B. Allow objects to give feedback to what happens to them by printing it. For example: print ("%Nmario.count = "); print(mario.count) ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 41

Concepts seen

Asking a question to a person Query Call

Telling person to behave according to a specification

Classes with Features

E.g. how many times to clap Arguments

Telling a person to do something Command Call

Telling different people to do the same has different outcomes

Polymorphism

What queries What commands

Interface

People Objects

Game Eiffel

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 42

Concepts seen

All people were some kind of ACROBAT

Inheritance

Persons need to be born and need to be named

Creation

E.g. partner1.buddy.clap (2) Chains of feature calls

Names for the people Entities

E.g. count in ACROBAT_WITH_BUDDY

Return value

Game Eiffel

ESERCITAZIONE-2-acrobats Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 43

top related