You are on page 1of 21

UML for .

NET Developers
Class Diagrams

Jason Gorman 2005. All rights reserved.

Accelerated learning for C# developers


Get the UML for .NET Premium Package at
http://www.parlezuml.com/tutorials/umlfordotnet.htm

Jason Gorman 2005. All rights reserved.

Classes
class Account
class Account
{
}

{
}

Account

Class Account
Class Account
End Class
End Class

Jason Gorman 2005. All rights reserved.

Attributes

Account
- balance : Single = 0
- limit : Single

class Account
class Account
{
{
private float balance = 0;
private float balance = 0;
private float limit;
private float limit;
}
}

Class Account
Class Account
[visibility] [/] attribute_name[multiplicity] [: type [= default_value]]

Private balance As Single = 0


Private balance As Single = 0
Private limit As Single
Private limit As Single
End Class
End Class

Jason Gorman 2005. All rights reserved.

Account
- balance : Single = 0
- limit : Single
+ deposit(amount : Single)
+ withdraw(amount : Single)

Operations
[visibility] op_name([[in|out] parameter : type[, more params]])[: return_type]

class Account
class Account
{
{
private float balance = 0;
private float balance = 0;
private float limit;
private float limit;
public void deposit(float amount)
public void deposit(float amount)
{
{
balance = balance + amount;
balance = balance + amount;
}
}

public void withdraw(float amount)


public void withdraw(float amount)
{
{
balance = balance - amount;
balance = balance - amount;
}
}

Class Account
Class Account
Private balance As Single = 0
Private balance As Single = 0
Private limit As Single = 0
Private limit As Single = 0
Public Sub deposit(ByVal amount As Single)
Public Sub deposit(ByVal amount As Single)
balance = balance + amount
balance = balance + amount
End Sub
End Sub
Public Sub withdraw(ByVal amount As Single)
Public Sub withdraw(ByVal amount As Single)
balance = balance - amount
balance = balance - amount
End Sub
End Sub
End Class
End Class

Jason Gorman 2005. All rights reserved.

class Account
class Account
{
{
private float balance = 0;
private float balance = 0;
public float limit;
public float limit;
protected int id;
protected int id;
internal int databaseId;
internal int databaseId;

Account
- balance : float = 0
+ limit : float
# id : int
~ databaseId : int

public void deposit(float amount)


public void deposit(float amount)
{
{
balance = balance + amount;
balance = balance + amount;
}
}

+ deposit(amount : single)
-withdraw(amount : single)
# getAvailableFunds() : single
~ getDatabaseId() : int

private void withdraw(float amount)


private void withdraw(float amount)
{
{
balance = balance - amount;
balance = balance - amount;
}
}

+ = public
- = private
# = protected
~ = package

protected int getId()


protected int getId()
{
{
return id;
return id;
}
}
internal int getDatabaseId()
internal int getDatabaseId()
{
{
return databaseId;
return databaseId;
}
}

Visibility C#
}

Jason Gorman 2005. All rights reserved.

Class Account
Class Account

Account
- balance : Single = 0
+ limit : Single
# id : Integer
~ databaseId : Integer

Private balance As Single = 0


Private balance As Single = 0
Public limit As Single = 0
Public limit As Single = 0
Protected id As Integer
Protected id As Integer
Friend databaseId As Integer
Friend databaseId As Integer

+ deposit(amount : Single)
-withdraw(amount : Single)
# getAvailableFunds() : Single
~ getDatabaseId() : Integer

Public Sub deposit(ByVal amount As Single)


Public Sub deposit(ByVal amount As Single)
balance = balance + amount
balance = balance + amount
End Sub
End Sub
Private Sub withdraw(ByVal amount As Single)
Private Sub withdraw(ByVal amount As Single)
balance = balance - amount
balance = balance - amount
End Sub
End Sub

+ = public
- = private
# = protected
~ = package

Protected Function getId() As Integer


Protected Function getId() As Integer
Return id
Return id
End Function
End Function

Visibility VB.Net

Friend Function getDatabaseId() As Integer


Friend Function getDatabaseId() As Integer
Return databaseId
Return databaseId
End Function
End Function
End Class
End Class

Jason Gorman 2005. All rights reserved.

short noOfPeople = Person.getNumberOfPeople();


short noOfPeople = Person.getNumberOfPeople();
Person p = Person.createPerson("Jason Gorman");
Person p = Person.createPerson("Jason Gorman");
System.Diagnostics.Debug.Assert(Person.getNumberOfPeople()
System.Diagnostics.Debug.Assert(Person.getNumberOfPeople()
== noOfPeople + 1);
== noOfPeople + 1);

class Person
class Person
{
{
private static int numberOfPeople = 0;
private static int numberOfPeople = 0;
private string name;
private string name;
private Person(string name)
private Person(string name)
{
{
this.name = name;
this.name = name;
numberOfPeople++;
numberOfPeople++;
}
}

Person
- numberOfPeople : int
- name : string

public static Person createPerson(string name)


public static Person createPerson(string name)
{
{
return new Person(name);
return new Person(name);
}
}

+ createPerson(name : string) : Person


+ getName() : string
+ getNumberOfPeople() : int
- Person(name : string)

public string getName()


public string getName()
{
{
return this.name;
return this.name;
}
}

Class & Instance


Scope C#

public static int getNumberOfPeople()


public static int getNumberOfPeople()
{
{
return numberOfPeople;
return numberOfPeople;
}
}
}

Jason Gorman 2005. All rights reserved.

Public Class Person


Public Class Person
Dim noOfPeople As Integer = Person.getNumberOfPeople()
Dim noOfPeople As Integer = Person.getNumberOfPeople()
Dim p As Person = Person.createPerson("Jason Gorman")
Dim p As Person = Person.createPerson("Jason Gorman")
System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() _
System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() _
= noOfPeople + 1)
= noOfPeople + 1)

Private Shared numberOfPeople As Integer = 0


Private Shared numberOfPeople As Integer = 0
Private name As String
Private name As String
Public Shared Function createPerson(ByVal name As String) As Person
Public Shared Function createPerson(ByVal name As String) As Person
Return New Person(name)
Return New Person(name)
End Function
End Function

Person
- numberOfPeople : Integer
- name : String
+ createPerson(name : String) : Person
+ getName() : String
+ getNumberOfPeople() : Integer
- New(name : String)

Class & Instance


Scope VB.Net

Private Sub New(By Val name As String)


Private Sub New(By Val name As String)
Me.name = name
Me.name = name
numberOfPeople = numberOfPeople + 1
numberOfPeople = numberOfPeople + 1
End Sub
End Sub
Public Function getName() As String
Public Function getName() As String
Return name
Return name
End Function
End Function
Public Shared Function getNumberOfPeople() As Integer
Public Shared Function getNumberOfPeople() As Integer
Return numberOfPeople
Return numberOfPeople
End Function
End Function
End Class
End Class

Jason Gorman 2005. All rights reserved.

Associations C#
multiplicity

1
b

A
B

b:B

Equivalent to

role name

class A
class A
{
{
}

public B b = new B();


public B b = new B();
}

A a = new A();
A a = new A();
B b = a.b;
B b = a.b;

class B
class B
{
{
}
}

Jason Gorman 2005. All rights reserved.

10

The most advanced UML tutorial for


C# developers
Get the UML for .NET Premium Package at
http://www.parlezuml.com/tutorials/umlfordotnet.htm

Jason Gorman 2005. All rights reserved.

11

Associations VB.Net
multiplicity

1
b

A
B

b:B

Equivalent to

role name

Class A
Class A
Public b As New B()
Public b As New B()
End Class
End Class

Dim a As New A()


Dim a As New A()
Dim b As B = a.b
Dim b As B = a.b

Class B
Class B
End Class
End Class

Jason Gorman 2005. All rights reserved.

12

Bi-directional Associations C#
multiplicity

A
B

role name

b:B

Equivalent to

B
a:A

class A
class A
{
{
public B b;
public B b;
public A()
public A()
{
{
b = new B(this);
b = new B(this);
}
}

}
A a = new A();
A a = new A();
B b = a.b;
B b = a.b;
A a1 = b.a;
A a1 = b.a;
System.Diagnostics.Debug.Assert(a == a1);
System.Diagnostics.Debug.Assert(a == a1);

class B
class B
{
{
public A a;
public A a;
public B(A a)
public B(A a)
{
{
this.a = a;
this.a = a;
}
}

}
Jason Gorman 2005. All rights reserved.

13

Bi-directional Associations
VB.Net
multiplicity

A
B

b:B

Equivalent to

role name
Class A
Class A
Public b As B()
Public b As B()
Sub Ne w()
Sub Ne w()
b = New B(Me)
b = New B(Me)
End Sub
End Sub
End Class
End Class
Class B
Class B
Public a As A
Public a As A
Public Sub New(ByRef a As A)
Public Sub New(ByRef a As A)
Me.a = a
Me.a = a
End Sub
End Sub
End Class
End Class

B
a:A

Dim a As New A()


Dim a As New A()
Dim b As B = a.b
Dim b As B = a.b
Dim a1 As A = b.a
Dim a1 As A = b.a
System.Diagnostics.Debug.Assert(a Is a1)
System.Diagnostics.Debug.Assert(a Is a1)

Jason Gorman 2005. All rights reserved.

14

Association names & role


defaults
Lives at
Person

Address

Default role name = address


Default multiplicity = 1
class Person
class Person
{
{

Public Class Person


Public Class Person
// association: Lives at
// association: Lives at
public Address address;
public Address address;

' association: Lives at


' association: Lives at
Public address As Address
Public address As Address

public Person(Address address)


public Person(Address address)
{
{
this.address = livesAt;
this.address = livesAt;
}
}

Public Sub New(By Val address As a ddress)


Public Sub New(By Val address As a ddress)
Me.address = address
Me.address = address
End Sub
End Sub
End Class
End Class

Jason Gorman 2005. All rights reserved.

15

Multiplicity & Collections


Customer

1..2

1..*
accounts

Customer
Account

accounts[1..*] : Account

Equivalent to

class Customer
class Customer
{
{

Class Customer
Class Customer
// accounts[1..*] : Account
// accounts[1..*] : Account
public System.Collections.ArrayList accounts = new ArrayList();
public System.Collections.ArrayList accounts = new ArrayList();

accounts[1..*] : Account
accounts[1..*] : Account
Public accounts As New ArrayList()
Public accounts As New ArrayList()

public Customer()
public Customer()
{
{
Account defaultAccount = new Account();
Account defaultAccount = new Account();
accounts.Add(defaultAccount);
accounts.Add(defaultAccount);
}
}

Public Sub New()


Public Sub New()
Dim defaultAccount As New Account()
Dim defaultAccount As New Account()
accounts.Add(defaultAccount)
accounts.Add(defaultAccount)
End Sub
End Sub
End Class
End Class

Jason Gorman 2005. All rights reserved.

16

Aggregation & Composition


0..1 1..*
Computer

HardwareDevice

Aggregation is made up of objects that can be shared or exchanged

1
ShoppingBasket

1..*
OrderItem

Co mposition is composed of objects that cannot be shared or exchanged


and live only as long as the composite object

Jason Gorman 2005. All rights reserved.

17

Generalization
Person

Employee

Class Person
Class Person

class Person
class Person
{
{
}
}

End Class
End Class
Class Employee
Class Employee
Inherits Person
Inherits Person

class Emp loyee : Person


class Emp loyee : Person
{
{
}
}

End Class
End Class

Jason Gorman 2005. All rights reserved.

18

Realization
IPerson
<<interface>>
IPerson

OR
Employee

Employee

Interface IPerson
Interface IPerson

interface IPerson
interface IPerson
{
{
}
}

End Interface
End Interface
Class Employee
Class Employee
Implements IPerson
Implements IPerson

class Emp loyee : IPerson


class Emp loyee : IPerson
{
{
}
}

End Class
End Class

Jason Gorman 2005. All rights reserved.

19

In the extended class diagrams tutorial

Overriding
Abstract classes
Dependencies
Qualified associations
Association classes
More on associations, visibility & scope
Information hiding

All with C# code examples


Jason Gorman 2005. All rights reserved.

20

Use Case Diagrams

Object Diagrams & Filmstrips

Model the users of the


system and the goals they
can achieve by using it

Model snapshots of the


running system and show how
actions change object state

Class Diagrams

Implementation Diagrams

Model types of objects


and the relationships
betw een them.

Model the physical


components of a system and
their deploy ment architecture

Sequence Diagrams

Packages &
Model Management

Model how objects


interact to achieve
functional goals

Activity Diagrams
Model the flow of use
cases and single and
multi-threaded code

Organise your logical and


physical models w ith packages

Object Constraint Language


Model business rules and
create unambiguous
specific ations

UML for
.NET

Statechart Diagrams

User Experience Modeling

Premium Package

Model the behaviour of


objects and event-driven
applications

Design user-centred systems


with UML

Design Principles

Design Patterns

Create w ell-designed
software thats easier to
change and reuse

Apply proven solutions to


common OO design problems
Jason Gorman 2005. All rights reserved.

Available
exclusively from
www.parlezuml.com
21

You might also like