Classes and Class Diagrams - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Classes and Class Diagrams

Description:

A description of a set of objects that share the same attributes, ... It may also have navigability or roles. Example Class Diagram. borland.com. Associations ... – PowerPoint PPT presentation

Number of Views:192
Avg rating:3.0/5.0
Slides: 43
Provided by: LMay
Category:

less

Transcript and Presenter's Notes

Title: Classes and Class Diagrams


1
Classes and Class Diagrams
2
Learning Outcomes
  • Students will be able to
  • Describe a class
  • Identify components of a class diagram
  • Explain the terms
  • Multiplicity
  • Inheritance
  • Aggregation
  • Generalisation

3
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

4
A Class
Eg
ClassName
Window
Attribute1 Attribute2, etc
origin size
Operation1 Operation2, etc
open() close() move() display()
5
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 ....




6
Attributes ...
Can be further specified by stating their type
and even a default value
Eg
Wall
height Float width Float thickness
Float isLoadBearing Boolean false
7
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

8
Operations ...
WashingMachine
Can be further specified by stating parameter
parameter type and function return values
brandName String modelName String serialNumber
String Capacity Integer
acceptClothes (cString) acceptDetergent
(dInteger) turnOn( ) Boolean capacity( )
Boolean
9
Attributes and Operations with Visibility
Television
Accessibility levels Public Protected -
Private
brandName String modelName String
changeVolume( ) changeChannel ( ) -
paintImageOnScreen ( )
10
Class responsibilities
WashingMachine
In an area below The operations list you can
describe the classs responsibility (must be
done in an unambiguous way)
brandName String modelName String serialNumber
String Capacity Integer
acceptClothes (cString) acceptDetergent
(dInteger) turnOn( ) Boolean capacity( )
Boolean
Take dirty clothes as input and produce clean
clothes as output
11
Class constraints
WashingMachine
brandName String modelName String serialNumber
String Capacity Integer
acceptClothes (cString) acceptDetergent
(dInteger) turnOn( ) Boolean capacity( )
Boolean
(capacity 16 or 18 or 20lb)
12
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

13
Example Class Diagram
borland.com
14
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
Team
Plays on ?
Employee
Employer
15
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)

16
Terms of Multiplicity
  • Optional
  • lower bound of 0
  • Mandatory
  • lower bound of 1 or more
  • Single-valued
  • upper bound of 1
  • Multivalued
  • upper bound of gt 1 (usually )

17
Multiplicity Examples
EggBox
Egg
1
holds
12,24
Car
Owner
Is owned by
1..
1
18
Class Diagram revisited
borland.com
19
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

20
Aggregation Composition
21
Class Diagram revisited
borland.com
22
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

23
Generalisation Example
24
Another Generalisation Example
25
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

26
Multiple Classification Example
Agilemodeling.com
27
Summary - Class Model
  • Static structure of objects in a system
  • Identity
  • Relationships
  • Attributes
  • Operations

28
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

29
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

30
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

31
Finding Classes
  • Physical Entities
  • Houses, People, Machines, etc
  • Concepts
  • seating assignments, payment schedules, etc
  • Usually Nouns

Tentative Classes
Eliminate Spurious Classes
Extract Nouns
Requirements Source
Classes
32
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 ?

33
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)
34
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

35
Exercise part b) - Bad Classes
Vague
Implementation
Attribute
Redundant
36
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

37
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.

38
Exercise part e) - Initial Class Diagram
- construct a class diagram showing just class
names
39
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)

40
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

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

42
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
Write a Comment
User Comments (0)
About PowerShow.com