classes and class diagrams. learning outcomes students will be able to : describe a class identify...
Post on 31-Dec-2015
216 Views
Preview:
TRANSCRIPT
Classes and Class Diagrams
Learning OutcomesStudents will be able to :• Describe a class• Identify components of a class diagram• Explain the terms:
– Multiplicity– Inheritance– Aggregation – Generalisation
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
A Class
ClassName
Operation1Operation2, etc
Attribute1Attribute2, etc
Window
open()close()move()display()
originsize
Eg
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 ....
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
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
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
Attributes and Operations with Visibility
Television
+ changeVolume( )+ changeChannel ( ) - paintImageOnScreen ( )
+ brandName: String+ modelName: String
Accessibility levelsAccessibility levels
+ Public+ Public# Protected# Protected- Private- Private
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
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)
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
Example Class Diagram
borland.com
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
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)
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 *)
Multiplicity Examples
EggBox Eggholds1 12,24
Car OwnerIs owned by1..* 1
Class Diagram revisited
borland.com
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
Aggregation & Composition
Class Diagram revisited
borland.com
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
Generalisation Example
+getCreditRating()
+name-address
Customer
+creditCardNumber
Personal Customer
+billForMonth()+remind()
+contactName-creditRating-creditLimit
Corporate Customer
Another Generalisation Example
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
Multiple Classification Example
Agilemodeling.com
Summary - Class Model
• Static structure of objects in a system– Identity– Relationships– Attributes– Operations
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
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
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
Finding Classes
• Physical Entities– Houses, People, Machines, etc
• Concepts– seating assignments, payment schedules,
etc
• Usually Nouns
Extract NounsEliminate Spurious
Classes
Requirements Source
ClassesTentative Classes
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 ?
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)
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
Exercise part b) - Bad Classes
VagueImplementation
Attribute
Redundant
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
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.
Exercise part e) - Initial Class Diagram
- construct a class diagram showing just class names
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)
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
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)
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
top related