learning to program with c# - 61 unit 6 reflection on the c# model of computation introduced so...

12
Learning to Program with Learning to Program with C# - 6 C# - 6 1 Unit 6 Unit 6 Reflection on the C# Model of Reflection on the C# Model of Computation introduced so far Computation introduced so far the corner stones of the object- the corner stones of the object- oriented model oriented model Why is it like this? Why is it like this? We have considered modelling We have considered modelling aspects only so far aspects only so far What else has caused the computing What else has caused the computing world to build a model like this? world to build a model like this?

Upload: samuel-skinner

Post on 03-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

11

Unit 6Unit 6

• Reflection on the C# Model of Reflection on the C# Model of Computation introduced so farComputation introduced so far– the corner stones of the object-the corner stones of the object-

oriented modeloriented model• Why is it like this?Why is it like this?

– We have considered modelling We have considered modelling aspects only so faraspects only so far

– What else has caused the computing What else has caused the computing world to build a model like this?world to build a model like this?

Page 2: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

22

Introduced so far…Introduced so far…• ObjectsObjects

– the fundamental building block of the modelthe fundamental building block of the model

• ClassesClasses– factories that create objectsfactories that create objects

• MethodsMethods– defined by a class to operate on objects of that class. May defined by a class to operate on objects of that class. May

adjust internal data stored in the object or class, or call up adjust internal data stored in the object or class, or call up operations on other objectsoperations on other objects

• InheritanceInheritance– enables the reuse of existing classes in the construction of enables the reuse of existing classes in the construction of

new classesnew classes

• PolymorphismPolymorphism– enables viewing of an object in different waysenables viewing of an object in different ways

Page 3: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

33

Modelling aspectModelling aspect• This structure introduced as a result of the need to This structure introduced as a result of the need to

model the real worldmodel the real world– the world is made up of objectsthe world is made up of objects– they are naturally classifiedthey are naturally classified

• the books, the chairs, the rockets, the peoplethe books, the chairs, the rockets, the people

– our use of objects requires us to operate on themour use of objects requires us to operate on them• we open a book: book.Open()we open a book: book.Open()• we ask someone their name: person.GetName()we ask someone their name: person.GetName()

– engineering techniquesengineering techniques• we reuse technology as often as we are ablewe reuse technology as often as we are able• e.g. a screwdriver is reused in countless problem solutionse.g. a screwdriver is reused in countless problem solutions

– backwards and forwards compatibilitybackwards and forwards compatibility• we work with standards in the worldwe work with standards in the world• they allow new and old technology to co-exist successfullythey allow new and old technology to co-exist successfully

Page 4: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

44

Another Motivation - Another Motivation - SafetySafety• The class model brings safetyThe class model brings safety

– safety: a guarantee that components of a system safety: a guarantee that components of a system operate together without damage to internal dataoperate together without damage to internal data

– for previous programmers, class safety & for previous programmers, class safety & typetype safety are safety are synonymoussynonymous

• The safety depends on the external structure of an The safety depends on the external structure of an object defined by its classobject defined by its class– its visible methods and data membersits visible methods and data members

• AnalogyAnalogy– Power adaptors for laptop computers are usually Power adaptors for laptop computers are usually

uniquely shaped to the power level they supplyuniquely shaped to the power level they supply– This shape (external structure) ensures that we don't This shape (external structure) ensures that we don't

mix the wrong supply and computermix the wrong supply and computer

Page 5: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

55

Class as safety Class as safety mechanismmechanism• All objects are categorised according to the class All objects are categorised according to the class

that produced themthat produced them• All methods specify the class of any parameter All methods specify the class of any parameter

objects they receiveobjects they receive• HenceHence

– Every call of a method can be checked for safety before Every call of a method can be checked for safety before execution, byexecution, by• noting the class of all parameters to the method in that method's noting the class of all parameters to the method in that method's

definitiondefinition• checking that each parameter supplied at the call site is of the checking that each parameter supplied at the call site is of the

expected typeexpected type

– If this check fails, this method call cannot runIf this check fails, this method call cannot run• usually this check is performed before the program is executed at usually this check is performed before the program is executed at

all, sometimes it is just before the call itselfall, sometimes it is just before the call itself

Page 6: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

66

Repercussions of class Repercussions of class safetysafety

• Forget all about inheritance & interfaces Forget all about inheritance & interfaces for a minutefor a minute– let's see what it's like without themlet's see what it's like without them– the point here is to show why we need them!the point here is to show why we need them!

• So, an object is associated with only ONE So, an object is associated with only ONE classclass

