0% found this document useful (0 votes)
5 views44 pages

11-Class Diagram

The document provides an overview of class diagrams in UML, detailing their purpose, structure, and key components such as classes, attributes, operations, and visibility. It explains various relationships including association, aggregation, composition, and generalization, along with their implications in modeling systems. Additionally, it includes examples and Java implementations to illustrate these concepts effectively.

Uploaded by

Hà Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views44 pages

11-Class Diagram

The document provides an overview of class diagrams in UML, detailing their purpose, structure, and key components such as classes, attributes, operations, and visibility. It explains various relationships including association, aggregation, composition, and generalization, along with their implications in modeling systems. Additionally, it includes examples and Java implementations to illustrate these concepts effectively.

Uploaded by

Hà Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

OBJECT LANGUAGE AND THEORY

12. CLASS DIAGRAMS


@Nguyễn Thị Thu Trang, [email protected] 2

Objectives
• Describe the static view of the system and show how to
capture it in a model.
• Demonstrate how to read and interpret a class diagram.
• Model an association and aggregation and show how to
model it in a class diagram.
• Model generalization on a class diagram.
@Nguyễn Thị Thu Trang, [email protected] 3

Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@NGUYỄN Thị Thu Trang, [email protected] 4

1.1. Classes in the UML


• A class is represented using a rectangle with three
compartments:
Professor
- name
• The class name - employeeID : UniqueId
- hireDate
- status
• The structure (attributes) - discipline
- maxLoad

• The behavior (operations) + submitFinalGrade()


+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()
@NGUYỄN Thị Thu Trang, [email protected] 5

Classes and Objects


• A class is an abstract definition of an object
• It defines the structure and behavior of each object in the class.
• It serves as a template for creating objects.
• Classes are not collections of objects

Professor
Professor Torpie

Professor Meijer Professor Allen


@NGUYỄN Thị Thu Trang, [email protected] 6

What Is an Attribute?
• An attribute is a named property of a class that describes
the range of values that instances of the property may
hold.
• A class may have any number of attributes or no
attributes at all.
Student
- name
- address
Attributes
- studentID
- dateOfBirth
@NGUYỄN Thị Thu Trang, [email protected] 7

Attributes in Classes and Objects


Class
:Student
- name = “M. Modano”
- address = “123 Main St.”
Student - studentID = 9
- name - dateOfBirth = “03/10/1967”
- address
- studentID Objects
- dateOfBirth
sv1:Student
- name = “D. Hatcher”
- address = “456 Oak Ln.”
- studentID = 2
- dateOfBirth = “12/11/1969”
@NGUYỄN Thị Thu Trang, [email protected] 8

What Is an Operation?
• A service that can be requested from an object to effect
behavior. An operation has a signature, which may restrict
the actual parameters that are possible.
• A class may have any number of operations or none at all.

Student

+ get tuition()
+ add schedule()
Operations + get schedule()
+ delete schedule()
+ has prerequisites()
@NGUYỄN Thị Thu Trang, [email protected] 9

Member Visibility
• Visibility is used to enforce encapsulation
• May be public, protected, or private

Private
operations

Public Protected
operations operations
@NGUYỄN Thị Thu Trang, [email protected] 10

How Is Visibility Noted?


• The following symbols are used to specify export control:
• + Public access
• # Protected access
• - Private access

ClassName
- privateAttribute
+ publicAttribute
# protectedAttribute
- privateOperation ()
+ publicOperation ()
# protecteOperation ()
@Nguyễn Thị Thu Trang, [email protected] 11

Scope
• Determines number of instances of the
attribute/operation
• Instance: one instance for each class instance
• Classifier: one instance for all class instances
• Classifier scope is denoted by underlining the
attribute/operation name
Class1
- classifierScopeAttr
- instanceScopeAttr
+ classifierScopeOp ()
+ instanceScopeOp ()
@Nguyễn Thị Thu Trang, [email protected] 12

1.2. What Is a Class Diagram?


• Static view of a system
CloseRegistrationForm Schedule CloseRegistrationController
- semester
+ open() + is registration open?()
+ close registration() + commit() + close registration()
+ select alternate()
+ remove offering()
+ level() Professor
+ cancel() - name
Student + get cost() - employeeID : UniqueId
+ delete() - hireDate
+ submit() - status
+ get tuition()
+ save() - discipline
+ add schedule()
+ any conflicts?() - maxLoad
+ get schedule()
+ create with offerings()
+ delete schedule()
+ update with new selections() + submitFinalGrade()
+ has pre-requisites()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()
13

