classes and class diagrams. learning outcomes students will be able to : describe a class identify...

42
Classes and Class Diagrams

Upload: melvin-henderson

Post on 31-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Classes and Class Diagrams

Page 2: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Learning OutcomesStudents will be able to :• Describe a class• Identify components of a class diagram• Explain the terms:

– Multiplicity– Inheritance– Aggregation – Generalisation

Page 3: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

What is a Class ?• A description of a set of objects that share

the same attributes, relationships and semantics.

• An abstraction• 3 Parts : A Name, Attributes and Operations• An object would be an instance of the class• EG

– Class – a libraryMember borrows a bookCopy– Object – Joe Bloggs borrows copy 1 of UML

Distilled

Page 4: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

A Class

ClassName

Operation1Operation2, etc

Attribute1Attribute2, etc

Window

open()close()move()display()

originsize

Eg

Page 5: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Attributes

• Named property of a class – usually a noun• Describes a range of values• A class can have many attributes or none• The attribute is shared by all objects of the class• EG

– All students have name, address, date of birth– All cats have ....

Page 6: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Attributes ...

Wall

height: Floatwidth: Floatthickness: FloatisLoadBearing: Boolean = false

Eg

Can be further Can be further specified by specified by

stating their stating their typetype and even a and even a default valuedefault value

Page 7: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Operations

• The implementation of a service• Something that can be done to an

object, that is shared by all objects of that class

• Each class can have many operations or none

• Usually a verb

Page 8: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Operations ...

WashingMachine

acceptClothes (c:String)acceptDetergent (d:Integer)turnOn( ) : Booleancapacity( ) : Boolean

brandName: StringmodelName: StringserialNumber: StringCapacity: Integer

Can be further Can be further specified by specified by

stating stating parameterparameter

& parameter & parameter typetype

and function and function return valuesreturn values

Page 9: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Attributes and Operations with Visibility

Television

+ changeVolume( )+ changeChannel ( ) - paintImageOnScreen ( )

+ brandName: String+ modelName: String

Accessibility levelsAccessibility levels

+ Public+ Public# Protected# Protected- Private- Private

Page 10: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Class responsibilities

WashingMachine

acceptClothes (c:String)acceptDetergent (d:Integer)turnOn( ) : Booleancapacity( ) : Boolean

brandName: StringmodelName: StringserialNumber: StringCapacity: Integer

In an area below In an area below The operations The operations

list you can list you can describe the describe the

class’s class’s responsibility responsibility

(must be done in(must be done inan unambiguous an unambiguous

way)way)Take dirty clothes as input and produce clean clothes as output

Page 11: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Class constraints

WashingMachine

acceptClothes (c:String)acceptDetergent (d:Integer)turnOn( ) : Booleancapacity( ) : Boolean

brandName: StringmodelName: StringserialNumber: StringCapacity: Integer

(capacity = 16 or 18 or 20lb)(capacity = 16 or 18 or 20lb)

Page 12: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

What is a Class diagram?

• A description of types of objects in the system

• It has classes, relationships and multiplicities

• Constraints may be applied to the way objects are connected

• It may also have navigability or roles

Page 13: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Example Class Diagram

borland.com

Page 14: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Associations• A relationship between classes• Used if there is a real-world association• A short sentence – associations correspond to verbs• E.g. – A player plays on a team• Links can be uni-directional or bi-directional• In class associations each class usually plays a role

e.g. in professional team, team is employer and player is employee

Player TeamPlays on ►

Employee Employer

Page 15: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Multiplicity• Records constraints on associations• Caters for permitted or required number

of links (a link is an instance of an association)

• Specify :– An exact number (e.g. use 1)– A range of numbers (e.g. use 2..8 or 1..*)– Arbitrary, unspecified number (use *)– Discrete numbers (e.g. use 5,10)

Page 16: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Terms of Multiplicity

• OptionalOptional • – – lower bound of 0lower bound of 0• MandatoryMandatory • – – lower bound of 1 or morelower bound of 1 or more• Single-valuedSingle-valued • – – upper bound of 1upper bound of 1• Multivalued Multivalued • – – upper bound of > 1 (usually *)upper bound of > 1 (usually *)

Page 17: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Multiplicity Examples

EggBox Eggholds1 12,24

Car OwnerIs owned by1..* 1

Page 18: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Class Diagram revisited

borland.com

Page 19: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Aggregation & Composition

• Ways of showing more about an association

• An object of one class is part of an object of another class

• Denoted by a diamond at the whole end of the association

• Composition is a special kind of aggregation – the whole owns its parts

Page 20: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Aggregation & Composition

Page 21: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Class Diagram revisited

