More on Objects and Classes. What is OOAD? understanddocument real world situation Analysts must...

60
More on More on Objects and Classes Objects and Classes

Transcript of More on Objects and Classes. What is OOAD? understanddocument real world situation Analysts must...

More on More on Objects and ClassesObjects and Classes

What is OOAD?

• Analysts must understandunderstand and documentdocument a real world situationreal world situation.

• Designers then create a product in the in the computer world,computer world,

• The product is intended to help the users help the users do their job do their job in the real world.

• Effective analysts Effective analysts must ensure the computer-world product accurately mirrors the users’ real-world needs.real-world needs.

What An Object Is:

• Dictionaries:– A thing– Can be seen, touched or otherwise sensed– Someone is aware of it– A material thing– A thing in a picture

• In other words, an object is a THING.THING.

THINGSTHINGS

• An object is some ThingThing in the world of the user– Car, Train, Elephant, Sale, Invoice, Division, Account, etc.– Physical or Conceptual

• It has AttributesAttributes that describe it:– Make, Model, Year, – Color, Weight, – Serial Number, License Number

• It has RelationshipsRelationships to other objects:– e.g., to a Person object, i.e., the Owner.

• It has BehaviorBehavior that it can do:– Create– Change Color– Change Owner– Destroy Itself (Suicide?)

Entity

Object

in the DATA WORLD• We have some kind of record in the computer for each real-

world object• It carries data for the attributes

– Make, Model, Year, Color, Weight, Serial Number, License Number– The set of attribute values represents the STATE of the object

• It carries a link of some kind for each relationship– Foreign Key or pointer

• It carries program code for each behavior– Create, Change Owner, Change Color, Delete Itself (Suicide?)– CRUD: Create, Read , Update, Delete.

In the Data World:

• Mostly, a Data-World Object is a RepresentationRepresentation of a Real-World Object.

• And since it carries only– The datadata we need for the job at hand, and– The behaviorbehavior (program code) for the job at hand

• This makes it an abstractionabstraction of the real-world object.

So…..So…..

A Data Object Data Object is an abstractionabstraction of some thingthing in the real world, that

carries both the datadata describing the real-world object , and the

operationsoperations (i.e., program codeprogram code) that have the

only allowable access only allowable access

to that data.to that data.

Real-World vs Data-World Objects

• In an OOPL, an object is created as a piece of RAM

• Organized into fields (data elements) for the attributes.

• The program code is part of the object..• It is made available as soon as the object is

created.

Inheritance and Aggregation

Remember Daisy?

Daisy: Cow

Colour = BrownPattern = none

Weight = 400kg

Cow

NameWeightPattern

Produce MilkGo “Moo”Eat Grass

ObjectClass

Thelma and Madge

Thelma Madge CowName

WeightPattern

Produce MilkGo “Moo”Eat GrassDaisy

All belong to the class COW

Introducing

Silver

Horse

NameWeightHeight

RunsJumps

Eat Grass

and

Dolly

Sheep

NameWeight

Wool colour

Goes “Baah”Produces wool

Eat Grass

Madge, Silver and DollyShare some attributes and operations

CowName

WeightPattern

Produce MilkGo “Moo”Eat Grass

HorseName

WeightHeightRuns

JumpsEat Grass

SheepName

WeightWool colourGoes “Baah”

Produces woolEat Grass

New Class

AnimalName

Weight

Eat Grass

This class contains all the shared attributes and operations of the three other classes

The concept of inheritanceIf we were to model sheep, cows and horses in one diagram we may well use the new animal class to make the process much more economical. The diagram would then look like this:

AnimalName

WeightEat Grass

Cow

PatternProduce milk

Go “moo”

Sheep

Wool colour

Produce woolGo “Baah”

Horse

HeightJumpRun

The concept of inheritance• By doing this we are saying that

the Cow, Horse and Sheep classes can INHERIT all the attributes from the animal class.

• In the Java programming language, in this instance, the Animal class is known as a SUPERCLASS and the Cow, Horse and Sheep classes are SUB-CLASSES

• In VB.NET the Animal class is known as the BASE class and the Cow, Sheep and Horse classes are called the DERIVED classes.

Superclass

Sub-class Sub-class

The concept of inheritance• So in addition to the Horse having

Height as an attribute, it also

inherits Name and Weight.

• And as well as having the

operations Jump and Run, it also

inherits Eat Grass

AnimalName

WeightEat Grass

Horse

HeightJumpRun

Inheritance

• Classes can be arranged in inheritance hierarchies• A class inherits operations and attributes from its parent• This is one of the underlying mechanisms for providing for

