Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and...

33
Sept 2005 92.3913 Ron McFad yen 1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development process. More detail added as time goes by. A static view of classes – shows structure: data, operations, and associations.

Transcript of Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and...

Page 1: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 1

Class Diagram

Begins as a conceptual or analysis class model and evolves to a design class model

Used throughout the development process. More detail added as time goes by.

A static view of classes – shows structure: data, operations, and associations.

Page 2: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 2

Classes

Represented by a rectangle with possibly 3 compartments

Customer

Customer

NameAddress

Customer

NameAddress

getName()checkCreditRating()

Customer

getName()checkCreditRating()

Page 3: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 3

Classes

«Singleton»dbFacade

Some classes are stereotyped:

Later in the course we study the Singleton design pattern. Stereotyping dbFacade as singleton conveys substantial information about the class and its behaviour.

Some methodologies have 3 stereotypes for “analysis” classes: boundary, control, entity

Page 4: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 4

Classes

Composite

Abstract Classes

An abstract class is one that is never instantiated. To indicate an abstract class, the class name is given in italics

See last 2 examples

Page 5: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 5

Attributes

Joe: Customer

Class instances

An individual object (instance of a class) is shown with naming information underlined

An unnamed customer: Customer

A customer named Joe

Page 6: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 6

Identifying classes … Main Success Scenario

1. Customer arrives at a POS checkout with goods and/or services to purchase

2. Cashier starts a new sale

3. Cashier enters item identifier

4. System records sale line item and presents item description, price, and running total

Cashier repeats steps 3-4 until indicates done

5. System presents total with taxes calculated

6. Cashier tells customer the total and asks for payment

7. Customer pays and System handles payment

8. System logs the completed sale and sends sale and payment information to the external Accounting (for accounting and commissions) and inventory systems (to update inventory)

9. System presents receipt

10. Customer leaves with receipt and goods (if any)...

Page 7: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 7

Attributes

an object contains data which are defined as part of the Class definition

examples:• Students have names, addresses, etc; • Courses have titles, descriptions, prerequisite information.

Rectangle

corner: Point

Student

nameaddress

Level of detail present will depend on whether you are in analysis or design, and your purposes at the time

Page 8: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 8

Attributes

To what degree is an attribute visible to other classes?Private –Public +Protected #Package ~

Student

-name-address

Page 9: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 9

Attributes

Default values =Derived values /Multiplicity [ ]Ordering {ordered}Uniqueness {unique}

Invoice

-date:Date = today-/total: Currency-payments[0..*]: Currency

Student

-name-address[1..3] {unique}

Page 10: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 10

Operations. What are the responsibilities of a class? What can it do?

Visibility

Parameters

Signature the name, parameters, and return type of the operation

Student

+getName()+getGPA(term :Term, gpaType: String)

Page 11: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 11

Associations

• correspond to verbs expressing a relationship between classes

• example a Library Member borrows a Copy of a Book

•Multiplicities• we indicate via multiplicities the range of allowable cardinalities for participation in an association• examples: 1, 1..*, 0..*, 1..3

Page 12: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 12

Associations

• Names and roles

• you can name the relationship and indicate how to read it• you can give role names for participating objects

Person CompanyWorks for1..* 1

employer employee

The role of a Person in this relationship The role of a Company in this relationship

The name of the relationship and thedirection for reading the name

Page 13: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 13

Associations

•example: a Library Member borrows a Copy of a Book

Member Book* *

borrowerborrows

Page 14: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 14

Associations

• example: An employee is supervised by an employee

*

0,1Employee

reports to

supervised

supervisor

A reflexive association

Page 15: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 15

Objects

Joe: Customer

Class instances

An individual object (instance of a class) is shown with naming information underlined

An unnamed customer: Customer

A customer named Joe

Page 16: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 16

Objects

Object state

Consider an instance of a class

Collectively, the specific attribute values and connections to other objects is known as the state of the object

Analysis uncovers states of interest. These can be modeled using statechart diagrams

Page 17: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 17

Generalization a generalization is a relationship between a general thing (the superclass or parent class) and a more specific kind of thing (the subclass or child class)example:a StaffMember is a specialized kind of LibraryMembera StudentMember is a specialized kind of LibraryMember

LibraryMember

StaffMember StudentMember

Page 18: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 18

Motivation for partitioning a class into subclasses:

•subclass has additional attributes of interest

•subclass has additional associations of interest

•subclass is operated on, handled, reacted to, or manipulated differently than the superclass or other subclasses

