Classes to code

BLOG IX.

Classes to code, before starting to code, first it is better for you to draw a diagram which helps you to define clearly what do you have to develop, specification, fulfill user’s needs. As soon as you have all the diagrams needed, it will facilitate your work of coding.

Now let’s talk about how to draw a diagram, you can visit one of my posts to understand about UML:
https://wordpress.com/read/feeds/99170391/posts/2428155733
https://wordpress.com/read/feeds/99170391/posts/2446328640

Once you understand all the concepts, let’s start to convert classes to code. For example:

Let’s take a look at this UML:
1. The upper part of the table goes the name of the class, first letter is in Uppercase.

2. The middle part of the table contains attributes of the class, then type of the attributes.

3. The lower part of the tables includes all the method of the class, after the name of the method you have to specify «()» such as howTo().

4. There are some signs, it refers to the access modifier, it goes before the name of the attributes.

  • public = «+»
  • package = «~»
  • protected = «#»
  • private= «-«

5. There are many relation between class to class:

  • Unidirectional one-to-one association:

In the example below, each Account is associated with exactly 1 Advertiser, and the only traversal of the link is from the Advertiser to the associated Account. In this case the Account is created by the Advertiser constructor, and the Advertiser retains a link to the Account, but not vice-versa:

  • Bidirectional one-to-one association:

As above, but now there is a link from the Account to the Advertiser as well as the other way around. The constructor for the Account is still called as a byproduct of constructing an Advertiser, but now it contains a link back to the Advertiser with which it is associated:

  • One-to-many association:

If the Advertiser can have multiple accounts, then the Advertiser needs to have some sort of collection ( Set, List, HashMap, etc. ) to refer to the Account(s). The Account is no longer created by the Advertiser constructor, but rather must be created separately and added to the Advertiser as a separate step. Note that the code below has a problem if Advertiser.addAccount( ) is called before Account.setOwer( ).

  • Many-to-many association:

Now both ends of the association need to use collections, with appropriate checks to maintain the consistency of the associations, and to prevent circular recursion.

Now you have some knowledge of how to convert UML to code, i hope it will help you so you can convert it easily.

Dejar un comentario

Diseña un sitio como este con WordPress.com
Comenzar