UML Class Diagram Review
Association (straight line sometimes with an arrow)
The association link indicates that two classes have a relationship. The arrow indicates which class has the responsibility for maintaining the relationship. For example a Car class would maintain the relationship of knowing about the driver. An association is implemented as an instance in one class. The Car object would contain an instance of a driver.
public class Car
{private Driver _driver;
public Driver Driver
{get { return _driver; }
set { _driver = value; }
}
public Car(Driver driver)
{Driver = _driver;
}
}
Associations can often be recognized by using "Uses" or "has" in a sentence. For example, a car has a driver.
Composition (solid black diamond)
Indicates that one class belongs to the other. For example a Hand is made up of fingers. If the Hand is destroyed, so are the fingers. A composition is usually implemented as a collection on one class.
public class Hand
{private IList<Finger> _fingerList;
public IList<Finger> FingerList
{get { return _fingerList; }
set { _fingerList = value; }
}
public Hand() { }}

Aggregation (white diamond)
Aggregation is similar to composition. Aggregation is less rigorous than composition. For example, an order is made up of several products. If an order is deleted the products will continue to exist. An aggregation is usually implemented as a collection on one class.
public class Order
{private IList<Product> _productList;
public IList<Product> ProductList
{get { return _productList; }
set { _productList = value; }
}
public Order() { }}

Composition And Aggregation
Composition and aggregation relationships can be recognized by using "part of" or "has" in a sentence. The relationships determined above could be placed in the following sentences.
- A Finger is part of a Hand.
- A Hand has fingers.
- A Product is part of a Order.
- A Order has Products.
Generalization (Open white Arrow)
Generalization refers to an inheritance relationship. Inheritance allows for a class to receive the characteristics of the inherited class. In .Net a class can only inherit from one class. For example, the SportsCar class would inherit from the Car class. The Car class would contain the general characteristics of a car. The SportsCar class would inherit the properties for color, engine and max speed. Additionally the SportsCar class would inherit methods to drive and add fuel to the car.

Interface (Dotted line with arrow pointing at interface)
An Interface is a contract. The contract dictates which methods and properties a class must implement. An interface guarantees that certain members will be implemented by a class. Interfaces are a great resource when creating unit tests. Interfaces allow you to create unit tests without having to worrying about the implementation. In .net a class can implement as many interfaces as desired. Typical naming conventions for Interfaces prefix the name of the interface with the letter "I".

The interface
public interface ICar
{ //Propertiesint Fuel { get; set; }
string Color { get; set; }
string MaxSpeed { get; set;}
//Methodsvoid Drive(int distance);
void AddFuel(int fuel);
void RemoveFuel(int fuel);
}
Implementing the interface
public class SportsCar : ICar
{ #region Private Fieldsprivate string _color;
private string _maxSpeed;
private int _fuel;
#endregion #region Public Fieldspublic int Fuel
{get { return _fuel; }
set { _fuel = value; }
}
public string Color
{get { return _color; }
set { _color = value; }
}
public string MaxSpeed
{get { return _maxSpeed; }
set { _maxSpeed = value; }
}
#endregion #region Public Methodspublic void Drive(int distance)
{ //Ok this is not a real //calculation. However it gives //you the general idea.RemoveFuel(distance);
}
public void AddFuel(int fuel)
{Fuel += fuel;
}
public void RemoveFuel(int fuel)
{Fuel -= fuel;
}
#endregion}
Further Resources
The following articles might be of use:
Uml Tutorial
Another explanation
If you would like to take an even deeper look at UML diagrams I suggest you purchase Martin Fowler’s UML Distilled. I often use this book as a quick reference. Fowler's writing style is very down to earth and should appeal to most audiences.
