Sei sulla pagina 1di 35

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
• kate@gregcons.com anytime
– Email, instant messenger, etc
Kate Gregory
• Interdisciplinary engineer
• Founder, Gregory Consulting Limited, 1986

• Mentor, Architect, Designer • Programmer


• Speaker – .NET (VB.NET, C#)
– Tech Ed, Tech Ed Europe – C++, C++/CLI
– Tech Ed Africa – XML
– INETA Speakers Bureau • Author
– C++ Connections – Building Internet Applications with Visual
C++
• Microsoft Regional Director, – Special Edition Using Visual C++ (4
MVP (C++) editions)
– Visual C++ .NET Kickstart
• Corporate Trainer – Whitepapers for Microsoft
– IBM, Nortel, MCI, CTV...
• Developer Community
– C++, OO Concepts, XML, C#,
– www.gregcons.com/kateblog
VB, .NET, Vista
programming, Windows 7 – Twitter: @gregcons
programming, ... – East of Toronto .NET User Group
Overview – Topics Covered
• OO • Inheritance
• Use Cases • Polymorphism
• CRC Cards
• Class Associations
• Diagramming Techniques
– Collaboration
• Design Patterns
– Class • Unified Modelling
– Interaction Language
– State
– Activity
– Deployment

Kate Gregory
Date Week Topic Hand Out Due Back Test
14-Sep-10 1 Administrivia / Overview / What is an Object? /      
Motivation, benefits of OO

21-Sep-10 2 Use Cases Lab 1: Use cases


28-Sep-10 3 CRC Cards, collab graphs Lab 2: CRC cards lab 1 5%

5-Oct-10 4 start class diag lab 2 5%


12-Oct-10 5 Finish class diag, Associations / Visibility Lab 3: Class Diag

19-Oct-10 6 Inh & Polymorphism   lab 3 5%  


26-Oct-10 Reading Break
2-Nov-10 7 Interaction diag / Design Patterns /midterm review Lab 4: Interaction Diag    

9-Nov-10 8 no lecture - midterm     Midterm 25%

16-Nov-10 9 Good Design / Modules & Packages / Metrics Lab 5: Critiques lab 4 5%  

23-Nov-10 10 State diagrams / Activity diagrams / Deployment      


diagrams / Summary and Conclusion / The Future

30-Nov-10 11 Critiques critique lab (before


class) 15%

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

• Messages are requests by one piece of code for the


object to do its thing
– Different objects might run different code for the same
message
– This is actually vital to the success of object orientation

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

• Different cars have different engines and mechanisms (fuel


injection, turbo, etc)
• Interface between driver and the car is common: gas pedal.
Push for faster, don’t push for slower.
• All drivers can accelerate all cars. Details of the engine are
encapsulated inside the hood, and the driver can ignore
them.

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

Seat Engine Wheel

Kate Gregory
Hierarchy and Inheritance
Motor Vehicle
-colour
-price
-model

+go()
+stop()
+turn left()
+turn right()

Bus Truck Car

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()

Bus Truck Car

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

• Think about things in the real world with a common


interface but different underlying mechanisms:
– Cars
– Phones (pay phone, home phone, cordless, cell, digital cell, ...)
– Elevators
• Form a group for your assignments all term
• Plan to download copy of assignment
– Due Sept 28th
• Read ahead: use cases

Kate Gregory

Potrebbero piacerti anche