• Say we have four classesSay we have four classes– a desk, a bicycle, a fridge and a televisiona desk, a bicycle, a fridge and a television– they all have a method GetWeightthey all have a method GetWeight

Page 7: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

77

Working with objects of Working with objects of different classesdifferent classes

– To find the weight of a collection of the four kinds To find the weight of a collection of the four kinds of objects, I must treat them all differently – of objects, I must treat them all differently – adding desks, cycles etc independently, then adding desks, cycles etc independently, then adding the totalsadding the totals• ie I need to write a separate code fragment for each ie I need to write a separate code fragment for each

kind of object to do the additionkind of object to do the addition• why? remember that code can only operate over why? remember that code can only operate over

objects of a single classobjects of a single class

– This is inflexible and tediousThis is inflexible and tedious• additional coding for each class involvedadditional coding for each class involved• adding code doesn't gracefully adapt to the appearance adding code doesn't gracefully adapt to the appearance

of new classesof new classes

Page 8: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

88

What is the dilemma here?What is the dilemma here?

• Safe application of operations…Safe application of operations…– has been determined by a match on the whole has been determined by a match on the whole

class structureclass structure

• Intuitively, we notice that…Intuitively, we notice that…– safe application only requires that the safe application only requires that the

particular operation expected at the call site particular operation expected at the call site matches the operation available in the object matches the operation available in the object present at executionpresent at execution

• Hence we could define safety on subsets Hence we could define safety on subsets of the whole class's structureof the whole class's structure– which is exactly what interfaces allow us to dowhich is exactly what interfaces allow us to do

Page 9: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

99

Trust & Flexibility vs Trust & Flexibility vs SafetySafety

– The interface requires us to trust that if many The interface requires us to trust that if many operations have the same name and signature operations have the same name and signature (parameters), then they are compatible(parameters), then they are compatible• for example, we assume that all GetWeight methods are for example, we assume that all GetWeight methods are

operating in the same wayoperating in the same way

– Levels of safetyLevels of safety• if all objects come from one class, high confidence that they if all objects come from one class, high confidence that they

are compatibleare compatible• if they come from a number of classes, all implementing one if they come from a number of classes, all implementing one

interface, we necessarily must be less certain…interface, we necessarily must be less certain…– but we've gained a level of flexibility not otherwise availablebut we've gained a level of flexibility not otherwise available

– Programming often involves trade-offs – here is Programming often involves trade-offs – here is anotheranother

Page 10: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

1010

So why have inheritance?So why have inheritance?

• Inheritance & interfaces often confusedInheritance & interfaces often confused– because the polymorphism offered by because the polymorphism offered by

interfaces is also offered by classes related by interfaces is also offered by classes related by inheritanceinheritance

• Fundamental value of inheritanceFundamental value of inheritance– enables a series of classes to be related enables a series of classes to be related

together according to their common partstogether according to their common parts• common members only specified once, in base classcommon members only specified once, in base class• can be accessed in objects of all classes derived from can be accessed in objects of all classes derived from

the base classthe base class

– increases efficiency of production and flexibilityincreases efficiency of production and flexibility

Page 11: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

1111

Polymorphic aspects of Polymorphic aspects of inheritanceinheritance

– Firstly,Firstly,• Sharing of methods is a form of polymorphic Sharing of methods is a form of polymorphic

behaviour – the code of the one common method behaviour – the code of the one common method can operate on objects of all derived classescan operate on objects of all derived classes

– Additionally,Additionally,• the relationship defined between objects using the relationship defined between objects using

inheritance – "is a" – introduces the same inheritance – "is a" – introduces the same fragmentation of the class as did the interfacefragmentation of the class as did the interface

• type safety can be specified on the base class only – type safety can be specified on the base class only – effectively, the base class is the interfaceeffectively, the base class is the interface

• no matter what additional operations the derived no matter what additional operations the derived classes have, classes have, as long as we view them with base as long as we view them with base class spectaclesclass spectacles, we can mix safely mix objects from , we can mix safely mix objects from all derived classesall derived classes

Page 12: Learning to Program with C# - 61 Unit 6 Reflection on the C# Model of Computation introduced so farReflection on the C# Model of Computation introduced

Learning to Program with C# Learning to Program with C# - 6- 6

1212

SummarySummary

• Reviewed the model so farReviewed the model so far– objects, classes, methods, inheritance, objects, classes, methods, inheritance,

polymorphismpolymorphism

• These are the defining features of the These are the defining features of the object-oriented modelobject-oriented model– they separate it from the other major models they separate it from the other major models

of computationof computation

• We now need to explore some of the We now need to explore some of the other features of the modelother features of the model– common to both the imperative and common to both the imperative and

functional models of computationfunctional models of computation