problem:
In an object-oriented system, these objects must be able to send messages or operations to other objects, which is the interaction and responsibility assignment of the object. However, due to the interaction of the object and the assignment of responsibilities, there is no fixed standard, so its design quality has a large variability, poor system design can lead to systematic and components fragile and difficult to maintain, reuse and expand. In order to make your system design, it is necessary to have a design principle to establish an assignment of interaction and responsibilities, which is Grasp (General Responsibility Assignment Software Pattern)
What is duty
Responsibilities: Simple saying is the function of each object to be completed, maybe some people will say some ways to object, and right. However, there is also a different responsibility, such as "access database", is not a way to complete, and there must be multiple classes and methods to cooperate, and some simple responsibilities can be done, such as "calculation 1 1 ". It can be seen that the responsibility and method is still different, and the responsibility is to be implemented. Implementation responsibilities can be done by a separate method, but if the complex duties are complex, it must be completed by multiple methods and objects.
How to allocate responsibilities
How to allocate duties is a very vague topic, everyone has their own experience, but there is still a certain rule to find. If you use it, you can not only avoid risks, but also enhance the quality of the software. There are five most basic methods in these regularities, that is, five modes:
1 Expert Mode (Expert)
2 Creator Mode (Creator)
3 high degree or high cohesion
4 low coupling or low coupling (Low Coupling)
5 Controller mode (Controller)
There are other modes, but these are the foundation. To be skilled, it must be gripped on the duty of the object.
The following is an analysis of five modes:
Expert mode: It is assigned to allocate the responsibility in the class that meets the information that must know all the responsibilities. Maybe everyone is not very clear about this, one of the following examples is an explanation of it. We need to calculate the total price of the goods when buying goods, the total price of the calculation item is a responsibility. If you have object-oriented analysis, you can analyze two objects (here, there are some problems j, just for Explanation issues), Sale and Product, the total price of the calculation item is a responsibility of Total. Its necessary information has the price of Product and Product, and there is no quantity in the product, while the number of information in Sale is input. Information from the product, you can get the price of the product, so according to the expert model, put Total in Sale (nor do you know if you understand, anyway, I am clearly j).
Creator Mode: The main solution is the creation of class instances and the assignment of the object instance to create tasks, that is, who is responsible for creating an instance of a class. When you find that the following conditions are met, you can use B object to create a object:
1 b gathers a object
2 B contains a object
3 B records a object instance
4 b To use A objects often
For example, there may be multiple Products in Sale above, and it is better to create a product by sale.
High polymerization and low coupling are familiar, that is, the object must remain independent, do not rely on other objects, while each duties must have a high degree of consolidation, but these two principles must be in use and other principles Team, you can't think about it.
The last thing to say is that the controller mode is simple to say who is responsible for the system event. People who first contact UML may not know very well, first understand the system events, system events can imagine high-level events initiated by the external participants, a better way to use the system sequence map (SEQUENCE) to find it Its the initiator is the user's user. For example, the salesperson of a sales office presses EndSale, and Endsale is a system event. The controller mode is to assign the responsibilities of the system event message to the class representing the following, often use the following: 1 represents the entire system
2 represents a class of some partial features
3 Represents a class of a role (role controller class), the system's participant
However, you should pay attention to the system's interface object does not handle system events. In fact, such requirements are also in line with the three-story development principles (separation of the Brower layer and business logic layer), in other words, the system operation reflects that the business logic should be logical Treatment, rather than by the interface layer of the system.
These five models are general duties allocation, I think it is the foundation base, you can create a system's interactive diagram to implement system design.