12
Learning Object-Oriented Programming and Design with TDD What is a class? a factory of objects Stéphane Ducasse http://stephane.ducasse.free.fr http://www.pharo.org W3S02

Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

Learning Object-OrientedProgramming and Design with TDD

What is a class?a factory of objects

Stéphane Ducassehttp://stephane.ducasse.free.fr

http://www.pharo.org

W3S02

Page 2: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

A Class is Mould of Objects

A class is an object mould◦ A class creates many instances

Each instance is still a separate entity◦ You can create mutiple baths and

break one.

W3S02 2 / 12

Page 3: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

A Class is Factory of Objects

A class Defines all the variables of all its

instances Defines all the behavior

◦ no repetition in all the instances

W3S02 3 / 12

Page 4: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

A Class Factorises Object Properties

State: All rectangles have an origin and an extent (containing different values) Behavior: Computing the top of a rectangle can be

◦ defined once in the class◦ applied to all rectangles

Rectangle >> top"Answer the position of the receiver's top horizontal line."^ origin y

W3S02 4 / 12

Page 5: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

A Class is a Factory of ObjectsA class describes the structure and the behavior of its instances Each instance has its own value All instances of class share the same behavior

areatopCornercenter

origincorner

Rectangle

instance of

W3S02 5 / 12

Page 6: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

An Instance has Specific Properties

A class defines the structure and behavior of all its instances Instances have specific values for the properties defined by their class

(Rectangle origin: 10@10 corner: 50@110) extent>>> 40@100(Rectangle origin: 10@10 corner: 50@60) extent>>> 40@50

W3S02 6 / 12

Page 7: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

Class vs. Instances

An object is instance of a class An object has been created by a class All instances of a class react similarly to a message All instances of a class execute the same method in response to a message

W3S02 7 / 12

Page 8: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

Instances Share the Same Behavior All the instances of a class share the same behavior A class defines the methods that are executed when an instance receives a

message

areatopCornercenter

origincorner

Rectangle

instance of

area ^ self height * self width

area

W3S02 8 / 12

Page 9: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

Methods are Looked up in the Class of the Receiver

areatopCornercenter

origincorner

Rectangle

instance of

area ^ self height * self width

area

W3S02 9 / 12

Page 10: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

Methods are looked up dynamically in the class ofthe receiver

aDog

angry()...

Dog

angryaCat

angry()...

Cat

angry

angry() angry()

aFish

angry()...

Fish

angry

angry()

W3S02 10 / 12

Page 11: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

What you should know

Any object is instance of a class. A class is a factory: it creates instances. All the instances of class execute the same method in response to the same

message. A class creates instances that share their behavior. A class defines methods that are executed on its instances.

W3S02 11 / 12

Page 12: Learning Object-Oriented Programming and Design …rmod-pharo-mooc.lille.inria.fr/MOOC/2018-PreSequelOOP...A Class is Factory of Objects A class Defines all the variables of all its

A course by Stéphane Ducassehttp://stephane.ducasse.free.fr

Reusing some parts of the Pharo Mooc by

Damien Cassou, Stéphane Ducasse, Luc Fabressehttp://mooc.pharo.org

Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 Francehttps://creativecommons.org/licenses/by-nc-nd/3.0/fr/