Wednesday, June 15, 2011

Umple tutorial 3: Mixins

This is the third series of Umple tutorial posts on my blog. The first two were about 1) basic attributes and associations, and 2) state machines. In this post I introduce the concept of Umple mixins.

A mixin, a concept popularized in the Ada language, but now available also in languages such as Ruby, allows bits of code to be incorporated into other programming elements.

Let us imagine the following Umple code that you put in file1.ump:

// Contents of file1.ump

class X {
  lazy name;

class Y {
  Integer id;

This could be a small self-contained system with two unconnected classes. However let us imagine that you also created the following Umple code in file2.ump:

// Contents of file2.ump

use file1.ump;

class X {
   Date dueDate;

association {1 X -- * Y;}

If you compile file1.ump and file2.ump together, the result is that class X now contains two attributes, name and id.  Furthermore there is also an association between classes X and Y. The second declaration is class X is not redefining the class, it is adding to the class.

It wasn't necessary to put the above code snippets in separate files. Any time Umple encounters class X { ... } it will incrementally add the contents of the curly brackets to class X.

This can be useful to allow you to keep the 'pure' class diagram part of the model separate from the methods that belong in each class. In Umple you always have a choice: keep these elements together, or keep them separate.

Another use of mixins is to create various versions of a product line that have different features. So for example if we have the following code:

// Contents of file3.ump

class X {
   Time dueTime;

association {0..1 X -- 1..* Y;}

One product could be built using file1.ump + file2.ump and another, slightly different product could be built using file1.ump + file3.ump.

Incidentally. This is not the only way to create variants in Umple. Another concept is VML4Umple. I will provide a tutorial for that at a later date. For the eager, you can look at Jenya Levin's masters thesis.

Note that class X is declared to be a singleton. This means that code will be generated following the standard "Gang of Four" singleton pattern. Umple has several features that help implement patterns, and more are planned as Umple is developed further.

1 comment:

  1. I didn't know you also have a blog. This is so cool! I write about Java and my own research as well. I also notice UMPLE has a facebook too. Have you ever used NetworkedBlogs? you can actually link your blog to the UMPLE page on facebook using that.

    - Ali