Thursday, April 26, 2007

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.

(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.

(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
    int Fuel { get; set; }
    string Color { get; set; }
    string MaxSpeed { get; set;}
    void Drive(int distance);
    void AddFuel(int fuel);
    void RemoveFuel(int fuel);

Implementing the interface
public class SportsCar : ICar
    #region Private Fields
    private string _color;
    private string _maxSpeed;
    private int _fuel;
    #region Public Fields
    public 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; }
    #region Public Methods
    public void Drive(int distance)
        //Ok this is not a real
        //calculation. However it gives
        //you the general idea.
    public void AddFuel(int fuel)
        Fuel += fuel;    
    public void RemoveFuel(int fuel)
        Fuel -= fuel;    

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.


Anonymous said...

It's so easy to choose high quality [url=]replica watches[/url] online: [url=]Rolex replica[/url], [url=]Breitling replica[/url], Chanel replica or any other watch from the widest variety of models and brands.

Anonymous said...

Looking for [url=]viagra[/url] ?Accede Our Blue Prices at, The Respected [b][url=]Online Chemist's snitch on [/url][/b] To [url=]Buy Viagra[/url] Online ! You Can also Go up Total Deals When You [url=]Buy Cialis[/url] and When You You [url=]Buy Levitra[/url] Online. We Also Familiarity a Unmatched Generic [url=]Phentermine[/url] In support of Your Regimen ! We Dole out up M‚layer signpost [url=]Viagra[/url] and Also [url=]Generic Viagra[/url] !

Free Poker said...

Happy new 2013.

Poker sem deposito
holdem bonus ohne einzahlung
darmo poker bez depozytu
chips for online poker
free holdem poker play

marko said...

Owning land or having one is the vast majority's fantasy however this fantasy is fairly difficult to go after purchasing a package of land is exceptionally costly. That is the reason heaps of individuals ask assistance from bank or private loaning organization with the goal that they can buy the land they needed to purchase. Cash Advance Chula-vista

Justin said...

In the event that these thoughts are valuable to you or on the off chance that you have questions with respect to the above please leave a remark and I will attempt to answer them in a different article. Likewise continue returning for section 2 of this article in several days. Cash Advance Chula-vista