Page 19: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 19

Generalization

Multiple subclasses can be grouped to indicate they are relatedsubclasses

LibraryMember

StaffMember StudentMember

It then becomes meaningful to consider certain constraints:

complete, incomplete, disjoint, overlapping

Page 20: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 20

Generalization

Inheritance of attributes and behaviour:•everything a LibraryMember can do, a StaffMember can do•everything a LibraryMember can do, a StudentMember can do

•If a LibraryMember can borrow a book, so can a StaffMember and a StudentMember

•a StaffMember and a StaffMember have all the attributes the LibraryMember has, and possibly moreSpecialization: there are some things that a specialized class can do that a LibraryMember cannot

LibraryMember

StaffMember StudentMember

Page 21: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 21

PaymentAmount: money

Cash Payment Credit Payment Cheque Payment

SalePays-for1 1

Every payment, regardless of whether it is cash, credit, or cheque, has an Amount and it is associated with a Sale

CreditCard Cheque

1

1

1

*

Page 22: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 22

The name Payment is italicized - meaning it is an abstract class

An abstract class is a class that will never be instantiated; only its subclasses can exist

If “Payment” was not in italics then a Payment could exist that is not a Cash, Credit, or Check payment

PaymentAmount: money

Cash Payment Credit Payment Cheque Payment

Page 23: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 23

What is the difference:

Payment

Unauthorized Payment

Authorized Payment

PaymentState

Unauthorized State Authorized State

Payment Is-in

* 1

Page 24: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 24

Aggregation and Composition

both are associations used to denote that an object from one class is part of an object of another class

HonoursProgramme Course

An example of Aggregation: a course is part of an honours programme. The same module could be part of several honours courses

Suppose the course “UML and Patterns” is part of both the “Software Engineering” and the “Computer Science” honours programmes

**

Page 25: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 25

Aggregation and Composition

Board Square

Composition is similar to, but stronger than aggregation. If you specify composition, then you are saying that one object owns its parts.

A Board is made up of several Squares. A Square will belong to just one Board. If a Board is deleted, then its Squares are deleted too.

What is the multiplicity at the composition end of the association?

*

Page 26: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 26

Aggregation and Composition

Invoice InvoiceLine

Consider Invoices and their Invoice Lines

Question: Is the association aggregation or composition?

?

?

*

Page 27: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 27

Aggregation and Composition

Sale SalesLineItem

Consider Sales and their SalesLineItems

Is the association an aggregation or a composition?

?

?

*

Page 28: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 28

Aggregation and Composition

Consider the General Calendar and its Course Specifications.

Is the association an aggregation or a composition?

Page 29: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 29

A composite is a group of objects in which some objects contain others; one object may represent groups, and another may represent an individual item, a leaf.

Composite Pattern

We will examine the composite pattern later in the course. At this time, we are many concerned with its structural aspect.

Consider the class diagram that follows. What objects does it allow us to instantiate and how will they relate to one another?

Page 30: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 30

MachComponent

getMachineCount()

Machine

getMachineCount()

MachComposite

components: List

getMachineCount()

*PlantFloor

1

*1

Composite Pattern

Consider a UML class diagram for machines. Machines may be complex and contain other machines.

Page 31: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 31

An object diagram illustrating the machine on floor 5 of the plant

L1: Machine

M1: MachComposite

L2: Machine

L7: Machine L4: Machine

M9: MachComposite

floor5 :Plantfloor

Composite Pattern

Page 32: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 32

The decorator pattern allows us to enclose an object inside another object. The enclosing object is called a decorator. The other object is the component, it is the decorated object.

The decorator conforms to the interface of the enclosed component and so its presence is transparent to the components clients. The decorator forwards requests to the component, but may perform some processing before/after doing so.

We will examine the decorator pattern later in the course. At this time, we are many concerned with its structural aspect.Consider the class diagram that follows. What objects does it allow us to instantiate and how will they relate to one another?

Decorator Pattern

Page 33: Sept 200592.3913 Ron McFadyen1 Class Diagram Begins as a conceptual or analysis class model and evolves to a design class model Used throughout the development.

Sept 2005 92.3913 Ron McFadyen 33

UML class diagram

DecoratedReceipt

print()

receipt

print()

Decorator

print()other()

1sale

timeOfDay productCoupon moneySaved

Decorator Pattern

1 1

How does the Decorator pattern differ from the Composite pattern?

What would a typical object diagram look like?