Sunday, July 5, 2009

Learning UML with C#

UML is a simple diagramming style that was developed from work done by Grady Booch, James Rumbaugh, and Ivar Jacobson, which resulted in a merging of ideas into a single specification and, eventually, a standard.

Here , We will see how to map a class and its relation in UML.

Basic UML diagrams consist of boxes representing classes. Let’s consider
the following class (which has very little actual function).

Basic Class :

public class Person {
private string name;
private int age;
//-----
public Person(string nm, int ag) {
name = nm;
age = ag;
}
public string makeJob() {
return "hired";
}
public int getAge() {
return age;
}
public void splitNames() {
}
}

We can represent this class in UML, as shown in Figure



The top part of the box contains the class name and package name (if any).
The second compartment lists the class’s variables, and the bottom compartment lists its methods. The symbols in front of the names indicate that member's visibility, where "+" means public, "-" means private, and "#" means protected. Static methods are shown underlined.

Abstract methods may be shown in italics or in an “{abstract}” label.

UML does not require that you show all of the attributes of a class, and it
is usual only to show the ones of interest to the discussion at hand.




Inheritance:

Now, we will look into inheritance and how to implement it in UML.

Let’s consider a version of Person that has public, protected, and private variables and methods, and an Emplo yee class derived from it.

public abstract class Person {
protected string name;
private int age;
//-----
public Person(string nm, int ag) {
name = nm;
age = ag;
}
public string makeJob() {
return "hired";
}
public int getAge() {
return age;
}
public void splitNames() {
}
public abstract string getJob(); //must override
}

We now derive the Employee class from it, and fill in some code for the getJob method.

public class Employee : Person {
public Employee(string nm, int ag):base(nm, ag){
}
public override string getJob() {
return "Worker";
}
}

You represent inheritance using a solid line and a hollow triangular arrow.
For the simple Employee class that is a subclass of Person, we represent this in UML, as shown in Figure



Note that the name of the Employee class is not in italics because it is now a concrete class and because it includes a concrete method for the formerly abstract getJob method.

1 comment:

treat jobs said...

Very nice one.


Thank You.