4.2类图
类图在UML使用较为广泛。类图代表系统的静态结构。系统的静态结构由类和他们之间的关系组成。
类图包括属性,操作,和关系。关系则包括我们常说的1:1,1:n,0:1,0:n,n:n等
上图是一张类图,有三个类:Customer用户,Order订单,OrderDetail订单明细。
Customer上面有三个属性:CustomerID,CustomerName,Address。CustomerID是public,前面的符号是+;CustomerName是protected,前面的符号是#;Address是private,前面的符号是-。
连接类之间的线代表他们之间的关系,也就是我们常说的1:1,1:n,0:1,0:n,n:n,0:n其中之一,关系还区分从哪个类的角度来看。图中的Customer和Order的关系,从Customer角度来看,一个Customer可以没有Order,也可以由多个Order,所以他们之间就是0:n的关系;从Order角度看,一个Order肯定属于一个Customer,所以他们之间就是1:1的关系。
一个Order肯定有一个以上的Detail,一个Detai肯定属于一个Order。
上面的类图是在PowerDesigner中画的,下面是自动生成的c#代码,当然了,肯定是需要调整的。
// File:
Customer.cs // Author: zy
// Created: 2010年8月6日 6:40:04
// Purpose: Definition of Class Customer using System;
public class Customer
{
private string address;
protected string customerName;
public Customer GetCustomerByID()
{
throw new NotImplementedException();
}
public int GetCustomerOrders()
{
throw new NotImplementedException();
}
public int customerID;
public System.Collections.Generic.List<Order> order;
/// <summary>
/// Property for collection of Order
/// </summary>
/// <pdGenerated>Default opposite class collection property</pdGenerated>
public System.Collections.Generic.List<Order> Order
{
get
{
if (order == null)
order = new System.Collections.Generic.List<Order>();
return order;
}
set
{
RemoveAllOrder();
if (value != null)
{
foreach (Order oOrder in value)
AddOrder(oOrder);
}
}
}
/// <summary>
/// Add a new Order in the collection
/// </summary>
/// <pdGenerated>Default Add</pdGenerated>
public void AddOrder(Order newOrder) {
if (newOrder == null) return;
if (this.order == null) this.order = new System.Collections.Generic.List<Order>();
if (!this.order.Contains(newOrder))
this.order.Add(newOrder);
}
/// <summary>
/// Remove an existing Order from the collection
/// </summary>
/// <pdGenerated>Default Remove</pdGenerated>
public void RemoveOrder(Order oldOrder)
{
if (oldOrder == null)
return;
if (this.order != null)
if (this.order.Contains(oldOrder))
this.order.Remove(oldOrder);
}
/// <summary>
/// Remove all instances of Order from the collection
/// </summary>
/// <pdGenerated>Default removeAll</pdGenerated>
public void RemoveAllOrder()
{
if (order != null)
order.Clear();
}
}
Customer.cs // Author: zy
// Created: 2010年8月6日 6:40:04
// Purpose: Definition of Class Customer using System;
public class Customer
{
private string address;
protected string customerName;
public Customer GetCustomerByID()
{
throw new NotImplementedException();
}
public int GetCustomerOrders()
{
throw new NotImplementedException();
}
public int customerID;
public System.Collections.Generic.List<Order> order;
/// <summary>
/// Property for collection of Order
/// </summary>
/// <pdGenerated>Default opposite class collection property</pdGenerated>
public System.Collections.Generic.List<Order> Order
{
get
{
if (order == null)
order = new System.Collections.Generic.List<Order>();
return order;
}
set
{
RemoveAllOrder();
if (value != null)
{
foreach (Order oOrder in value)
AddOrder(oOrder);
}
}
}
/// <summary>
/// Add a new Order in the collection
/// </summary>
/// <pdGenerated>Default Add</pdGenerated>
public void AddOrder(Order newOrder) {
if (newOrder == null) return;
if (this.order == null) this.order = new System.Collections.Generic.List<Order>();
if (!this.order.Contains(newOrder))
this.order.Add(newOrder);
}
/// <summary>
/// Remove an existing Order from the collection
/// </summary>
/// <pdGenerated>Default Remove</pdGenerated>
public void RemoveOrder(Order oldOrder)
{
if (oldOrder == null)
return;
if (this.order != null)
if (this.order.Contains(oldOrder))
this.order.Remove(oldOrder);
}
/// <summary>
/// Remove all instances of Order from the collection
/// </summary>
/// <pdGenerated>Default removeAll</pdGenerated>
public void RemoveAllOrder()
{
if (order != null)
order.Clear();
}
}
UML是一种标准的对象建模语言。它独立于任何编程语言。这是一个关键的特性,既是一个优点,也是一个缺点。说它是一个优点,是因为它使得在使用UML进行建模以及表达业务流程的时候,变成一个非常强大的工具。同时可以用它产生一个通用的模型。缺点就是因为优点产生的,独立性越强,也就越是远离了系统运行的代码。