design and code re-use• We use a special relationship to indicate this on the class

diagram:

parentchild

Inheritance - example

student

coursemodules taken

person

nameaddress

Lecturer

modules taught

Suppose we have already defined the notion of person as an objectWe now want to add a new kind of person - a student

So we make use of the existing person definition, and introduce an inheritance relationshipNow students have the same attributes as person, plus some new ones of their ownAnd likewise, we can create a new type of person, a lecturer, that inherits from person

Relationships are inherited

student

coursemodules taken

Lecturer

modules taught

person

namecompanyaddress

Suppose that we find that person and company share addressWe can split address out into a separate object And then add in some relationships Now if we add in a student And make the student inherit from person Then students have addresses too by inheritance And we can do the same for a lecturer

Inheritance - jugglingjugglingObject

weight

throw()catch()pickUp()drop()

jugglingBall

club

rotationSpeed

ball

diameter

tennisBall

bounce()

egg

smash()

Consider juggling objects

There are three types of thingto juggle.

And balls can be further refined

So tennis balls can be bounced, thrown, caught, picked up and dropped

Inheritance goes all the way down the hierarchy

Inheritance - Vehicles

VehicleEngineSeats

start()stop()

Lorry

Carry goods()

Taxi

Carry Fares()

MotorcyclePillion

Wheelie()Trailer

Meter

ShapesQuadrilateral

Square Rectangle Parallelogram Trapezoid

All these shapes share features of the Quadrilateral superclass but also have

specific features that are unique to themselves.

AndRugby playerPositionNo of capsTeam

Kick()Run()Tackle()Ruck()

Fly Half

Take penalty()Drop kick()

Jonny Wilkinson

kick world cup winning goal()

Forward

Scrum()

Be careful though – Magnitude Inheritance example

Magnitude

compare()print()

Coordinate

add()subtract()multiply()

Integer

add()subtract()multiply()

Why not push add, subtract, multiply up the hierarchy?

Because, though they have the same name, they do different things!

Finding inheritance

• There are two methods of finding Inheritance• GENERALISATION – looking for commonality amongst

similar objects• If two things are similar, are there common aspects

(e.g. employee and customer)

• SPECIALISATION – providing additionality• If you want to create something new, is there

something similar to inherit from (e.g. web customer might be a special case of customer)

Aggregation

• Objects can be made up of other objects, e.g. a car is made up of engine, body, transmission and wheels.

• Aggregation hierarchies are used to describe this

• There is no inheritance in an aggregation hierarchy

• We use a special relationship to indicate aggregation

wholepart

Aggregation - Orders

OrderHeaderOrderNoDateCustomer No

OrderSummary

GetTotal()

OrderLineItemNoQty

Order1

1

1

1

1

1

1

1

1

1..n1..n

1

Aggregation - OrdersBLOGGS FISH Co

Order No BF123

Date 26/11/2003

Cust No CC12432

Item No Qty

RV33 6

TL41 3

PQ36 11

LF50 22

Total £345.85

Invoice to be settled within 28 days

Thank you for your order

ORDER

Order Header

Order Lines

Order Summary

Aggregation - PathwaysPathway

Module

Lecture Assessment

Finding aggregation

• Break complicated objects into their parts

• Build objects out of existing objects

Inheritance and Aggregation• Are important types of relationships

• Allow developers to re-use existing designs

and implementations

• Adds more meaningful structures

• Reduces the amount of development and

design time that is needed.

Conclusion• Inheritance and aggregation are special types of

relationship

• Inheritance passes attributes and operations

• Aggregation does not pass attributes and operations

• Inheritance allows for re-use of designs and

implementation

Real-World vs Data-World Objects

Jacobson’s Three Types

Jacobson’s Three Types

In the interests of stability and resilience to change, Jacobson (pron. Yahcobson) classifies objects into three

types:

• Entity Objects• Interface Objects• Control Objects

Let’s look at these in detail. . .Let’s look at these in detail. . .

Jacobson’s Three Types

• Entity Objects– These are the ones we have spent most time

discussing.– They represent objects in the users’ real world.– All relevant behavior associated with the

corresponding real-world object is modeled by the methods.

Jacobson’s Three Types

Definition:Definition:

Entity Objects Entity Objects are the fundamental objects and classes that we discover in Analysis by

scanning the users’ businessscanning the users’ business..

Jacobson’s Three Types

Interface Objects:

Most software systems need to exchange data with other systems.

Jacobson’s Three Types

Interface Objects:

These might be:– Other information systems, such as