Static Structure vs. Dynamic Behavior


• Static aspects: Software component and how they are
related to one another
• Dynamic aspects: How the components interact with
one another and/or change state internally over time.
@Nguyễn Thị Thu Trang, [email protected] 14

Example: Class Diagram


• Is there a better way to organize class diagrams?

LoginForm RegistrationController
RegisterForCoursesForm

Schedule
CloseRegistrationForm CloseRegistrationController

Professor
Student
Course CourseOffering

CourseCatalogSystem
BillingSystem
@Nguyễn Thị Thu Trang, [email protected] 15

Review: What Is a Package?


• A general purpose mechanism for organizing elements
into groups.
• A model element that can contain other model elements.
• A package can be used:
• To organize the model under development
• As a unit of configuration management

University
Artifacts
@Nguyễn Thị Thu Trang, [email protected] 16

Example: Registration Package

Registration

CloseRegistrationForm CloseRegistrationController

RegisterForCoursesForm RegistrationController
@NGUYỄN Thị Thu Trang, [email protected] 17

A Package Can Contain Classes


• The package, University Artifacts, contains one package
and five classes.

Student
Artifacts Professor

University
Artifacts Course Schedule

Student CourseOffering
@Nguyễn Thị Thu Trang, [email protected] 18

Class Relationships
• Association

• Aggregation
• Composition

• Generalization
• Realization
@Nguyễn Thị Thu Trang, [email protected] 19

Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 20

What Is an Association?
• The semantic relationship between two or more classifiers
that specifies connections among their instances.
• A structural relationship specifying that objects of one thing
are connected to objects of another thing.

Student Schedule Course


@Nguyễn Thị Thu Trang, [email protected] 21

Example: What Associations?


1: submit schedule( ) 2: submit schedule( )
: RegisterForCoursesForm : RegistrationController

: Student
8: any conflicts?( )

3: save( )
4: submit( )

: Schedule 7: still open?( )


9: add student(Schedule)

6: has pre-requisites(CourseOffering)

: CourseOffering
: Student
5: is selected?( )
10: mark as enrolled in( )

: PrimaryScheduleOfferingInfo
@Nguyễn Thị Thu Trang, [email protected] 22

Role
• Role
• Useful technique for specifying the context of a class
and its objects
• Optional
• Role name
• String placed near the end of the association next to the
class to which it applies
• Indicates the role played by the class in terms of the
association.
• Part of the association and not part of the classes
@Nguyễn Thị Thu Trang, [email protected] 23

What Is Multiplicity?
• Multiplicity is the number of instances one class relates
to ONE instance of another class.
• For each association, there are two multiplicity
decisions to make, one for each end of the association.
• For each instance of Professor, many Course Offerings may be
taught.
• For each instance of Course Offering, there may be either one
or zero Professor as the instructor.

instructor
Professor CourseOffering
0..1 0..*
@Nguyễn Thị Thu Trang, [email protected] 24

Multiplicity Indicators
Unspecified
Exactly One 1

Zero or More 0..*

Zero or More *

One or More 1..*

Zero or One (optional value) 0..1

Specified Range 2..4

Multiple, Disjoint Ranges 2, 4..6


@Nguyễn Thị Thu Trang, [email protected] 25

Example: Multiplicity
1
RegisterForCoursesForm RegistrationController
1

0..1

0..1

1 0..*
Student Schedule CourseOffering
0..* 0..4
@Nguyễn Thị Thu Trang, [email protected] 26

Many-to-many association
@Nguyễn Thị Thu Trang, [email protected] 27

Java
implementation for
Association
//InsuranceCompany.java file
public class InsuranceCompany
{
// Many multiplicity can be implemented using Collection
private List<InsuranceContract> contracts;

/* Methods */
}
// InsuranceContract.java file Field Reference
public class InsuranceContract
{
private InsuranceCompany refers_to;

InsuranceContract(){ }

/* Methods */
public void setCompany(InsuranceCompany comp) { refers_to = comp; }
}
@Nguyễn Thị Thu Trang, [email protected] 28

Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 29

What Is an Aggregation?
• A special form of association that models a whole-part
relationship between the aggregate (the whole) and its
parts.
• An aggregation is an “is a part-of” relationship.
• Multiplicity is represented like other associations.

Whole 1 Part

