29
Class modeling Luciano García Bañuelos Institute of Computer Science

Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

Class modeling

Luciano García Bañuelos Institute of Computer Science

Page 2: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 3: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 4: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

!"#$%&

!"#$%&'()*!+

'%()*&+

!"#$%&'()*!+, ,

!"#$%&'()

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

Page 5: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

!"#$%& '%()*&+! "##$!"#$%&"#

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

Page 6: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

More about associations

Class modeling -- Luciano García-Bañuelos 5

!"#$ %$&'()*

! !

! !

! !

seller buyer

bidder

A pair of classes can have multiple associations

!"#$%& '%()*&+! "##$

%&'()*%%

+),-%,

.)//

"##$

!

!"#"$%&

Page 7: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 8: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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,+(.'-

Page 9: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 10: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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.

Page 11: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 12: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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$:

!"#$%&

Page 13: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

!"#$%&'#( )"*$

Page 14: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

!"#$%&$'( !)*+,-,$'(

Page 15: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 16: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 17: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 18: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 19: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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)

Page 20: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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''*

!"#$%&' ()*)+)"& (,$%-.#,&.! !

Page 21: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

*

Page 22: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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)

Page 23: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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}

Page 24: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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}

Page 25: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 26: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 27: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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.

Page 28: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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

Page 29: Luciano García Bañuelos - ut · 22 Class modeling -- Luciano García-Bañuelos Part can be shared by several wholes Part is always a part of a single whole Parts can live independently

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+'(