borland.com

Page 22: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Generalisation• Uses inheritance • EG – a business could have two types

of customer – Corporate customer– Personal customer

• Both have similarities that can be placed in a general Customer class (supertype)

• Personal and Corporate customers are subtypes

Page 23: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Generalisation Example

+getCreditRating()

+name-address

Customer

+creditCardNumber

Personal Customer

+billForMonth()+remind()

+contactName-creditRating-creditLimit

Corporate Customer

Page 24: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Another Generalisation Example

Page 25: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Classification

• Relationship between an object and its type

• Single Classification :– Object belongs to single type– May inherit from supertypes

• Multiple Classification :– Object may be described by several types– Not necessarily connected by inheritance

Page 26: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Multiple Classification Example

Agilemodeling.com

Page 27: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Summary - Class Model

• Static structure of objects in a system– Identity– Relationships– Attributes– Operations

Page 28: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Summary

• Class : Group of similar objects• Association : group of similar

connections between objects• Generalisation : structures description

of objects by organising classes by similarities/differences

• Attributes and Operations : elaborate structure

Page 29: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Domain Class Model

• Describes real-world classes and their relationships to each other.

• Information gained from :-– Problem statement– Artefacts– Expert knowledge of application domain– General knowledge of real world

• Does not rely on a single source

Page 30: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Constructing a Domain Class Model

• Find classes• Prepare data dictionary• Find associations• Find attributes of objects and links• Organise and simplify using inheritance• Iterate and refine

Page 31: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Finding Classes

• Physical Entities– Houses, People, Machines, etc

• Concepts– seating assignments, payment schedules,

etc

• Usually Nouns

Extract NounsEliminate Spurious

Classes

Requirements Source

ClassesTentative Classes

Page 32: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Eliminate Spurious Classes

• Is it beyond the scope of the system ?• Does it refer to the system as a whole ?• Does it duplicate another class ?• Is it too vague ?• Is it too specific ?• Is it too tied up with physical I/Os ?• Is it really an attribute ?• Is it really an operation ?• Is it really an association ?

Page 33: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise - scenario• Littlesand, Pebblesea and Mudport are

three charming resorts on the south coast which are very popular with tourists, since they score well on beach ratings and hours of sunshine for the sea.

• All three resorts have a large number of places to stay, ranging from one-room guest houses to the exclusive Palace Hotel at Pebblesea. The local tourist board wants to set up a central system to deal with room bookings in the area.Britton & Doake (2000)

Page 34: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part a) & b) - Tentative Classes

• Write out the scenario on previous slide and underline the tentative classes (possible classes)

• Some of the words/phrases that you underline will class attributes

• Then following the example on the next slide, categorise the classes into

– Vague – too vague to use– Redundant – no use for it– Implementation – concerned with implementation issues– Attribute – the item has turned out to be an attribute of a

class

Page 35: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part b) - Bad Classes

VagueImplementation

Attribute

Redundant

Page 36: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part c) - Good Classes

• Write out a list of the classes that you have left

• These will be used to create you class diagrams

Page 37: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part d) - Prepare Data Dictionary

• Isolated words can be misinterpreted

• Write a paragraph precisely describing each class

• Eg: Room – a room at a place to stay against which bookings can be made. Rooms may be of various types. A place to stay can contain more than one room.

Page 38: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part e) - Initial Class Diagram

- construct a class diagram showing just class names

Page 39: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part f) - Find Associations and multiplicity

• Show relationships between classes• Often correspond verbs :

– Physical Locations (NextTo, PartOf, ContainedIn, etc

– Directed actions (Drives)– Communication (TalksTo)– Ownership (Has, PartOf)– Satisfaction of Condition (WorksFor,

MarriedTo, Manages, etc)

Page 40: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part g) - Find Attributes for the main (resort)

class

• Data Properties of individual objects

• Use – Knowledge of application domain– Artefacts

• Capture the most important – you can add detail later

Page 41: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Exercise part h) - Refine Using Inheritance

• Bottom-up generalisation– Classes with similar attributes,

associations, operations– Define superclass to share common

features

• Top-down specialisation– Similar class names (eg fixed menu,

pop-up menu, sliding menu)

Page 42: Classes and Class Diagrams. Learning Outcomes Students will be able to : Describe a class Identify components of a class diagram Explain the terms: –Multiplicity

Further Reading

• Object-Oriented Modelling and Design with UML, Michael Blaha and James Rumbaugh,

• UML Distilled, Martin Fowler,Addison Wesley ISBN 0-321-19368-7

• Using UML, Perdita Stevens

• www.agilemodeling.com• Object-Oriented System Development (2000)

Britton & Doake