0..1
@Nguyễn Thị Thu Trang, [email protected] 30

What is Composition?
• A special form of aggregation with strong
ownership and coincident lifetimes of the part with
the aggregate
• Also called composition aggregate
• The whole “owns” the part and is responsible for
the creation and destruction of the part.
• The part is removed when the whole is removed.
• The part may be removed (by the whole) before the
whole is removed.
Whole Part
@Nguyễn Thị Thu Trang, [email protected] 31

Examples: Association Types


• Association Car Driver
• use-a
• Objects of one class are associated with objects of
another class
• Aggregation Car Door
• is-a-part
• Strong association, an instance of one class is made up
of instances of another class
• Composition
• Strong aggregation, the composed object can’t be
shared by other objects and dies with its composer
• Share life-time Car Engine
@Nguyễn Thị Thu Trang, [email protected] 32

Aggregation Example

• A shared aggregation is one in which the parts may be


parts in any wholes
@Nguyễn Thị Thu Trang, [email protected] 33

Aggregation – Java implementation


class Car {
private List<Door> doors; //Field reference
Car(String name, List<Door> doors) {
this.doors = doors;
}

public List<Door> getDoors() {


return doors;
}
}
@Nguyễn Thị Thu Trang, [email protected] 34

Composition Example
• A compound aggregate is shown as attributes in a class
@Nguyễn Thị Thu Trang, [email protected] 35

Composition – Java implementation


final class Car {
// For a car to move, it need to have a engine.
private final Engine engine; // Composition
//private Engine engine; // Aggregation

Car(Engine engine) {
this.engine = engine;
}

// car start moving by starting engine


public void move() {
//if(engine != null)
{
engine.work();
System.out.println("Car is moving ");
}
class Engine {
} // starting an engine
} public void work() {
System.out.println("Engine of car has been started ");
}
}
@Nguyễn Thị Thu Trang, [email protected] 36

Content
1. Class diagrams
2. Association
3. Aggregation and Composition
4. Generalization
@Nguyễn Thị Thu Trang, [email protected] 37

Review: What Is Generalization?


• A relationship among classes where one class shares the
structure and/or behavior of one or more classes.
• Defines a hierarchy of abstractions where a subclass
inherits from one or more superclasses.
• Single inheritance
• Multiple inheritance
• Is an “is a kind of” relationship.
@Nguyễn Thị Thu Trang, [email protected] 38

Example: Single Inheritance


• One class inherits from another.
Ancestor
Account
- balance
Superclass - name
(parent) - number

+ withdraw()
+ createStatement()

Generalization
Relationship

Subclasses
Savings Checking
(children)

Descendents
@Nguyễn Thị Thu Trang, [email protected] 39

Example: Multiple Inheritance


• A class can inherit from several other classes.

FlyingThing Animal

Multiple Inheritance

Airplane Helicopter Bird Wolf Horse

Use multiple inheritance only when needed and


always with caution!
@Nguyễn Thị Thu Trang, [email protected] 40

Inheritance Tree Example


Abstract and Concrete Classes
• Abstract classes cannot have any objects
• Concrete classes can have objects

Animal Abstract class

+ communicate () Abstract operation


Communication There are no direct instances of Animal

Lion Tiger

+ communicate () + communicate ()

All objects are either lions or tigers


Generalization vs. Aggregation
Window Scrollbar

WindowWithScrollbar

Window A WindowWithScrollbar “is a” Window


A WindowWithScrollbar “contains a” Scrollbar

1
WindowWithScrollbar Scrollbar
1
Interfaces and Realizes Relationships
<<interface>>
Serializable Serializable

Schedule

Schedule

Icon presentation

Normal presentation
@Nguyễn Thị Thu Trang, [email protected] 44

Exercise
Document a class diagram using the following information:
• A class diagram containing the following classes: Personal
Planner Profile, Personal Planner Controller, Customer
Profile, and Buyer Record and other necessary ones.
• Associations drawn using the following information:
• Each Personal Planner Profile object can be associated with up to
one Personal Planner Controller object.
• Each Personal Planner Controller object must be related to one
Personal Planner Profile.
• A Personal Planner Controller object can be associated with up to
one Buyer Record and Customer Profile object.
• An instance of the Buyer Record class can be related to zero or one
Personal Planner Controller.
• Zero or one Personal Planner Controller objects are associated with
each Customer Profile instance
• A customer can become a VIP one.

You might also like