Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

Category Archives: Visio

Association vs. Dependency vs. Aggregation vs. Composition

This might sound like a preliminary topic but recently I had a healthy discussion around them and thought of sharing few thoughts here. The description below is in context with Class Diagrams and this blog post uses UML to express the relationship in form a diagram.

Association is reference based relationship between two classes. Here a class A holds a class level reference to class B. Association can be represented by a line between these classes with an arrow indicating the navigation direction. In case arrow is on the both sides, association has bidirectional navigation.

class Asset { ... }
class Player {
Asset asset;
public Player(Assest purchasedAsset) { ... } /*Set the asset via Constructor or a setter*/
}

Dependency is often confused as Association. Dependency is normally created when you receive a reference to a class as part of a particular operation / method. Dependency indicates that you may invoke one of the APIs of the received class reference and any modification to that class may break your class as well. Dependency is represented by a dashed arrow starting from the dependent class to its dependency. Multiplicity normally doesn’t make sense on a Dependency.

class Die { public void Roll() { ... } }
class Player
{
public void TakeTurn(Die die) /*Look ma, I am dependent on Die and it's Roll method to do my work*/
{ die.Roll(); ... }
}

Aggregation is same as association and is often seen as redundant relationship. A common perception is that aggregation represents one-to-many / many-to-many / part-whole relationships (i.e. higher multiplicity), which of course can be represented by via association too (hence the redundancy). As aggregation doesn’t convey anything more effective about a software design than an association, there is no separate UML representation for it (though some developers use a hollow diamond to indicate aggregation). You can give aggregation a miss unless you use it to convey something special.

class Asset { ... }
class Player {
List assets;
public void AddAsset(Asset newlyPurchasedAsset) { assets.Add(newlyPurchasedAssest); ... }
...
}

Composition relates to instance creational responsibility. When class B is composed by class A, class A instance owns the creation or controls lifetime of instance of class B. Needless to say when class instance A instance is destructed (garbage collected), class B instance would meet the same fate. Composition is usually indicated by line connecting two classes with addition of a solid diamond at end of the class who owns the creational responsibility. It’s also a perceived wrong notion that composition is implemented as nested classes. Composition binds lifetime of a specific instance for a given class, while class itself may be accessible by other parts of the system.

public class Piece { ... }
public class Player
{
Piece piece = new Piece(); /*Player owns the responsibility of creating the Piece*/
...
}

Though elementary, I hope above distills your thinking 🙂

Advertisements

Getting Started with Visio

I always knew the importance of Visio to sketch logical diagrams but never found time to explore it beyond a level. Since yesterday though, I have got to know quite a bit about it & I thought of sharing the same with you guys.

Hope you have opened Visio at this point of time. There are various shapes what are shipped with Visio & Visio allows you to create a drawing using it. Toolbar contains shortcut to create a new drawing & also there is a shortcut to access various shapes as shown below.

toolbar

Visio also allows you to create a drawing with default set of shapes, though I don’t find that much useful. I prefer creating a new drawing & then selecting the shapes I want through the shortcut.

The extension for Visio files is .vsd. A single .vsd file can contain only one drawing, though a single drawing can have many pages. You can right click the first page as shown below to add new pages.

pages

It’s also important to note that Visio allows you to open multiple drawings at the same time (like MDI interface). Visio groups related shapes together into an entity called – Stencil. You will find that over a period of time there are certain shapes that you tend to use often. You can group them for easy access by creating your own Stencil. There is one stencil available to you by default called – Favorites.

stencils

To add a shape to your Stencil just drag & drop it from other Stencils. By default stencils are in opened in read only mode. Hence you might see the following popup when you try to drag & drop shapes across Stencils. Click on – ‘Yes’.

addingshapetostencil

You can also edit a Stencil, while a drawing is open, using Shapes window (you can search for shape using Search control there). All you need to do is click on Stencil Icon (Icon left to your Stencil name).

editingstencil

When you are in edit mode you can move the shapes across Stencils without any intervention. Once done click on the – ‘Save’ button to the right or one that appears in the menu. There are few shapes I frequently use, you can download them here. To download additional stencils you can visit this excellent site. If you are an UML guy definitely check this out.

There are various color themes Visio provides to you. You can get access to it as shown below.

colorschemes

There is also a feature called Layout Shapes in the Shapes menu & Spell check in Tools menu, you can check them if they are of your interest. Hope above would give you a good head start on Visio. Happy Drawing 🙂 .