Class modeling
Luciano García Bañuelos Institute of Computer Science
Object versus Class
Class modeling -- Luciano García-Bañuelos 1
!"#$%&'()*+,
!"#$%&!"#$%&
'()*+,"*$%&-./.0/12
-+,.$%&'()*+,
!"#$%&!'()*%&
'()*+,"*$%&.-/.3/.4
'()*+,
!"#$%&5*)(!6
'()*+,"*$%&7"*$
!"#$%&'()*+,
!"#$%&!"#$%&
'()*+,"*$%&-./.0/12
-+,.$%&'()*+,
!"#$%&!'()*%&
'()*+,"*$%&.-/.3/.4
'()*+,
!"#$%&5*)(!6
'()*+,"*$%&7"*$
A class describes a group of objects with the same properties,
behavior, kinds of relationships, and
semantics
An object corresponds to the description of a single entity/instance in the application domain
Class diagram
Class modeling -- Luciano García-Bañuelos 2
!"#$$%#&'
!""#$%&!'()*+,!"!-./()+0+,(1!23"4!3)
!""#$%&!'(5*+,!"!-./(5+0+,(1!23"4!35666
7/(#&!'()*+8!#92'(:";$<")=*+#(<23"-./()
7/(#&!'(5*+8!#92'(:";$<"5=*+#(<23"-./(5666
(')$*+
:!'(
%$#">,!"(
?>!:9(@7%
?>!:9(A,,#(<<
,'*&'-)./012'/-
?737#*+$:"(9(#
/7<$"$7:*+B7$:"
'7C(8,(3"!*+4(?"7#=
<(3(?"8/*+B7$:"=*+D773(!:
#7"!"(8$:+!:93(*+137!"+0+E6E=
!"#$$%#&'
!""#$%&!'()*+,!"!-./()+0+,(1!23"4!3)
!""#$%&!'(5*+,!"!-./(5+0+,(1!23"4!35666
7/(#&!'()*+8!#92'(:";$<")=*+#(<23"-./()
7/(#&!'(5*+8!#92'(:";$<"5=*+#(<23"-./(5666
(')$*+
:!'(
%$#">,!"(
?>!:9(@7%
?>!:9(A,,#(<<
,'*&'-)./012'/-
?737#*+$:"(9(#
/7<$"$7:*+B7$:"
'7C(8,(3"!*+4(?"7#=
<(3(?"8/*+B7$:"=*+D773(!:
#7"!"(8$:+!:93(*+137!"+0+E6E=
!"#$$%#&'
!""#$%&!'()*+,!"!-./()+0+,(1!23"4!3)
!""#$%&!'(5*+,!"!-./(5+0+,(1!23"4!35666
7/(#&!'()*+8!#92'(:";$<")=*+#(<23"-./()
7/(#&!'(5*+8!#92'(:";$<"5=*+#(<23"-./(5666
(')$*+
:!'(
%$#">,!"(
?>!:9(@7%
?>!:9(A,,#(<<
,'*&'-)./012'/-
?737#*+$:"(9(#
/7<$"$7:*+B7$:"
'7C(8,(3"!*+4(?"7#=
<(3(?"8/*+B7$:"=*+D773(!:
#7"!"(8$:+!:93(*+137!"+0+E6E=
Some details can be omitted
Richer specifications are also possible
!"#$%&
!"#$%&'()*!+
'%()*&+
!"#$%&'()*!+, ,
!"#$%&'()
Class associations
Class modeling -- Luciano García-Bañuelos 3
In the application domain, objects are usually linked together
!"#$%&'()*"$
!"#$%&!"#$%&
'()*"$
!"#$%&'()*!+
+,)-%&'()*"$
!"#$%&!'()*&
./(%&'()*"$
!"#$%&!+,-&
0123(%&'()*"$
!"#$%&!./01-&
45%&6"78,$-
!"#$%&!23&
9:+%&6"78,$-
!"#$%&!45'&
6"78,$-
!"#$%&'()*!+; ;
,-!.'(/01
Object links
Class association
!"#$%& '%()*&+! "##$!"#$%&"#
Multiplicity and end names
Class modeling -- Luciano García-Bañuelos 4
Multiplicity specifies the number of objects that can be related with respect to
their underlying class
Examples • 1 (default) • * multiple • 1..* one or more • 3..5 three to five, inclusive
An association end name specify the role of a class in the association
employee employer
More about associations
Class modeling -- Luciano García-Bañuelos 5
!"#$ %$&'()*
! !
! !
! !
seller buyer
bidder
A pair of classes can have multiple associations
!"#$%& '%()*&+! "##$
%&'()*%%
+),-%,
.)//
"##$
!
!"#"$%&
Association classes
Class modeling -- Luciano García-Bañuelos 6
Association may also have properties
giving rise to association classes
!"#$%& '%()*&+! "##$
%&'()*%%
+),-%,
.)//
"##$
!
!"#"$%&
,%#-$.%#
/0(0,*
1).234(%
'%,5),&067%804369
!"#$ %&$'"()(*&+!
"#$%&'(%)*#(+
,-(.&$*/)(*&+
,('-'.%/%0
0*1(*2%00'#3
!
0*$#1(&$2 !4
Association classes may participate in other
associations
Generalization
Class modeling -- Luciano García-Bañuelos 8
Generalization is a relationship between classes providing an organized view of possible variations in both structure and behavior
!"#$%&'()(&#"!*+",-*.$.%&/0#12$3&4$056-%())785,9:-%;<))):*,-09!'.$:%(=(7"-#>0:,6".5$'.$:%?=?7"-#+294@"-$%?))72A6.>0"16."5#B"-2%C$+29!
'()(DE0"16."5#'*#1
!"#$%&F?)G&#"!*+",-*.$.%&H.94!&4$056-%<)))785,9:-%;G)))):*.+",$I.$"%?))7#G
-*J$E0"#$-$.%G7,#-*J$K$!5-6%L7#-*J$'.$::*.$%(<7"-#:6$22'.$::*.$%(=M7"-#
F?)GDN$"-F3,6"!5$.
!"#$
#"!%&"'(%)$)
*$+,-(
'./(
!"#$%&'()
/%'(+.!0)$//%)$
1+/'-"),$0)$//%)$
&2.*3"($
1+"4-)",#5"($)+"2
*$+%,-+.&/#&%
/%)&"'$6)$"
(%7$8+"#$($)
(%7$9$!,(-
(%7$0)$//%)$
/-$220)$//%)$
0'+)!12,+(.'-
Generalization and specialization
Class modeling -- Luciano García-Bañuelos 9
!"#$
#"!%&"'(%)$)
*$+,-(
'./(
!"#$%&'()
/%'(+.!0)$//%)$
1+/'-"),$0)$//%)$
&2.*3"($
1+"4-)",#5"($)+"2
*$+%,-+.&/#&%
/%)&"'$6)$"
(%7$8+"#$($)
(%7$9$!,(-
(%7$0)$//%)$
/-$220)$//%)$
0'+)!12,+(.'-
Generalization
Specialization
The superclass holds common properties
and associations
Each superclass adds specific properties and
associations
Inheritance and overriding
Class modeling -- Luciano García-Bañuelos 10
!"#$%&'()*!+,-.(/0)1,-2((%!$0
3,-/0)!4!+
5,-/0)!4!+
!"#$%
!"#$%&'()*!+,-.(/0)1,-2((%!$0
6(%(+,-7)+/04
&"'"(!"#$%Inheritance is the mechanism for sharing attributes, operations, and associations via the generalization/specialization relationship
A subclass overrides a superclass operation when it lists the same name. The overriding
operation is expected to refine and/or replace the
overridden operation.
Generalization set name
Class modeling -- Luciano García-Bañuelos 11
The generalization set name is an optional attribute that indicates the aspect being abstracted by a particular generalization
!"#$%&"'%()"*+
!"#$%&
'(&)"*&(+",(-. /0,)"*&(+",(-. /1%&&)"*&(+",(-.
The class Vehicle can be specialized according to the following aspects:
• Means of propulsion: wind, fuel, animal, gravity • Operating environment: land, air, water, outer space
Class properties: Advanced modeling
Class modeling -- Luciano García-Bañuelos 13
Multiplicity • [1] (default) • [*] multiple • [1..*] one or more • [3..5] three to five, inclusive
Visibility (depends on the programming language used for implementation)
• + public • # protected • - private • ~ package
Constraints for multi-valued attributes
• {ordered} • {unique}
!"#$%&'()*!+&,-.
"//)$''%&'()*!+&,-001.
234!$56#$)%&'()*!+&,1.&74)/$)$/86!*96$:
!"#$%&
Navigation
Class modeling -- Luciano García-Bañuelos 14
If an association is directed, messages can pass only on that direction
If the association does not have directions, then it is a bidirectional association
!"#$%&'#( )"*$
Dependencies
Class modeling -- Luciano García-Bañuelos 15
A dependency is the most general relation between classes It indicates that an object affects another object
!""#$%&'%()*+&,-
.,"',/& 012,1
),"$1'&*3#%&1#4
!"#$%&$'( !)*+,-,$'(
Qualifiers
Class modeling -- Luciano García-Bañuelos 16
A qualifier is an attribute or list of attributes whose values serve to partition the set of objects associated with an object across an association
!"##$%$
&'()$*'
!"#$%&"'(
)
)
!"##$%$
&'()$*'
*
A qualifier selects among the target objects, reducing the effective multiplicity from “many” to “one”
Qualifier
Constraints
Class modeling -- Luciano García-Bañuelos 17
Constrains are simple properties of associations, classes and many other things in UML
Specify limitations that implementers need to satisfy
!"#$%&
'()%&
!"#$%&'"()"%#*+,
*+#$,+$"
-./)"/")01
!%+$
-23456!"#$%&7'()%&568390
Examples of constraints
Class modeling -- Luciano García-Bañuelos 18
!"#$%&'
()*+
!"#$%&$&%'
,-'*#-"&%
()*)$+
./01#2%%
",#$'
-#((
.//0!
3#//4''%%
!"#$"%&'! !
!()*+%&'
"(1-(&2'
"()*)$+3453-#((/()*)$+'
Only one association can be instantiated
The ChairOf association is a subset of MemberOf
association
Tasks must be ordered
Salary constraint must be enforced by the
implementation
Constraints in UML
Class modeling -- Luciano García-Bañuelos 19
Constraints can be applied to almost every element in UML diagrams, using: natural language mathematical notation OCL (Object Constraint Language)
Constraints can be used for expressing: Invariants
interest > 3% Preconditions
before loan() takes place, salary > 5,000$ Postconditions
after loan() takes place, dayCollect = 1 or 10
N-ary associations
Class modeling -- Luciano García-Bañuelos 20
Occasionally, you will find some associations among three or more classes Try to decompose those n-ary associations into binary associations
!"#$%&&#"
'%(%&)%"
!""#$%#&'!
*%+,-%"%./#0"&%
1%2)3##4(
(
(
((
5,&)%./#0"&%
!"#$%&&#"
'%(%&)%" *%+),##-! "
!
"
"!
""
./&)%01#2"&%
#$$%&'%()#
3%4/5%"%01#2"&%
The meaning of the new model might be slightly different (for instance, you must be careful about multiplicity)
Aggregation and composition
Class modeling -- Luciano García-Bañuelos 21
Aggregation is a special form of association Underlines the fact that an object is made of constituent parts
Composition is a more restrictive form of aggregation Two additional constraints
A constituent part can belong to at most one assembly The part has a coincident lifetime as the assembly
!"#$%&#'(
)*"+' ,$-.$' /'01
!
"
!" !
!
!
"
23''*
!"#$%&' ()*)+)"& (,$%-.#,&.! !
Association versus Composition
Class modeling -- Luciano García-Bañuelos 22
Part can be shared by several wholes
Part is always a part of a single whole
Parts can live independently (i.e., whole cardinality can be 0..*)
Parts exist only as part of the whole (e.g. when a Window is destroyed all other widgets are also destroyed)
Whole is not solely responsible for the object
Whole is responsible and should create/destroy the objects
0..4 Category Document
*Window Frame
*
Abstract classes
Class modeling -- Luciano García-Bañuelos 23
An abstract class is a class that has no direct instances It may define common properties It may define some operation signatures called abstract operations
!"#$%&'()*
!"#$%&'#(")#$*+*,-
!"#$%&''
.&/01("2#!
3""45!6#("
!"##$%&'(&)#*+''
.&/01("2#!
7&1$5!6#("
,-./$%&'(&)#*+''
An abstract class is specified with a name in an italic font (or using
{abstract} near the class name)
Abstract operation (use an italic font)
Multiple inheritance
Class modeling -- Luciano García-Bañuelos 24
!"#$%&''
()*+,-"'!"#$%&'' ./$$,-"'!"#$%&'' 0)1)2'*314-5-4/)$6%1+*-7/+%*
!"##$%&'()*%+%*",#-.)/0%1"/.0
!"#$%&"!'()(*(+, "*'*-!./*$)(*(+,
A class can specialize more than
one superclasses
Be careful with the “diamond problem”:
some overlapping may cause conflicts
The “diamond problem” will not be present if the
classes are disjoint
{disjoint, incomplete}
Multiple inheritance
Class modeling -- Luciano García-Bañuelos 25
!"#$%&"
'()*!"#$%&" +(,"-!"#$%&"
!"# $%"&'()*+,+%-./0*+120
!"#$%&'(()*+,-."/(&$0$1
In this example: • overlapping There might be vehicles that belong to more than one subclass • complete Although more combinations might exist, only those presented are meaningful
UML generalization set constraints:
{complete, disjoint} {incomplete, disjoint} default {complete, overlapping} {incomplete, overlapping}
Delegation as a workaround for multiple inheritance
Class modeling -- Luciano García-Bañuelos 26
!"#$%&'()'*+,-(( ./++%&'()'*+,-(( 0"1"2(#314&5&4/"+6,1$#&7/$,#
!"#$%&"!'()(*(+, "*'*-!./*$)(*(+,
!"#$%&'' !"#$%&''()*)+'"'*,0 0
Metadata
Class modeling -- Luciano García-Bañuelos 27
Metadata is data that describes other data Database catalogs are an example of metadata This a central concept to disciplines such as the Model Driven
Architecture (MDA)
!"#$$%#&'
!"#$$
#(()*+,('%#&'
#(()*+,('-./'
%&&'()*&+01220
Enumerations
Class modeling -- Luciano García-Bañuelos 28
!"#$
!"#$
%&"$'( !)*+( ,'"#-( ./"012$(
%&'(
!"#$%&'"#$
()*+%&,)*+
!"#$
!-)./!
01"2!
3/)($!
.#)45*.!
6/*"4/()$#5*7
%&'(
)0/
+#*8
9"//*
:::
6/*"4/()$#5*7
)"*+
An enumeration is a data type that has a finite set of values. You should avoid modeling enumerations as generalization hierarchies.
Derived data
Class modeling -- Luciano García-Bañuelos 29
A derived element is a function of one or more elements, which in turn can be derived
!"#$%&'$(
)*'+(
!"#$%&
'+(*,*-.##(/$0'$(*1*!"#$%&'$(
!"!#$
%&!'()
*$$+#,-%#$.
!""#
$%!&'()&**&+
/01$.!.$##"23.
*$$+#,-%#$.04056666
Packages
Class modeling -- Luciano García-Bañuelos 30
A package is a group of elements (classes, associations, generalizations and nested packages) with a common theme A model can be partitioned in packages to make it easier to
understand
!"#$%&'($)$*
!"#$%
!+,$
!"#$%
-./#$ 01+'(
Top Related