Copyright Jason Gorman 20031 UML for.NET Developers Class Diagrams.

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Advanced Object Oriented Concepts Tonga Institute of Higher Education.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
UML Class and Sequence Diagrams Violet Slides adapted from Marty Stepp, CSE 403, Winter 2012 CSE 403 Spring 2012 Anton Osobov.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
UML Class Diagram. UML Class Diagrams2 Agenda What is a Class Diagram? Essential Elements of a UML Class Diagram Tips.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
More about classes and objects Classes in Visual Basic.NET.
1 Inheritance & Interface. 2 Why Inheritance? Common properties? Methods to send congratulations to Employees and Customers Public Sub SendCongratulationsE(ByVal.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
UML a crash course Alex Lo Brian Kiefer. Overview Classes Class Relationships Interfaces Objects States Worksheet.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Unified Modeling Language
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Inheritance Chapter 14. What is inheritance?  The ability to create a class from another class, the “parent” class, extending the functionality and state.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Class Diagram.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Module 7: Object-Oriented Programming in Visual Basic .NET
CS-434: Object-Oriented Programming Using Java Week 2
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Introduction to Java Class Diagrams. Classes class Account { … } Account.
Training: INSPIRE Basics EC JRC 1/15 UML class diagram: example INSPIRE UML class diagram for administrative units.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
What is inheritance? It is the ability to create a new class from an existing class.
Object-Oriented Programming in C++
Composition - 1 J. Sant.. Agenda Define Composition Reuse and Composition. Composition in Java. Composition Exercise with simple containment.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Distributed Java Programming Distributed Java Programming Class #2 August 22, 2002.
Domain Model Classes and Objects Association Structure Requirement Specification Domain Model.
UML Class Diagrams 1 These lecture slides are copyright (C) Marty Stepp, They may not be rehosted, sold, or modified without expressed permission.
10-Nov-15 Java Object Oriented Programming What is it?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Visibility Control.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Interfaces.
UML The Unified Modeling Language A Practical Introduction Al-Ayham Saleh Aleppo University
Peyman Dodangeh Sharif University of Technology Fall 2014.
2007ACS-3913 Ron McFadyen1 Class Diagram See Schaum’s UML Outline, especially chapters 4, 5, 6, 7.
Object Oriented Analysis and Design Class and Object Diagrams.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Class diagrams Terézia Mézešová.
Chapter 16 UML Class Diagrams.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Intermediate PHP (4) Object-Oriented PHP (2). Object-oriented concepts Classes, attributes and operations Class attributes Per-class constants Class method.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UML Review – class diagrams SE-2030 Dr. Mark L. Hornick 1.
These materials where developed by Martin Schray
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Unified Modeling Language (UML)
UML Class Diagrams.
Object-Oriented Programming: Classes and Objects
Chapter 16 UML Class Diagrams.
Inheritance and Polymorphism
Advanced Programming in Java
Packages, Interfaces & Exception Handling
Object-Oriented Programming: Classes and Objects
Object Oriented Analysis and Design
The Basics of Class Diagrams for a single class
UML Class Diagram.
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Unified Modelling Language
Software Engineering System Modeling Extra examples Dr.Doaa Sami
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
Object-Oriented Programming
Presentation transcript:

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