Copyright Jason Gorman UML for.NET Developers Class Diagrams
Copyright Jason Gorman Classes Account class Account { } class Account { } Class Account End Class Class Account End Class
Copyright Jason Gorman Attributes Account class Account { private float balance = 0; private float limit; } class Account { private float balance = 0; private float limit; } Class Account Private balance As Single = 0 Private limit As Single End Class Class Account Private balance As Single = 0 Private limit As Single End Class - balance : Single = 0 - limit : Single [visibility] [/] attribute_name[multiplicity] [: type [= default_value]]
Copyright Jason Gorman Operations Account class Account { private float balance = 0; private float limit; public void deposit(float amount) { balance = balance + amount; } public void withdraw(float amount) { balance = balance - amount; } class Account { private float balance = 0; private float limit; public void deposit(float amount) { balance = balance + amount; } public void withdraw(float amount) { balance = balance - amount; } Class Account Private balance As Single = 0 Private limit As Single = 0 Public Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub End Class Class Account Private balance As Single = 0 Private limit As Single = 0 Public Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub End Class - balance : Single = 0 - limit : Single + deposit(amount : Single) + withdraw(amount : Single) [visibility] op_name([[in|out] parameter : type[, more params]])[: return_type]
Copyright Jason Gorman Visibility – C# Account - balance : float = 0 + limit : float # id : int ~ databaseId : int + deposit(amount : single) -withdraw(amount : single) # getAvailableFunds() : single ~ getDatabaseId() : int + = public - = private # = protected ~ = package class Account { private float balance = 0; public float limit; protected int id; internal int databaseId; public void deposit(float amount) { balance = balance + amount; } private void withdraw(float amount) { balance = balance - amount; } protected int getId() { return id; } internal int getDatabaseId() { return databaseId; } class Account { private float balance = 0; public float limit; protected int id; internal int databaseId; public void deposit(float amount) { balance = balance + amount; } private void withdraw(float amount) { balance = balance - amount; } protected int getId() { return id; } internal int getDatabaseId() { return databaseId; }
Copyright Jason Gorman Account - balance : Single = 0 + limit : Single # id : Integer ~ databaseId : Integer + deposit(amount : Single) -withdraw(amount : Single) # getAvailableFunds() : Single ~ getDatabaseId() : Integer + = public - = private # = protected ~ = package Class Account Private balance As Single = 0 Public limit As Single = 0 Protected id As Integer Friend databaseId As Integer Public Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Private Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub Protected Function getId() As Integer Return id End Function Friend Function getDatabaseId() As Integer Return databaseId End Function End Class Class Account Private balance As Single = 0 Public limit As Single = 0 Protected id As Integer Friend databaseId As Integer Public Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Private Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub Protected Function getId() As Integer Return id End Function Friend Function getDatabaseId() As Integer Return databaseId End Function End Class Visibility – VB.Net
Copyright Jason Gorman Class & Instance Scope – C# Person - numberOfPeople : int - name : string + createPerson(name : string) : Person + getName() : string + getNumberOfPeople() : int - Person(name : string) class Person { private static int numberOfPeople = 0; private string name; private Person(string name) { this.name = name; numberOfPeople++; } public static Person createPerson(string name) { return new Person(name); } public string getName() { return this.name; } public static int getNumberOfPeople() { return numberOfPeople; } class Person { private static int numberOfPeople = 0; private string name; private Person(string name) { this.name = name; numberOfPeople++; } public static Person createPerson(string name) { return new Person(name); } public string getName() { return this.name; } public static int getNumberOfPeople() { return numberOfPeople; } short noOfPeople = Person.getNumberOfPeople(); Person p = Person.createPerson("Jason Gorman"); System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() == noOfPeople + 1); short noOfPeople = Person.getNumberOfPeople(); Person p = Person.createPerson("Jason Gorman"); System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() == noOfPeople + 1);
Copyright Jason Gorman Class & Instance Scope – VB.Net Person - numberOfPeople : Integer - name : String + createPerson(name : String) : Person + getName() : String + getNumberOfPeople() : Integer - New(name : String) Public Class Person Private Shared numberOfPeople As Integer = 0 Private name As String Public Shared Function createPerson(ByVal name As String) As Person Return New Person(name) End Function Private Sub New(ByVal name As String) Me.name = name numberOfPeople = numberOfPeople + 1 End Sub Public Function getName() As String Return name End Function Public Shared Function getNumberOfPeople() As Integer Return numberOfPeople End Function End Class Public Class Person Private Shared numberOfPeople As Integer = 0 Private name As String Public Shared Function createPerson(ByVal name As String) As Person Return New Person(name) End Function Private Sub New(ByVal name As String) Me.name = name numberOfPeople = numberOfPeople + 1 End Sub Public Function getName() As String Return name End Function Public Shared Function getNumberOfPeople() As Integer Return numberOfPeople End Function End Class Dim noOfPeople As Integer = Person.getNumberOfPeople() Dim p As Person = Person.createPerson("Jason Gorman") System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() _ = noOfPeople + 1) Dim noOfPeople As Integer = Person.getNumberOfPeople() Dim p As Person = Person.createPerson("Jason Gorman") System.Diagnostics.Debug.Assert(Person.getNumberOfPeople() _ = noOfPeople + 1)
Copyright Jason Gorman Associations – C# AB 1 b multiplicity role name A b : B Equivalent to class A { public B b = new B(); } class B { } class A { public B b = new B(); } class B { } A a = new A(); B b = a.b; A a = new A(); B b = a.b; 1
Copyright Jason Gorman Associations – VB.Net AB 1 b multiplicity role name A b : B Equivalent to Class A Public b As New B() End Class Class B End Class Class A Public b As New B() End Class Class B End Class Dim a As New A() Dim b As B = a.b Dim a As New A() Dim b As B = a.b 1
Copyright Jason Gorman Bi-directional Associations – C# A b : B Equivalent to class A { public B b; public A() { b = new B(this); } class B { public A a; public B(A a) { this.a = a; } class A { public B b; public A() { b = new B(this); } class B { public A a; public B(A a) { this.a = a; } A a = new A(); B b = a.b; A a1 = b.a; System.Diagnostics.Debug.Assert(a == a1); A a = new A(); B b = a.b; A a1 = b.a; System.Diagnostics.Debug.Assert(a == a1); B a : A AB 1 b multiplicity role name a 1
Copyright Jason Gorman Bi-directional Associations – VB.Net AB 1 b multiplicity role name A b : B Equivalent to Class A Public b As B() Sub New() b = New B(Me) End Sub End Class Class B Public a As A Public Sub New(ByRef a As A) Me.a = a End Sub End Class Class A Public b As B() Sub New() b = New B(Me) End Sub End Class Class B Public a As A Public Sub New(ByRef a As A) Me.a = a End Sub End Class Dim a As New A() Dim b As B = a.b Dim a1 As A = b.a System.Diagnostics.Debug.Assert(a Is a1) Dim a As New A() Dim b As B = a.b Dim a1 As A = b.a System.Diagnostics.Debug.Assert(a Is a1) a 1 B a : A
Copyright Jason Gorman Association names & role defaults PersonAddress Lives at Default role name = address Default multiplicity = 1 class Person { // association: Lives at public Address address; public Person(Address address) { this.address = livesAt; } class Person { // association: Lives at public Address address; public Person(Address address) { this.address = livesAt; } Public Class Person ' association: Lives at Public address As Address Public Sub New(ByVal address As address) Me.address = address End Sub End Class Public Class Person ' association: Lives at Public address As Address Public Sub New(ByVal address As address) Me.address = address End Sub End Class
Copyright Jason Gorman Multiplicity & Collections CustomerAccount 1..* accounts class Customer { // accounts[1..*] : Account public System.Collections.ArrayList accounts = new ArrayList(); public Customer() { Account defaultAccount = new Account(); accounts.Add(defaultAccount); } class Customer { // accounts[1..*] : Account public System.Collections.ArrayList accounts = new ArrayList(); public Customer() { Account defaultAccount = new Account(); accounts.Add(defaultAccount); } Class Customer ’ accounts[1..*] : Account Public accounts As New ArrayList() Public Sub New() Dim defaultAccount As New Account() accounts.Add(defaultAccount) End Sub End Class Class Customer ’ accounts[1..*] : Account Public accounts As New ArrayList() Public Sub New() Dim defaultAccount As New Account() accounts.Add(defaultAccount) End Sub End Class Customer accounts[1..*] : Account Equivalent to 1..2
Copyright Jason Gorman Aggregation & Composition ComputerHardwareDevice 1..* Aggregation – is made up of objects that can be shared or exchanged ShoppingBasketOrderItem 1..* Composition – is composed of objects that cannot be shared or exchanged and live only as long as the composite object
Copyright Jason Gorman Generalization Person Employee class Person { } class Employee : Person { } class Person { } class Employee : Person { } Class Person End Class Class Employee Inherits Person End Class Class Person End Class Class Employee Inherits Person End Class
Copyright Jason Gorman Realization > IPerson Employee IPerson OR interface IPerson { } class Employee : IPerson { } interface IPerson { } class Employee : IPerson { } Interface IPerson End Interface Class Employee Implements IPerson End Class Interface IPerson End Interface Class Employee Implements IPerson End Class
Copyright Jason Gorman Overriding Operations – C# Account + deposit(amount : float) + withdraw(amount : float) SettlementAccount + withdraw(amount : float) -debt : float = 0 / availableFunds : float = balance + limit - debt # balance : float = 0 # limit : float = 0 class Account { protected float balance = 0; protected float limit = 0; public virtual void deposit(float amount) { balance = balance + amount; } public virtual void withdraw(float amount) { balance = balance - amount; } class SettlementAccount : Account { private float debt = 0; float availableFunds() { return (balance + limit - debt); } public override void withdraw(float amount) { if (amount > this.availableFunds()) { throw new InsufficientFundsException(); } base.withdraw(amount); } class Account { protected float balance = 0; protected float limit = 0; public virtual void deposit(float amount) { balance = balance + amount; } public virtual void withdraw(float amount) { balance = balance - amount; } class SettlementAccount : Account { private float debt = 0; float availableFunds() { return (balance + limit - debt); } public override void withdraw(float amount) { if (amount > this.availableFunds()) { throw new InsufficientFundsException(); } base.withdraw(amount); }
Copyright Jason Gorman Overriding Operations – VB.Net Account + deposit(amount : Single) + withdraw(amount : Single) SettlementAccount + withdraw(amount : Single) -debt : Single = 0 / availableFunds : Single = balance + limit - debt # balance : Single = 0 # limit : Single = 0 Class Account Protected balance As Single = 0 Protected limit As Single = 0 Public Overridable Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Overridable Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub End Class Class SettlementAccount Inherits Account Private debt As Single = 0 Public Function availableFunds() As Single Return (balance + limit - debt) End Function Public Overrides Sub withdraw(ByVal amount As Single) If amount > availableFunds() Then Throw New InsufficientFundsException() End If MyBase.withdraw(amount) End Sub End Class Class Account Protected balance As Single = 0 Protected limit As Single = 0 Public Overridable Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Overridable Sub withdraw(ByVal amount As Single) balance = balance - amount End Sub End Class Class SettlementAccount Inherits Account Private debt As Single = 0 Public Function availableFunds() As Single Return (balance + limit - debt) End Function Public Overrides Sub withdraw(ByVal amount As Single) If amount > availableFunds() Then Throw New InsufficientFundsException() End If MyBase.withdraw(amount) End Sub End Class
Copyright Jason Gorman Abstract Classes & Abstract Operations – C# Account + deposit(amount : float) + withdraw(amount : float) SettlementAccount + deposit(amount : float) + withdraw(amount : float) - balance : float = 0 - limit : float = 0 - debt : float = 0 / availableFunds : float = balance + limit - debt abstract class Account { public abstract void deposit(float amount); public abstract void withdraw(float amount); } class SettlementAccount : Account { private float balance = 0; private float limit = 0; private float debt = 0; float availableFunds() { return (balance + limit - debt); } public override void deposit(float amount) { balance = balance + amount; } public override void withdraw(float amount) { if (amount > this.availableFunds()) { throw new InsufficientFundsException(); } balance = balance - amount; } abstract class Account { public abstract void deposit(float amount); public abstract void withdraw(float amount); } class SettlementAccount : Account { private float balance = 0; private float limit = 0; private float debt = 0; float availableFunds() { return (balance + limit - debt); } public override void deposit(float amount) { balance = balance + amount; } public override void withdraw(float amount) { if (amount > this.availableFunds()) { throw new InsufficientFundsException(); } balance = balance - amount; }
Copyright Jason Gorman Abstract Classes & Abstract Operations – VB.Net Account + deposit(amount : Single) + withdraw(amount : Single) SettlementAccount + deposit(amount : Single) + withdraw(amount : Single) - balance : Single = 0 - limit : Single = 0 - debt : Single = 0 / availableFunds : Single = balance + limit - debt MustInherit Class Account Public MustOverride Sub deposit(ByVal amount As Single) Public MustOverride Sub withdraw(ByVal amount As Single) End Class Class SettlementAccount Inherits Account Private balance As Single = 0 Private limit As Single = 0 Private debt As Single = 0 Public Function availableFunds() As Single Return (balance + limit - debt) End Function Public Overrides Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Overrides Sub withdraw(ByVal amount As Single) If amount > availableFunds() Then Throw New InsufficientFundsException() End If balance = balance - amount End Sub End Class MustInherit Class Account Public MustOverride Sub deposit(ByVal amount As Single) Public MustOverride Sub withdraw(ByVal amount As Single) End Class Class SettlementAccount Inherits Account Private balance As Single = 0 Private limit As Single = 0 Private debt As Single = 0 Public Function availableFunds() As Single Return (balance + limit - debt) End Function Public Overrides Sub deposit(ByVal amount As Single) balance = balance + amount End Sub Public Overrides Sub withdraw(ByVal amount As Single) If amount > availableFunds() Then Throw New InsufficientFundsException() End If balance = balance - amount End Sub End Class
Copyright Jason Gorman More on Generalization A DCB A DCB Equivalent to {abstract} Mammal BirdCatHuman Mammal BirdCatHuman Equivalent to
Copyright Jason Gorman Dependencies – C# CustomerDAO DataAccessObject System::Data::SqlClient::SqlDataReader + find(id : int) : Customer public Customer find(int id) { SqlConnection cn = new SqlConnection(CONNECT_STRING); cn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE id = “ + id.ToString(), cn); SqlDataReader reader = cmd.ExecuteReader(); Customer c; while (reader.Read()) { c = new Customer(reader.GetInt32(0), reader.GetString(1)); } cn.Close(); return c; } public Customer find(int id) { SqlConnection cn = new SqlConnection(CONNECT_STRING); cn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE id = “ + id.ToString(), cn); SqlDataReader reader = cmd.ExecuteReader(); Customer c; while (reader.Read()) { c = new Customer(reader.GetInt32(0), reader.GetString(1)); } cn.Close(); return c; }
Copyright Jason Gorman Dependencies – VB.Net CustomerDAO DataAccessObject System::Data::SqlClient::SqlDataReader + find(id : Integer) : Customer Public Function find(ByVal id As Integer) As Customer Dim cn As New SqlConnection(CONNECT_STRING) cn.Open() Dim cmd As New SqlCommand("SELECT * FROM Customers WHERE id = " _ & id.ToString(), cn) Dim reader As SqlDataReader = cmd.ExecuteReader() Dim c As Customer Do While reader.Read() c = New Customer(reader.GetInt32(0), reader.GetString(1)) Loop cn.Close() Return c End Function Public Function find(ByVal id As Integer) As Customer Dim cn As New SqlConnection(CONNECT_STRING) cn.Open() Dim cmd As New SqlCommand("SELECT * FROM Customers WHERE id = " _ & id.ToString(), cn) Dim reader As SqlDataReader = cmd.ExecuteReader() Dim c As Customer Do While reader.Read() c = New Customer(reader.GetInt32(0), reader.GetString(1)) Loop cn.Close() Return c End Function
Copyright Jason Gorman Qualified Associations – C# LibraryTitle 0..* 0..1 ISBN 0..* item 0..* class Library { public Hashtable titles = new Hashtable(); public Title item(string ISBN) { return (Title)titles.Item(ISBN); } class Library { public Hashtable titles = new Hashtable(); public Title item(string ISBN) { return (Title)titles.Item(ISBN); }
Copyright Jason Gorman Qualified Associations – VB.Net LibraryTitle 0..* 0..1 ISBN 0..* item 0..* Class Library Public titles As New Hashtable() Public Function item(ByVal ISBN As String) As Title Return CType(titles.Item(ISBN), Title) End Function End Class Class Library Public titles As New Hashtable() Public Function item(ByVal ISBN As String) As Title Return CType(titles.Item(ISBN), Title) End Function End Class
Copyright Jason Gorman Association Classes – C# CustomerVideo Rental - dateRented : DateTime * class Customer { ArrayList rentals = new ArrayList(); } class Video { Rental rental; } class Rental { public Customer customer; public Video video; private DateTime dateRented; public Rental(DateTime dateRented, Customer customer, Video video) { this.dateRented = dateRented; video.rental = this; customer.rentals.Add(this); this.customer = customer; this.video = video; } class Customer { ArrayList rentals = new ArrayList(); } class Video { Rental rental; } class Rental { public Customer customer; public Video video; private DateTime dateRented; public Rental(DateTime dateRented, Customer customer, Video video) { this.dateRented = dateRented; video.rental = this; customer.rentals.Add(this); this.customer = customer; this.video = video; } + Rental(DateTime, Customer, Video)
Copyright Jason Gorman Association Classes – VB.Net Class Customer Public rentals As New ArrayList() End Class Class Video Public rental As Rental End Class Class Rental Public video As video Public customer As customer Private dateRented As DateTime Public Sub New(ByVal dateRented As DateTime, _ ByVal video As video, _ ByVal customer As Customer) Me.dateRented = dateRented video.rental = Me customer.rentals.Add(Me) Me.video = video Me.customer = customer End Sub End Class Class Customer Public rentals As New ArrayList() End Class Class Video Public rental As Rental End Class Class Rental Public video As video Public customer As customer Private dateRented As DateTime Public Sub New(ByVal dateRented As DateTime, _ ByVal video As video, _ ByVal customer As Customer) Me.dateRented = dateRented video.rental = Me customer.rentals.Add(Me) Me.video = video Me.customer = customer End Sub End Class CustomerVideo Rental - dateRented : DateTime * + Rental(DateTime, Customer, Video)
Copyright Jason Gorman Associations, Visibility & Scope LibraryTitle 0..* - titles Class Library Private titles As ArrayList End Class Class Library Private titles As ArrayList End Class TeamPerson 2..* # members class Team { protected ArrayList members; } class Team { protected ArrayList members; } Customer 0..* - allInstances class Customer { private static ArrayList allInstances; } class Customer { private static ArrayList allInstances; }
Copyright Jason Gorman Information Hiding – C# Person name : string parents 0..2 children0..* class Person { public string name; public Parent[] parents = new Parent[2]; public ArrayList children = new ArrayList(); } class Person { public string name; public Parent[] parents = new Parent[2]; public ArrayList children = new ArrayList(); } Person mary = new Person(); Person ken = new Person(); Person jason = new Person(); jason.parents[0] = mary; jason.parents[1] = ken; mary.children.Add(jason); ken.children.Add(jason); jason.name = "Jason"; Person mary = new Person(); Person ken = new Person(); Person jason = new Person(); jason.parents[0] = mary; jason.parents[1] = ken; mary.children.Add(jason); ken.children.Add(jason); jason.name = "Jason";
Copyright Jason Gorman Information Hiding – C# Person - name : string - parents children0..* Person mary = new Person(); Person ken = new Person(); Person jason = new Person(mary, ken); jason.setName("Jason"); Person mary = new Person(); Person ken = new Person(); Person jason = new Person(mary, ken); jason.setName("Jason"); class Person { private string name; private Parent[] parents = new Parent[2]; private ArrayList children = new ArrayList(); public Person(Person mother, Person father) { this.setParent(0, mother); this.setParent(1, father); } public void setName(string value) { this.name = value; } public void setParent(int index, Person parent) { parents[index] = parent; parent.addChild(this); } public void addChild(Person child) { this.children.Add(child); } public Person() { } class Person { private string name; private Parent[] parents = new Parent[2]; private ArrayList children = new ArrayList(); public Person(Person mother, Person father) { this.setParent(0, mother); this.setParent(1, father); } public void setName(string value) { this.name = value; } public void setParent(int index, Person parent) { parents[index] = parent; parent.addChild(this); } public void addChild(Person child) { this.children.Add(child); } public Person() { } + Person(mother : Person, father : Person) + Person() + setName(value :string) + setParent(index : int, parent : Person) + addChild(child : Person)
Copyright Jason Gorman Information Hiding – VB.Net Person name : String parents 0..2 children0..* Class Person Public name As String Public parents(2) As Person Public children As New ArrayList() End Class Class Person Public name As String Public parents(2) As Person Public children As New ArrayList() End Class Dim ken As New Person() Dim mary As New Person() Dim jason As New Person() jason.parents(0) = ken jason.parents(1) = mary ken.children.Add(jason) mary.children.Add(jason) jason.name = "Jason" Dim ken As New Person() Dim mary As New Person() Dim jason As New Person() jason.parents(0) = ken jason.parents(1) = mary ken.children.Add(jason) mary.children.Add(jason) jason.name = "Jason"
Copyright Jason Gorman Information Hiding – VB.Net Person - name : string - parents children0..* Dim ken As New Person() Dim mary As New Person() Dim jason As New Person(mary, ken) jason.setName("Jason") Dim ken As New Person() Dim mary As New Person() Dim jason As New Person(mary, ken) jason.setName("Jason") Class Person Private name As String Private parents(2) As Person Private children As New ArrayList() Public Sub New(ByVal mother As Person, ByVal father As Person) setParent(0, mother) setParent(1, father) End Sub Public Sub New() End Sub Public Sub setParent(ByVal index As Integer, ByVal parent As Person) parents(index) = parent parent.addChild(Me) End Sub Public Sub addChild(ByVal child As Person) children.Add(child) End Sub Public Sub setName(ByVal value As String) name = value End Sub End Class Class Person Private name As String Private parents(2) As Person Private children As New ArrayList() Public Sub New(ByVal mother As Person, ByVal father As Person) setParent(0, mother) setParent(1, father) End Sub Public Sub New() End Sub Public Sub setParent(ByVal index As Integer, ByVal parent As Person) parents(index) = parent parent.addChild(Me) End Sub Public Sub addChild(ByVal child As Person) children.Add(child) End Sub Public Sub setName(ByVal value As String) name = value End Sub End Class + New(mother : Person, father : Person) + New() + setName(value : String) + setParent(index : Integer, parent : Person) + addChild(child : Person)
Copyright Jason Gorman Exercise #1 - Beginner Book # title : string - author : string ~ published : DateTime = DateTime.Now() - comments[0..*] : string + createBook(title : string, author : string, published : DateTime) : Book - setTitle(value : string) + addComment(comment : string) Write the C# or VB.Net code to implement the class diagram exactly as you see it above [ VB.Net developers should choose equivalent data types if necessary]
Copyright Jason Gorman Exercise #2 - Intermediate A BC I D 0..* * 1 Write the C# or VB.Net code to implement the class diagram exactly as you see it above
Copyright Jason Gorman Exercise #3 - Advanced A + doStuff() B C D theC key : string 0..* 1 - cs Write the C# or VB.Net code to implement the class diagram exactly as you see it above * ~ children 0..1