System Architecture Design--learning the design of the article (UML)

Source: Internet
Author: User

Overview

"Programming is a technology, more is an art, not only satisfied with the completion of the code to run the results of the correct long time, often consider how to make the code more concise, easier to maintain, easy to expand and take, only in this way can really be improved." It's really cool to write elegant code. UML class diagram is not a learning will be, need to have a slowly skilled process. The so-called learning, in fact, this (class design) is to understand the beginning of object-oriented. "--Big Talk design mode

For beginners, we should not only learn about object-oriented encapsulation, inheritance and polymorphism, but we should also apply these ideas to our code based on our understanding. After I've studied Java for a long time, I seldom really think about the relationships between classes and classes in addition to inheritance, implementation, and affiliation (inclusion). And when I run into problems, think about how to change the design of a class. So before we start the whole project, we should try to think about how each class should be designed, how classes and classes should handle their relationships, so that we don't have to have problems and rethink when we're working on patterns and business design. This is also a necessary condition for implementing code reuse to achieve low coupling with high cohesion.

Let's start with the UML class diagram and learn about the design of the class.

1. Classes (Class)

In my opinion, a class can be seen as a unit of object-oriented programming, the smallest element of encapsulation, inheritance, and polymorphism. In object-oriented thinking, all things are objects.
In UML, the class diagram is divided into three layers, the first layer shows the name of the class, and if it is an abstract class, it is displayed in italics. The second layer is the attribute of the class, usually the fields and attributes. The third layer is the operation of the class, usually the method and behavior.
where "+" means public, "-" means private, "#" means protected.

2. Inheritance (Extend)

This is one of the three main features of object-oriented programming. Represented in UML using triangles and solid lines.

code is represented as

public class Bird extend Animal{    ...}
3. Implementation (implements)

Java interface is the declaration of a series of methods, is a collection of methods features, an interface only the methods of the characteristics of the method is not implemented, so these methods can be implemented in different places by different classes, and these implementations can have different behavior (function).

code is represented as

publicclass Bird extend Animal implements Flyable{    publicvoidfly(){        ...    }}publicinterface Flyable{    publicabstractvoidfly();}
4. Dependency (Dependency)

Dependencies can be understood in terms of the necessary conditions, simple to understand is the relationship of dependence , but not interdependence. In general, this relationship is expressed in the constructor method, as a parameter passed in. This is to say that if there are no parameters, then there is no way to construct a dependency relationship.

code is represented as

publicclass Animal{    privatenull;    privatenull;    publicAnimal(Oxygen o,Water w){        this.oxygen = o;        this.water = w;    }}
5. Combination (composition)

A composite relationship is also a strong "owning" relationship, and unlike dependencies, a component in a composition can be created by itself. In the preceding example, the two of the dependencies are actually independent, but one side relies on the other. and The combination of the two sides, is one side of the other, embodies the strict part and the whole relationship, part and the whole life cycle is consistent .

code is represented as

publicclass Bird{    private Wing wing;    publicBird(){        new Wing();    }}publicclass Wing{    ...}
6. Aggregation (Aggregation)

Aggregation represents a weak "owning" relationship, which reflects that a object can contain a B object, but the B object is not part of a object. The difference is that the life cycle of the components that make up the relationship is the same as the whole, and if the bird dies, then his wings will not be used (if you think the wings of the dead bird can be transplanted to another bird to continue to use it, then the bird and the wing are actually the aggregation relationship, The specific explanation will be discussed later). The whole and part of the aggregation relationship is relatively weakened, as if the birds were made of birds, and if there were no flocks, birds or birds.

code is represented as

publicclass Bery{    private Bird[] arrayBirds;    publicabstractvoidSouthwardMigration();    ...}
7. Association (Association)

A join between a class and a class that enables a class to know the properties and methods of another class. For example, if a depends on B, then B is represented as a global variable. Association relationships have two-way associations and one-way associations. Bidirectional affinity: Two classes are aware of the public properties and operations of another class. One-way Association: Only one class knows the public properties and operations of another class. Most associations should be unidirectional, one-way relationships easier to establish and maintain, and help find classes that can be taken. Association relationships are among the weakest relationships in all relationships.

code is represented as

publicclass Bird{    private Climate climate;}publicclass Climate{    private String season;    ...}
8. Relationships between associations, aggregations, combinations, dependencies

Their respective definitions have been said before, here just to say a question, is that the relationship between the four are actually relative, here to make a metaphor for the relationship between men and women, compared to everyone can better understand:

  1. Male and female relations at the beginning, the two sides just know, this time either the man is interested in the woman, or the woman is interested in the man, or both sides have goodwill, then the relationship between the two sides is related to the relationship, but simply to pay attention to each other. This is the relationship, only one party knows the other.
  2. Generally speaking, we have more than one friend of the opposite sex, even if the man is interesting to the woman, the woman is also a man of many heterosexual friends, and the woman. These heterosexual friends are actually an aggregation. Therefore, if you stand in the woman's perspective, she and the man is related to the relationship, if standing in the opposite sex friends, the woman is just one of the male heterosexual friends, the woman and the opposite sex friend is the aggregation relationship. This also shows that the relationship is actually relative.
  3. Next, the two sides ended up in love, fell in Iove, and finally walked into the halls of marriage, forming their families. At this point in the family's perspective, they and the family is a combination of relations. It is worth noting that if the family breaks up, the role of the husband and wife will not exist. Strictly embodies the whole and part of the relationship, the life cycle is the same.
  4. Family maintenance is a source of income, in general, the man is the pillar of the family, then the relationship between the family is dependent on the man. And the family is broken, the man has the same financial resources, so the life cycle of the two is not the same.

At this point, we will go to the next stage of learning-design mode.

Note : Reprint please indicate, turn from itboy-wood grass.
respect for originality and respect for technology.

System Architecture Design--learning the design of the article (UML)

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.