Wednesday, November 24, 2010

Umple tutorial 1: Basic attributes and associations


From time to time, my posts will include short tutorials about how to use aspects of Umple. This is the first such tutorial. For an overview of Umple, see my recent post on this blog, my recent post on Jordi Cabot's blog, or go to the Umple home page.

In the  Umple below:
  • Lines 3, 4 and 5: These define Umple/UML attributes, not Java (or PHP or Ruby) member/instance variables. Since they are attributes, the generated system will have private variables, get methods and set methods for each. In a later post, I will discuss other keywords you can use with attributes, e.g. to make them immutable. I will also show how to inject different code into the generated functions, so there is never any need to edit generated code itself.
  • Lines 3 and 4: These show how Umple defaults to using String as the data type for attributes. The reason we did this is to allow for quick modeling with little need to initially worry about the type of an attribute. Sensible code can still be generated.
  • Line 5: Integer is an Umple data type. When generating Java, Umple will use the type int. However, Umple can also generate PHP and Ruby, which use dynamic typing.
  • Line 6: This shows a many-to-many association with the class Address. This association is bidirectional meaning that if you have an Address, you can find all the Students who live there . Several students can share an address, and a student may have several addresses on file. A student must have at least one address. Umple supports all possible UML multiplicities, and maintains referential integrity if associations are bidirectional, like this one is.
  • Lines 8 to 11: These constitute a normal Java method. This is not further processed by Umple; when you generate a system, these lines are passed through unchanged. Without these four lines, the code would be ‘pure model’, and code could be generated from the file in Java, Ruby and PHP. But with these lines, the developer has committed to using Java as the base language. It it possible instead to keep all base language code in separate file(s), therefore separating pure model from base language methods. You could even have separate files for Java methods and equivalent PHP and Ruby methods. This would allow you to generate equivalent final systems in all three base languages.
  • Line 16: This shows that attributes can be many-valued.
1  class Student
2  {
3     firstName;
4     lastName;
5     Integer number;
6     * -- 1..* Address;
8     public String fullName()
9     {
10       return getFirstName() + " " + getLastName();
11    }
12 }
13
14 class Address
15 {
16    String[] line;
17 }

Copy and paste the above into UmpleOnline and generate Java code to see what happens. Or put it into a file StudentAndAddress.ump, and compile it on the command line or in Eclipse.

How would you use such a file in your development? Most likely, you would create methods in various Java classes that use the generated Java API shown below. Some of your methods could be injected into classes Address and Student, using Umple’s mixin mechanism.

Constructor of Student
  public Student(String aFirstName, String aLastName, int aNumber, Address... allAddresses)

Methods of class Student
  public boolean setFirstName(String aFirstName)
  public boolean setLastName(String aLastName)
 
  public boolean setNumber(int aNumber)
  public int getNumber()

  public String getFirstName()
  public String getLastName()

  public Address getAddress(int index)
  public List<Address> getAddresses()
  public int numberOfAddresses()
  public boolean hasAddresses()
  public int indexOfAddress(Address aAddress)
  public boolean isNumberOfAddressesValid()
  public static int minimumNumberOfAddresses()
  public boolean addAddress(Address aAddress)
  public boolean removeAddress(Address aAddress)
  public boolean setAddresses(Address... newAddresses)

  public void delete()

  public String fullName()

Consructor of Address
  public Address()

Methods of class Address
  public boolean addLine(String aLine)
  public boolean removeLine(String aLine)
  public String getLine(int index)
  public String[] getLine()
  public int numberOfLine()
  public boolean hasLine()
  public int indexOfLine(String aLine)

  public Student getStudent(int index)
  public List<Student> getStudents()
  public int numberOfStudents()
  public boolean hasStudents()
  public int indexOfStudent(Student aStudent)
  public static int minimumNumberOfStudents()
  public boolean addStudent(Student aStudent)
  public boolean removeStudent(Student aStudent)

  public void delete()

1 comment:

  1. This is a nice article..
    Its very easy to understand ..
    And this article is using to learn something about it..

    c#, dot.net, php tutorial

    Thanks a lot..!

    ReplyDelete