COIS 2240: Object Oriented Analysis and Design 2010 Trent University Kate Gregory
COIS 2240: Object Oriented Analysis and Design 2010 Trent University Kate Gregory
Kate Gregory
Agenda
• Administrivia
• Overview
• Object Oriented Programming Concepts
– Object, attribute, method, message
– Encapsulation
– Inheritance and Polymorphism
• Benefits of OO
Kate Gregory
Administrivia
• Text: class notes from bookstore
– Updated on web if they change during the year
• Grading Scheme: 5 Labs – 35%, Midterm – 25%,
Final – 40%
• Office Hours :
– After class, before class, other times if pre-arranged
• Web Support : www.gregcons.com/2240
• [email protected] anytime
– Email, instant messenger, etc
Kate Gregory
• Interdisciplinary engineer
• Founder, Gregory Consulting Limited, 1986
Kate Gregory
Date Week Topic Hand Out Due Back Test
14-Sep-10 1 Administrivia / Overview / What is an Object? /
Motivation, benefits of OO
16-Nov-10 9 Good Design / Modules & Packages / Metrics Lab 5: Critiques lab 4 5%
7-Dec-10 12 Critiques
??? Final Exam Final 40%
How this course works
• Hour before class – tutorial
– Come if you have q’s – I will be here
• Hour after class – workshop
– Start your assignment with your group
– Ask me questions about class
• Reach me online any time
• Assignments are done in groups
– No exceptions
Kate Gregory
Working in Groups
• Since 1999, students in groups have done
MUCH better than students who work alone
on these labs
• Students report learning a great deal from
their groupmates throughout the term
• Choose wisely
• Group size 3 or 4
– 2 or 5 with my permission
Kate Gregory
“Old Fashioned” Software
Methodologies
• What did developers do before OO?
• What do untrained / self taught developers tend to do?
• Data driven
– Data structures developed initially
– Algorithms developed to work on the above data
• Algorithm driven
– Algorithms developed initially
– Data structures developed to fit the above algorithms
• Both methods are prone to problems since something is
ignored in the initial design
Kate Gregory
Object Oriented Methodology
• Centered on both data (attributes) and algorithms
(methods)
• An object is constructed of both attributes and methods –
neither is ignored during the design of a system.
• Attributes represent state within the object and the
methods represent the “things that the object can do”.
• Example, Employee
– Attributes include : name, salary, email address
– Methods include: issue paycheque for, send email to
Kate Gregory
Object Attributes
• Represents state of the object
• External representation may be independent of the internal
representation.
– Possible representations of colour
• String – “Red”, “Yellow”
• Integer – 0 = black, 255 = white
• Several integers - (0,0,0)= (Red, Green, Blue)
• Object – Red, Yellow
Kate Gregory
Methods
• Methods are the OO concept
– Various languages call them functions, procedures, and
subroutines.
• Methods encapsulate the behaviour of the object,
provide an interface to the object and hide the
internal representation of the object’s attributes.
– Use methods to access the attributes of an object
• Example : myCar.getColour() instead of myCar.Colour.
• Why ?
– Some OO languages expose properties: look like
accessible attributes, act like methods
• Best of both worlds
• Think about what might change, and encapsulate it
Kate Gregory
Object Communication – Messages
Kate Gregory
Messages - Example
• Two objects – Graph, Text Document
• Both objects expose a draw method as part of their
interface
• Graph.Draw( ) and TextDoc.Draw( ) are both legal
calls
– Sure, many languages don’t allow two functions with the
same name, but you can tell them apart and so can the
compiler
• Each method does different things
– drawing a graph is different than drawing a text document
Kate Gregory
Methods
• Sending a message implies calling a method
• The object is responsible for “figuring out”
what to do when the message is received
• Contrast to a traditional function which
would not imply any knowledge or
capability not already known to those who
call it
• The calling code trusts the object to handle
the request appropriately
Kate Gregory
Key Vocabulary
– This section is meant to make you aware of the terms and provide
a simple introduction. More detailed investigations will follow.
• Encapsulation
• Inheritance
• Virtual
• Polymorphism
• Aggregation
Kate Gregory
Encapsulation
• Data Hiding
• Implemented in many OO languages with
keywords such as public, private, protected
– Public: exposed to the whole system.
– Private: internal to the class, not visible to the
whole system
– Protected: exposed only to subclasses (covered
later)
Kate Gregory
S y s te m
P u b lic A t t r ib u t e s
a n d M e th o d s
P r iv a te
A t t r ib u te s
and
m e th o d s
Kate Gregory
Example of Encapsulation: Car engine
Kate Gregory
Encapsulation
• Item to be used in an invoicing application
– Description, price, weight
• How to access description?
– newitem.description
– newitem.GetDescription()
• Effect of design changes
Kate Gregory
Encapsulation
• Invoice holds customer reference, list of
items purchased
• Should invoice print method:
– get customer details and print them?
– Ask customer to print details?
• What happens when customer design
changes?
Kate Gregory
Aggregation
• Objects many contain references to other objects.
• Example, a Car object may contain:
– 4 wheel objects
– 1 engine object
– 1+ seat objects
Car
Kate Gregory
Hierarchy and Inheritance
Motor Vehicle
-colour
-price
-model
+go()
+stop()
+turn left()
+turn right()
Kate Gregory
Inheritance
• From the general to the specific
• Subclasses inherit all attributes and methods from
the super class
• Subclasses often add methods and attributes to
refine or redefine functionality
Kate Gregory
Inheritance example - vehicles
• All vehicles have a Stop method
– involves pressure on the brakes
• One specific kind of car has Anti-Lock
Brakes
– Stop method has different implementation
• Client code just asks for Stop
Kate Gregory
Polymorphism
Motor Vehicle
+colour: ?
+price: ?
+model: ?
+go()
+stop()
+turn left()
+turn right()
Ford Mustang
Toyota Corolla Pontiac Sunfire
+Stop()
+go()
Kate Gregory
Polymorphism
• Imagine an array of MotorVehicles
• Want to do the same thing to all of them
• Send them all a Stop message
• Same message, different behaviours (Anti-
lock etc) from different vehicles
• The correct version of the method will be
called even though the object is being
referred to in more generic terms.
Kate Gregory
Polymorphism
• Must involve an inheritance hierarchy
– All shapes can be drawn
– All employees can be paid
– All documents can be printed
• The details don’t matter to the calling code
Kate Gregory
OO Benefits for Developers
• Less to remember
• Less gotcha bugs
• Freedom to make changes
• Less time on routine and more time on
interesting parts
• Programs tend to succeed
• Proof: developers won’t go back
Kate Gregory
OO Benefits for Users
• Programs are more robust the first time
• Programs get better, not patchy
• Modifications are quicker
• Applications are more likely to solve the
business problem
Kate Gregory
Encapsulation Benefit
• Bank account: balance in pennies or dollars?
• Product: description as character array in
object, or read from database when needed?
• Object consumers benefit because they don’t
need to know
• Object providers benefit because they are
free to change
Kate Gregory
Enforcing Rules
• Remember to:
– open a file before you write to it
– charge a service charge for all withdrawals
– add all deposits to the transaction log
– set the colour before you draw, and reset it
afterwards
Kate Gregory
OO Promotes Reuse
• Class libraries
– with compilers
– for sale by third parties
– free on the net
• Classes from previous projects
• Inheritance - instant reuse
Kate Gregory
Reuse Benefits
• Less work for developer
• A reused class is a tested class
• Reuse boring stuff : linked lists, hash tables,
database access...
• Use your time for unique parts of the
problem
Kate Gregory
For Next class
Kate Gregory