From UML modeling language to C # code

Source: Internet
Author: User
From UML modeling language to C # code

[Translation] vczhgame 2004-02-01

<Short comment>
The youngest article on this site was published! Vczhgame is the youngest member of the team, this year, the second year of high school, the overseas cooking complex, the snail bait room, the rabbit, the rabbit, the rhyme, the fierce pen, the sentence, the cockroach, the oysters, the fading U, the grass, the especially good, the yellow grass the primary hacker, the frog, and STC contributed to more articles on Object-Oriented Technology and game development. I hope you will give us special encouragement and support! Here, the team members wish vczhgame students learning and learning progress, and they will be admitted to the ideal university.
Congratulations to all members of Computer Science and Technology Network

Preface

In this article, I will tell you how to transform UML into C # code.
First, I will describe a case related to UML. Now, we focus on how to turn UML into C # code without describing how to create a UML chart. This is why we can see the UML charts in the design and analysis phase from the very beginning.

Case Description

A company wants to improve their information system. The first step is to analyze the staff training procedures and automate some tasks.

  1. When an employee applies for training from the Training Officer, a training procedure begins. The Training Officer will decide whether to accept the employee's training application.
  2. If the Training Officer accepts the employee's training application, the Training Officer will find the appropriate training program in the currently available training program. Once the employee decides the training program she wants, the Training Officer will register with the instructor.
  3. If an employee stops training, the employee must notify the Training Officer in the shortest time to cancel the registration.
  4. After the training, the employee must submit a self-evaluation report and a registration form to the training officer.
  5. The Training Officer checks the form before submitting it to the note service.

Note:UML in Practice, p. Roques, Wiley, 2004Case studies

Main Method

We will use the following UML chart to explain:

Class Diagram
Class diagrams are used to define the code framework and can be said to be the declaration of the entire class.
In the beginning, we can say something like:

  • A uml class diagram will become a C # class
  • The attribute of the UML class diagram will be the variable of the C # class instance.
  • The operation method of the class graph will be the method of the C # class.

You will notice that the navigable role is implemented as an instance variable, just like an attribute, but it is not a simple type, but a user-defined type. This implies the creation of a class.

I will give a simple illustration to illustrate these methods.

Figure A framework code of an order class

Interaction Diagram
The interaction diagram illustrates the operation process of the class method, especially the call sequence of the associated methods.
The following figure shows the relationship between the message serial number and the Code implemented by the C # method.

Figure BEnregistreremprunteur (register borrower)Method subject

After these examples, I will give some actual examples.
From UML class diagram to C # framework code

Let's take a look at Figure C. Pay attention to the "Training Application" class.

Figure C internal relationship of Training Application

The preceding method is enough to convert this chart into C # framework code. The only thing we need to remember is that we must add an import statement to ensure connection with classes in other packages (whether user-defined packages or standard base class libraries ).
The equivalent C # framework code is shown in Figure D.

Figure D: Training Application Class C # framework code

If we want to add a read/write operation for an attribute, we naturally need to add an attribute for the class according to the encapsulation class method, just like the following code:

Public int propdatevalidite {
Get {return datevalidite ;} // This is the "read" Operation of the propdatevalidite attribute
Set {datevalidite = value ;} // This is the "write" Operation of the propdatevalidite attribute
}

Now let's take a look at Figure E and pay attention to the "training" class in it.

Figure E: Relationship between class composition and them

Compared with the previous example, some additional problems occur:
• Generalization of the Relationship Between Elements
• Diversity:Theme<1 .. *> andSession<0 .. * >>{ ordered}
The preceding method is no longer applicable. We have seen how to use <1> (or <0 .. 1>) to achieve navigable connections. However, how can we convert a link to the multiple <*>?
There is a fairly simple principle: Replace the reference to a single object with a type with a specific attribute (such as an object reference set) to achieve <*> multiple.
It is difficult to correctly select the set type provided by. NET Framework. Although it is easy to use C # To create manager arrays, this is not necessary for a correct solution. In general, people prefer to use a collection type like C #'s arraylist or hashtable.
• If you want to remember the order between objects and use an integer index to retrieve objects, select arraylist;
• If you want to get an object with any keyword, you can use hashtable or sortedlist.
Here are some solutions for different needs:

Figure F: transformation from UML to C #

For the formation class, we will use:
• An arraylist is used to organize sessions.
• A hashtable is used to organize theme, rather than a simple array. We use the outline name as a qualified word.

With the preceding instructions, we can write the following code:

Figure G: C # framework code of the formation class

Conclusion

Converting a UML chart into code is a must for standardization and sensitivity. Therefore, for some languages, the converted code may conflict with the UML chart. The code generation tool can automatically perform UML and code conversion, but it also provides different options (such as arraylist and hashtable) to the object editor ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.