Object Orientation What, How and Why Will van Beek Independent Consultant 18 march 2012
© 2005 Progress Software Corporation2 © proWill What is Object Orientation ? Basics & Principles Why use Object Orientation ? How to use Object Orientation Demo – the Animal Farm Object Orientation – What, How and Why
© 2005 Progress Software Corporation3 © proWill Basics of Object Orientation. Abstraction - what is Abstraction ?
© 2005 Progress Software Corporation4 © proWill What is Abstraction ? Plato’s cave Basics of Object Orientation.
© 2005 Progress Software Corporation5 © proWill What is Abstraction ? Plato’s cave. Abstraction = model / derivative of reality. Modeling Grouping relevant properties and behavior in unambiguous classes. Basics of Object Orientation.
© 2005 Progress Software Corporation6 © proWill Abstraction = model of reality What is a Class(ification) ? Aristotle Animal sponges worms arthropods vertebrates flies lobsters spiders reptiles mammals Basics of Object Orientation.
© 2005 Progress Software Corporation7 © proWill Abstraction = model of reality Class = abstract definition of properties, behavior and relations to other classes What is an Object ? Concrete instance of a class. Basics of Object Orientation. Example
© 2005 Progress Software Corporation8 © proWill Example Person UML Classdiagram Name Class Properties Behavior payAttention break askQuestion attentionLevel satisfactionLevel PreReqsOK Basics of Object Orientation.
© 2005 Progress Software Corporation9 © proWill What is Object Orientation ? Basics & Principles Why use Object Orientation ? How to use Object Orientation Demo – the Animal Farm Object Orientation – What, How and Why
© 2005 Progress Software Corporation10 © proWill Delegation Is it really your responsibility ? Principles of Object Orientation
© 2005 Progress Software Corporation11 © proWill Principles of Object Orientation Encapsulation Interface Implementation
© 2005 Progress Software Corporation12 © proWill Polymorphism Principles of Object Orientation Interface Implementation comes in many shapes
© 2005 Progress Software Corporation13 © proWill Polymorphism Start() Marathon EngineDrivingDeviceDiggingSailing Plane MachineProgramSkatingVomittingWritingPinball Principles of Object Orientation
© 2005 Progress Software Corporation14 © proWill Inheritance Is-a Engine (generic) Engine (Ford) Engine (Volvo) Engine (Volkswagen) Principles of Object Orientation
© 2005 Progress Software Corporation15 © proWill Delegation Encapsulation Polymorphism Inheritance Principles of Object Orientation – Recap
© 2005 Progress Software Corporation16 © proWill What is Object Orientation Why use Object Orientation ? Differences with Procedural Programming How to use Object Orientation Demo – the Animal Farm Object Orientation – What, How and Why
© 2005 Progress Software Corporation17 © proWill Differences with Procedural Programming.
© 2005 Progress Software Corporation18 © proWill The Reality of Procedural Programming define variable hProc as handle no-undo. for each superProc: run value(superProc.fileName) persistent set hProc. session:add-super-procedure(hProc). end.
© 2005 Progress Software Corporation19 © proWill The Reality of Procedural Programming
© 2005 Progress Software Corporation20 © proWill The Reality of Procedural Programming
© 2005 Progress Software Corporation21 © proWill The Reality of Procedural Programming Rigidity Fragility Immobility Can we do better ?
© 2005 Progress Software Corporation22 © proWill What is Object Orientation ? Why use Object Orientation ? How to use Object Orientation Principles of sound design Demo – the Animal Farm Object Orientation – What, How and Why
© 2005 Progress Software Corporation23 © proWill Principles of Good / Sound Design. Low & Loose Coupling (Law Of Demeter) Don’t talk to strangers ! An object knows only about himself and its direct parent (Superclass) The principle of least knowledge High coupling inextricably Lots of classes with different functionality inextricably connected Swiss Knife Classes
© 2005 Progress Software Corporation24 © proWill Principles of Good / Sound Design. High Cohesion Do only 1 thing ! All other responsibilities have been moved to other classes The principle of single responsibility Low Cohesion Different functionalities in 1 class
© 2005 Progress Software Corporation25 © proWill What is Object Orientation ? Why use Object Orientation ? How to use Object Orientation Demo – the Animal Farm the Procedural way the Object Oriented way Object Orientation – What, How and Why
© 2005 Progress Software Corporation26 © proWill Example – the Animal Farm
© 2005 Progress Software Corporation27 © proWill Demo
© 2005 Progress Software Corporation28 © proWill The Procedural way Encapsulation Delegation
© 2005 Progress Software Corporation29 © proWill The Procedural way Fragile Rigid
© 2005 Progress Software Corporation30 © proWill The Object Oriented way Let’s talk Interface – how to operate the coffee machine Where to put the cup, the coffee & the water How to turn it On/Off
© 2005 Progress Software Corporation31 © proWill The Object Oriented way Let’s talk Interface – how to operate the animals makeSound
© 2005 Progress Software Corporation32 © proWill The Object Oriented way – the iAnimal interface class function makeSound returns logical private().
© 2005 Progress Software Corporation33 © proWill Access Modifiers AccessModifier - private + public define public variable cTest as character no-undo.define private variable cTest as character no-undo.define protected variable cTest as character no-undo. # protected sub super
© 2005 Progress Software Corporation34 © proWill Polymorphism – one interface, multiple implementations iAnimal.cls + makeSound cat.cls + makeSound dog.cls + makeSound cow.cls + makeSound borg.cls + makeSound System.Media. Soundplayer + SoundLocation + Play 1. Initiate the animal’s vocal cords 2. Specify the name of the sound file 3. Play the sound file 1. Initiate the animal’s vocal cords 2. Specify the name of the sound file 3. Play the sound file
© 2005 Progress Software Corporation35 © proWill Inheritance – a cat/dog/cow/borg is-an animal iAnimal.cls + makeSound System.Media. Soundplayer + SoundLocation + Play cat.cls + makeSound dog.cls + makeSound cow.cls + makeSound borg.cls + makeSound animal.cls - vocalCords # soundFile - initializeVocalCords + makeSound
© 2005 Progress Software Corporation36 © proWill Encapsulation / Delegation – vocalCords iAnimal.cls + makeSound System.Media. Soundplayer + SoundLocation + Play cat.cls + makeSound dog.cls + makeSound cow.cls + makeSound borg.cls + makeSound animal.cls - vocalCords # soundFile - initializeVocalCords + makeSound
© 2005 Progress Software Corporation37 © proWill Implementations – the animal class
© 2005 Progress Software Corporation38 © proWill Implementations – the cat class
© 2005 Progress Software Corporation39 © proWill Implementations – the dog class
© 2005 Progress Software Corporation40 © proWill Implementations – the cow class
© 2005 Progress Software Corporation41 © proWill Implementations – the borg class
© 2005 Progress Software Corporation42 © proWill Encapsulation / Delegation – animalFarm animalFarm.cls - Animal as iAnimal + visitAnAnimal(pcName)
© 2005 Progress Software Corporation43 © proWill Encapsulation / Delegation – visit animalFarm.cls - Animal as iAnimal + visitAnAnimal(pcName) visit.cls + animalFarm start.p
© 2005 Progress Software Corporation44 © proWill Implementations – the animalFarm class
© 2005 Progress Software Corporation45 © proWill Implementations – the visit class
© 2005 Progress Software Corporation46 © proWill Implementations – the start procedure
© 2005 Progress Software Corporation47 © proWill Questions?
© 2005 Progress Software Corporation48 © proWill Thanks you for your time
© 2005 Progress Software Corporation49 © proWill proWillvanBeek Keep in touch