• Human Resources, Accounting, etc., etc.

– A real-world system controlled by our software• Industrial processes, refineries, etc.• Machinery, robots, assembly lines, etc.

– Monitoring systems• Security systems, Intensive Care Units, etc.

Interface Objects:

• These days, they might also be:– Communications Interfaces

• Modem and phone line• Fax• Peripheral devices e.g. printers, scanners, sound and

video Input/Output.

• Graphical User Interfaces (GUIs)

Interface Objects:

• Suppose you developed a system that needed to communicate over modem lines.

• For simplicity, during testing you might just plug two serial ports together from one machine to another.

Interface Objects:

• When the software is deployed, we find things have changed -

it must communicate over a LAN.

• And then what if the needs grew and the users wanted it to work across the Internet?

Well, . .Well, . .

Interface Objects:

• To handle this, we would hide all the processing to do with the comm link within an object.

• When the changes happen, we rewrite some of the methods within this class,

• And add an attribute or two.

Nothing outside this class needs to be

touched!!

Interface Objects,

Advantages:• The complexity of the interface is hiddenhidden within the interface object.

• All the interface processing is hidden,

• And can be changed can be changed to handle changes such as the communications medium . . . .

• Without upsetting the rest of the system

Interface Objects,

Advantages:

• This is an example of

EncapsulationEncapsulation,, or Information Hiding, Information Hiding,

Interface Objects,

Advantages:• One reason this form of encapsulation is so important is that

The interfaces to other systems are The interfaces to other systems are the least stable aspect of any the least stable aspect of any

information system. . .information system. . .

Control Objects:

• Sometimes we find a method (subroutine, function) that uses data, and calls other methods, from many different many different objects and classes.

• These methods don’t easily fit don’t easily fit or don’t don’t logically belong logically belong in any of our entity classes entity classes or interface classes.interface classes.

Control Objects:

• So we create a class just to have somewhere to put this method;

• This we call a

Control Class.Control Class.

Control Objects:

• Sometimes this control class will have just this one method.one method.

• Nothing else, no attributesno attributes

• Other times it will need a few attributes for intermediate results, etc.

• And perhaps a few other methods.

Control Objects:

Definition:Definition:

Control Objects Control Objects are created during Analysis or Design to give us somewhere to put behavior that doesn’t easily fit doesn’t easily fit into existing interface or entity objects.

Real-World vs Data-World Objects

So far, we have looked at all these:

• What An Object Is– In the Real World – In the Data World

• Jacobson’s Three Types– Entity Objects– Interface Objects– Control Objects

Attributes

In the real world

• A child concludes there are objects out there• And notices some properties they have

– e.g., Color, softness, taste, warmth, etc.

• The child is gathering data about these objects• We will call each such data item an Attribute• These Attributes fit our earlier definition.• Thus, we build a mental model in our heads

Behavior In the real world:

• A child makes two observations about the objects surrounding her:

• They have attributesattributes, and

• They DODO things.

• And she learns to influence that behavior by sending them messagesmessages

Behavior In the real world:

• An object (a parent) responds to the child’s message by performing the behavior that goes with that message

• And in the process performs some serviceservice that the child needed.– This view of the behavior as a serviceservice is

important in ‘Responsibility-Driven Design’ which is an important part of DSDM

Behavior In the real world:

• Throughout life we use this method to manipulate objects around us

• We issue commandscommands to people, computers, cars, graphic objects, and all kinds of things,

• Sometimes we get the resultsresults we desire.• Everyone, including your users, manipulates their world

with this “send-a-message-get-a-service” “send-a-message-get-a-service” approach to life.

Behavior In the data world, there are three types of behavior that may concern us:

• OvertOvert behavior• PassivePassive behavior• ResponsiveResponsive behavior

Now let’s look at these in detail. . .Now let’s look at these in detail. . .

Behavior In the real worldBehavior In the real world

Overt Behavior:

• Where we actually instruct the object to do do something:something:

• “Graphic object, display thyself”• “Heating valve, open yourself”• “Window, minimize yourself”

etc. . .

Behavior In the real world

Passive Behavior:

• This is really just record-keeping:record-keeping:• The recording of the data generated by some action

or event in the real world.• Here we do not make something happen but merely record what happened:• “Purchase Order, change your status.”• “Customer, change your address”

etc. . .

Behavior In the real worldBehavior In the real world

Responsive Behavior:

• The providing of information (i.e., attribute values) in response to a message requesting them.

• “Here is a customer number, give me the name and address”

• “How many sales exist for this customer?”etc. . .