Object-oriented encapsulation of change, or more accurately, should be encapsulated in the same place, leaving a change in the place can be changed when needed, then where will change?
1. Changes in data
For example, a factory produces a paper box, program to calculate its volume, need to have a long, wide, high size, the size of the box is fixed, then in the code directly hard coding, such as Long 1, Width 2, high 3, method to return 1*2*3, or even directly return 6, without any problems. Now demand has changed, the factory produces two sizes of boxes, the other a long 2 width 2 High 2, this time the change is data. Use variables to protect against data changes. I now only in the calculation of the volume of the method to set the length and width of three parameters, in the method to return a long * wide * Gao can be. What is not changed here is the process of calculating the volume, the variable of the width and height, and the change is the data used in the calculation.
2. Process change
Now the manufacturers have produced another triangular prism of the bottom surface of the box, the original calculation of the volume of the formula is not used. Notice here that the process of calculating the volume is needed, but how the process is implemented needs to change. Use inheritance and overrides to protect against process changes. You can turn the method of calculating volume into a virtual method, and then rewrite it in the inherited class, returning a length of * * * * high/2. What remains unchanged here is that the behavior of the volume is necessarily calculated, and the process of the behavior is varied, and the data values required by the behavior are changed.
3, parameter changes
Manufacturers produce a third product, the bottom area is round, cylindrical box. The parameters required at this time are not the length and width, but the radius and the height of two variables. This time the method of calculating the volume can not be used to pass the original three parameters. Use attributes to protect against changes in parameters. At this time we are in the abstract parent class as long as the calculation volume of the parameter-free method, and then in the subclass to customize different attributes. For example, in the Cuboid box subclasses define the length of height, in the cylindrical box subclasses define the radius and height. Wait a minute.
4. Increased behavior
Now there is a second factory to find us to do the program, they calculate volume in addition to the size of the box after the need to add a package volume. Then the third plant needs to be in the volume with a 1.05 material consumption factor. Although it can also be used to protect against change, but it is not a pure calculation of the volume of the box. and a variety of manufacturers behave strangely and unpredictable behavior changes. Use events to ward off an increase in behavior. The method of calculating the volume of a box raises an event that calculates the volume of the box, allowing the person handling the event to know the parameters of the calculation and the results of the calculation, and to change it. Then, in order to do the program for the second to third factory, you can handle the new behavior in the event of calculating the volume of the box. What remains unchanged here is the need to compute the volume of this behavior, and the change is that there will be many additional behaviors after this behavior, and it is unknown.
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.