Upload
allen-davidson
View
220
Download
1
Embed Size (px)
Citation preview
Basic Concepts
Introduction to OO Development and
Software Engineering Principles
SYSC 3100 - System Analysis and Design
Everything is an Object!
• You, me, your neighbour, desks, chairs, doors, etc.
• Some of these things are the same, and some of them are different.
• Some of them are “active”, and others are “passive”.
Objectives
• Define objects, attributes and operations.• Differentiate between OOA, OOD and OOP (!)• Describe a Class and an object.• Define information hiding and encapsulation.• Define generalization and specialization and
the isA rule.• Define polymorphism and over-riding.• Describe active objects and persistence.
Object-Oriented Development (OOD)
• Objects are abstractions of real-world or system entities and manage themselves
• Objects are independent and encapsulate state and representation information (attributes).
• System functionality is expressed in terms of object services (operations)
• Shared data areas are eliminated. Objects communicate by message passing
• Objects may be distributed and may execute sequentially or in parallel
Objects, Attributes, Operations
Attributesverticesborder colorfill color
Operationsdrawerasemove
Bicycle objectsBicycle class
abstractinto
abstractinto
Attributesframe sizewheel sizenumber of gearsmaterial
Operationsshiftmoverepair
Polygon objects Polygon class
O-O World View
• An object-oriented system is regarded as a network of cooperating objects which- interact by sending each other messages- maintain their own state- have an individual identity
state o3
o3:C3
state o4
o4: C4
state o1
o1: C1
state o6
o6: C1
state o5
o5:C5
state o2
o2: C3
ops1() ops3 () ops4 ()
ops3 () ops1 () ops5 ()
Advantages of OOD
• Easier maintenance. Classes may be understood as stand-alone entities, including their own functionality and data
• Classes are appropriate reusable components• For some systems, there may be an obvious
mapping from real world entities to system classes, e.g., accounts in banking system
Object-oriented development
• Object-oriented analysis (OOA), design (OOD) and programming (OOP) are related but distinct
• OOA is concerned with developing an object model of the application domain
• OOD is concerned with developing an object-oriented system model (structure, behaviour) to implement requirements
• OOP is concerned with realising an OOD using an OO programming language such as Java or C++
Principal Concepts
– Objects, classification– encapsulation and information hiding– modularity– generalization and inheritance– typing– polymorphism– dynamic binding– concurrency– persistence
Classes and Objects
•Object–A chunk of structured data in a running software
system –Has properties
• Represent its state
–Has behaviour• How it acts and reacts• May simulate the behaviour of an object in the real world
–Has an identity: each object is unique
High-level Examples
Class
Person. ‘Hussain Pervez.’ Speak, walk, read.Studying, resting, qualified.
Shirt.My favourite button white denim shirt.
Shrink, stain, rip. Pressed, dirty, worn.
Sale. Sale no #0015, 16/06/02.
Earn loyalty points. Invoiced, cancelled.
Identity Behaviour State
Bottle of ketchup.
This bottle of ketchup.
Spill in transit. Unsold, opened, empty.
Objects
Margaret:
date of birth: 1980/03/03
position: Teller
Transaction 487:
amount: 200.00time: 2001/09/01 14:30
Greg:
date of birth: 1970/01/01address: 75 Object Dr.
Mortgage Account 29865:
balance: 198760.00
opened: 2000/08/12property: 75 Object Dr.
Instant Teller 876:
location: Java Valley Cafe
Savings Account 12876:
balance: 1976.32opened: 1997/03/03
Jane:
date of birth: 1955/02/02
position: Manageraddress: 99 UML St.
address: 150 C++ Rd.
Classes
•A class:–Is a unit of abstraction in an object oriented analysis,
design, or program–Represent instances of real-world and system entities–Represents similar objects
• Its instances
–Is a kind of software module• Describes its instances’ structure (properties)• Contains operations/methods to implement their behaviour
Classes and Objects: Example
MarySharp:Person
name=“Mary Sharp”birthdate=16 March 1950
JoeSmith:Person
name=“Joe Smith”birthdate=21 October 1983
name: string
Person
birthdate: date
Class with Attributes Objects with Values
name
Person
changeJob
fileName
File
GeometricObject
color
move (delta : Vector)
sizeInByteslastUpdate
birthdate position
changeAddress select (p : Point): Booleanrotate (in angle : float = 0.0)
Encapsulation and Information Hiding
Encapsulation: The technique of hiding details which are not needed by the user of an abstraction
Applied in object-oriented systems by the separation of object interfaces and bodies
A_STACK
PUSHPOP
interface
body
client’s view
Message-passing and Encapsulation
Message from another object requests a service.
Operation called only via valid operation signature.
Data accessed only by object’s own operations.
An object’s data is hidden
(encapsulated).
‘Layers of an onion’ model of an object:
An outer layer of operation signatures…
…gives access to middle layer of operations…
…which can access inner core of data
Object-Oriented Computation Model
draw-line
clear
Attributes #3
#4
remove
insert
Attributes #4
#2
remove
insert
Attributes #2
#3
update
draw
Attributes #1
#1
control flow data flowsystem operation
Organizing Classes into Inheritance Hierarchies
•Superclasses–Contain features common to a set of subclasses
•Inheritance hierarchies–Show the relationships among superclasses and
subclasses•Inheritance–The implicit possession by all subclasses of
features defined in its superclasses
An Example Inheritance Hierarchy
Account
MortgageAccountSavingsAccount ChequingAccount
•Inheritance–The implicit possession by all subclasses of
features defined in its superclasses
Generalization and Specialization
• Classification is hierarchic in nature• Generalization: the technique of factoring out
common properties into shared classes– facilitates reuse and change control– the reverse operation is the specialization
• For example, a person may be an employee, a customer, a supplier of a service
• An employee may be paid monthly, weekly or hourly• An hourly paid employee may be a driver, a cleaner,
a sales assistant
Specialization Hierarchy
Person
Employee Customer Supplier
monthly paid weekly paid hourly paid
Driver Cleaner Sales assistant
More general(superclasses)
More specialized(subclasses)
The Isa Rule
•Always check generalizations to ensure they obey the “isa” rule–“A checking account is an account”–“A village is a municipality”
•Should ‘Province’ be a subclass of ‘Country’?–No, it violates the isa rule
• “A province is a country” is invalid!
Inheritance hierarchy of mathematical objects
Rectangle
QuadrilateralCircle
Ellipse Polygon PlaneLine
Shape3DShape2D
MatrixShape Point
MathematicalObject
Make Sure all Inherited Features Make Sense in Subclasses
Account
balanceopenedcreditOrOverdraftLimit
creditdebitcalculateInterest
MortgageAccount
collateralPropertycollateralValue
setCollateralValue
ChequingAccount
highestChequeNumber
withdrawUsingChequecalculateServiceCharge
SavingsAccount
Methods, Operations and Polymorphism
•Operation–A higher-level procedural abstraction that specifies a
type of behaviour–Independent of any code which implements that
behaviour• E.g., calculating area (in general)
–Operations are defined during Analysis and Design–They are implemented with methods
Methods, Operations and Polymorphism
•Method–A procedural abstraction used to implement the
behaviour of a class.–Several different classes can have methods with the
same name• They implement the same abstract operation in ways
suitable to each class • E.g, calculating area in a rectangle is done differently from
in a circle
Polymorphism
•A property of object oriented software by which an abstract operation may be performed in different ways in different classes.–Requires that there be multiple methods of the same
name–The choice of which one to execute depends on the
object (i.e., type) that is in a variable, on which the method is executed
–Reduces the need for programmers to code many if-else or switch statements
Overriding
•A method would be inherited, but a subclass contains a new version instead–For restriction
• E.g. scale(x,y) would not work in Circle
–For extension• E.g. SavingsAccount might charge an extra fee
following every debit
–For optimization• E.g. The getPerimeterLength method in Circle is
much simpler than the one in Ellipse
How a decision is made about which method to run
1. If there is a concrete method for the operation in the current class, run that method.
2. Otherwise, check in the immediate superclass to see if there is a method there; if so, run it.
3. Repeat step 2, looking in successively higher superclasses until a concrete method is found and run.
4. If no method is found, then there is an error– In Java and C++ the program would not have
compiled
Dynamic binding
•Occurs when decision about which method to run can only be made at run time–Needed when:
• A variable is declared to have a superclass as its type, and• There is more than one possible polymorphic method that
could be run among the type of the variable and its subclasses
Abstract Classes and Methods
•An operation should be declared to exist at the highest class in the hierarchy where it makes sense
– The operation may be abstract (lacking implementation) at that level– If so, the class also must be abstract
• No instances can be created• The opposite of an abstract class is a concrete class
– If a superclass has an abstract operation then its subclasses at some level must have a concrete method for the operation
• Leaf classes must have or inherit concrete methods for all operations• Leaf classes must be concrete
Concurrency
•The nature of objects as self-contained entities make them suitable for concurrent implementation.
•There are two forms of objects in concurrent systems.•Passive:
–operations execute only in response to external stimuli–call other objects only in response to external stimuli
•Active–operations may execute asynchronously–has an independent thread of control–call other objects “spontaneously”–change state “spontaneously”
Active transponder object
• Active objects may have their attributes modified by operations but may also update them autonomously using internal operations
• Transponder object broadcasts an aircraft’s position. The position may be updated using a satellite positioning system. The object periodically update the position by triangulation from satellites
An active transponder objectclass Transponder extends Thread {
Position currentPosition ;Coords c1, c2 ;Satellite sat1, sat2 ;Navigator theNavigator ;
public Position givePosition (){
return currentPosition ;}
public void run (){
while (true){
c1 = sat1.position () ;c2 = sat2.position () ;currentPosition = theNavigator.compute (c1, c2) ;
}
}
} //Transponder
Persistence
• The ability of an object to exist after the termination of the program which created it
• Allows data storage in terms of objects (e.g. files are objects)• Provides a model for interaction with a DBMS or FMS• E.g., Java serialization, Object-oriented DBMS
OO programDatabase
Summary
• An object is some “thing” which has attributes (variables) and operations that they can perform
• A class is the blueprint or template that describes objects that have similar characteristics.
• Big idea: hide implementation of objects to make it easy to change without affecting the world.
• Polymorphism allows different types to implement common operations differently.
• “Active objects” operate spontaneously and independently of each other (like us!).