After examining different approaches of drawing UML sequence diagrams, today I came across a good way of drawing class diagrams as well. I don’t know how I didn’t come across this site before! Its a similar concept to the websequencediagrams site, where we can specify in simple text notation and it generates a pretty good diagram from that!

Yuml.me allows us to create very cool looking class, activity and state diagrams using a very elegant text based notation!

I modified one of their examples to include a more comprehensive set of notations (complete list can be found in their samples page:

[Customer]1-orders 0..*>[Order]
[Order]++*-*>[LineItem], [Order]-1>[DeliveryMethod]
[Customer|firstName;lastName;email|save();addOrder(Order)]
[Order]*-*>[Product]
[DeliveryMethod]^[National]
[DeliveryMethod]^[International]
[Customer]uses-.->[PaymentStrategy]
[Customer][Address]

<img src="http://yuml.me/diagram/class/[Customer]1-orders 0..*>[Order], [Order]++*-*>[LineItem], [Order]-1>[DeliveryMethod], [Customer|firstName;lastName;email|save();addOrder(Order)], [Order]*-*>[Product], [DeliveryMethod]^[National], [DeliveryMethod]^[International], [Customer]uses-.->[PaymentStrategy], [Customer][Address].” alt=”UML Class Diagram” />

Some explanation on some aspects of the class diagram…

Composition Relationships Eg: Order – LineItem

Order contains 1 or more instance of type LineItem. This is a relationship known as composition represented by the filled black diamond. The Order class is composed of LineItems. The arrowhead on the other end of the relationship denotes that the relationship is navigable in only one direction. That is, LineItem does not know about Order. In UML relationships are presumed to be bidirectional unless the arrowhead is present to restrict them. Had this arrowhead been omitted, it would have meant that LineItem knew about Order it belonged to.

Inheritance
A kind of relationship, like National and International being two kinds of DeliveryMethods.

Aggregation / Association Eg: Customer – Order

The weak form of aggregation is denoted with an open diamond. This relationship denotes that the aggregate class (the class with the white diamond touching it i.e. Customer) is in some way the “whole”, and the other class Order in the relationship is somehow “part” of that whole. Customer has Orders.

Association is used to indicate a form of containment that does not have a whole / part implication. For example, each Order refers to its DeliveryMethod. This is not aggregation since Order is not part of a DeliveryMethod.

Dependency Eg Customer – PaymentStrategy

Sometimes the relationship between a two classes is very weak. They are not implemented with
member variables at all, but maybe with member function arguments. This is indicated by the dotted line.

Further reading: Object Mentor’s UML resources