UML For Java Developers: Class Diagrams
UML For Java Developers: Class Diagrams
Class Diagrams
1
© Jason Gorman 2005. All rights reserved.
"I am currently working on a
team which is [in] the process
of adopting RUP and UML
UML for Java Developers (5 Days)
standards and practices. After Since Autumn 2003, over 100,000 Java and .NET developers have
one of our design sessions, I learned the Unified Modeling Language from Parlez UML
needed to lookup some (http://www.parlezum l.com) , making it one of the most popular
information and came across UML training resources on the Internet.
your site. Absolutely great!
Most [of] the information I've
had questions about is UML for Java Developers is designed to accelerate the learning
contained within your tutorials process by explaining UML in a language Java developers can
and then some." understand – Java!
advertisement
2
© Jason Gorman 2005. All rights reserved.
UML for Java Developers covers the most useful aspects of the
UML standard, applying each notation w ithin the context of an
iterative, object oriented development process
Activity Diagrams
Object Constraint Language
Model the flow of use cases
Model business rules and create
and single and multi-threaded unambiguous specifications
code
www.parlezuml.com/training.htm
3
© Jason Gorman 2005. All rights reserved.
Classes
class Account
class Account
Account
{
{
}
}
4
© Jason Gorman 2005. All rights reserved.
Attributes
class Account
class Account
{
{
private float balance = 0;
private float balance = 0;
Account private float limit;
private float limit;
- balance : Single = 0 }
- limit : Single }
5
© Jason Gorman 2005. All rights reserved.
Account
- balance : Single = 0
Operations
- limit : Single
+ deposit(amount : Single) [visibility] op_name([[in|out] parameter : type[, more params]])[: return_type]
+ withdraw(amount : Single)
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;
}
}
6
© Jason Gorman 2005. All rights reserved.
class Account
class Account
{
{
private float balance = 0;
private float balance = 0;
Account public float limit;
public float limit;
protected int id;
- balance : float = 0 protected int id;
+ limit : float int databaseId;
int databaseId;
# id : int
~ databaseId : int public void deposit(float amount)
public void deposit(float amount)
+ deposit(amount : single) {
{
-withdraw(amount : single) balance = balance + amount;
balance = balance + amount;
# getAvailableFunds() : single }
~ getDatabaseId() : int }
int getDatabaseId()
Visibility
int getDatabaseId()
{
{
return databaseId;
return databaseId;
}
}
}
}
7
© Jason Gorman 2005. All rights reserved.
class Person
class Person
{
int noOfPeople = Person.getNumberOfPeople(); {
int noOfPeople = Person.getNumberOfPeople();
private static int numberOfPeople = 0;
Person p = Person.createPerson("Jason Gorman"); private static int numberOfPeople = 0;
Person p = Person.createPerson("Jason Gorman");
private String name;
private String name;
Scope
public static int getNumberOfPeople()
public static int getNumberOfPeople()
{
{
return numberOfPeople;
return numberOfPeople;
}
}
}
}
8
© Jason Gorman 2005. All rights reserved.
Associations
multiplicity
1 1 A
A B
b b:B
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
{
{
}
}
9
© Jason Gorman 2005. All rights reserved.
Bi-directional Associations
multiplicity
1 1 A
A B
a b b:B
class A a: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;
class B
class B A a1 = b.a;
A a1 = b.a;
{
{ assert a == a1;
assert a == a1;
public A a;
public A a;
public B(A a)
public B(A a)
{
{
this.a = a;
this.a = a;
}
} 10
}
}
© Jason Gorman 2005. All rights reserved.
Association names & role
defaults
Lives at
Person Address
class Person
class Person
{
{
// association: Lives at
// association: Lives at
public Address address;
public Address address;
11
© Jason Gorman 2005. All rights reserved.
Multiplicity & Collections
1..2 1..* Customer
Customer Account
accounts accounts[1..*] : Account
Equivalent to
class Customer
class Customer
{
{
// accounts[1..*] : Account
// accounts[1..*] : Account
ArrayList accounts = new ArrayList();
ArrayList accounts = new ArrayList();
public Customer()
public Customer()
{
{
Account defaultAccount = new Account();
Account defaultAccount = new Account();
accounts.add(defaultAccount);
accounts.add(defaultAccount);
}
}
}
}
12
© Jason Gorman 2005. All rights reserved.
Aggregation & Composition
0..1 1..*
Computer HardwareDevice
1 1..*
ShoppingBasket OrderItem
13
© Jason Gorman 2005. All rights reserved.
Generalization
Person
Employee
class Person
class Person
{
{
}
}
14
© Jason Gorman 2005. All rights reserved.
Realization
Person
<<interface>>
Person
OR
Employee Employee
interface Person
interface Person
{
{
}
}
15
© Jason Gorman 2005. All rights reserved.
www.parlezuml.com
16
© Jason Gorman 2005. All rights reserved.