Easy introduction to UML (4)-OCP is a progressive Cook, uml-ocp

Source: Internet
Author: User

Easy introduction to UML (4)-OCP is a progressive Cook, uml-ocp
In the previous chapter, we discussed the SRP principle of one of the five features of object-oriented design (OOD. In this section, we will look at another important principle: OCP principle (Open-Close principle ).
Software entity shocould be open for extension, but closed for modification
Review in the previous section: 1. Easy introduction to UML (1)-class diagram (Appendix: java Comparable interface and Comparator Interface Usage)
Http://blog.csdn.net/woyunowuyuda/article/details/38981647 2. easy getting started with UML (2)-master Junit and make our development more efficient
Http://blog.csdn.net/woyunowuyuda/article/details/39029689 3. Easy introduction to UML (3)-SRP good cook, let others Programming
Http://blog.csdn.net/woyunowuyuda/article/details/39032305
In the previous section, we said that a cook must meet the SRP principle and do his job well. we can implement it through inheritance and interface. But now there is another new problem: the cook had to study Hunan cuisine after studying Sichuan cuisine in school and finding a job in Hunan. Learn to learn, who knows that the top boss is a Cantonese student and has to learn how to cook Cantonese food. For the Cooker class defined in the previous section, add a method today and a method tomorrow. The day after tomorrow, find that a method is not practical and delete it. In this way, the program looks messy. It is not only inefficient but also prone to errors. To solve these problems, we think of the OCP principle, that isA software entity (class, module, function, etc.) should be open for extension and closed for modification.. How can we understand this sentence? Let's take the most common example: almost everyone has a mobile phone, either Android or apple. You can install new software without restrictions (as long as your memory is large enough ). If you want your wallpaper, do you need to modify the android source code? No. You only need to install another wallpaper. This is what we call the OCP principle. It is open for our expansion. However, you only need to change the peripheral environment of the module without changing the module itself. Next we will continue to take the kitchen knife (with respect to the chef industry) and understand the OCP principles at the code layer:


After graduation, only Sichuan food (I'm sorry for using pinyin) can be added directly to this category if he wants to learn Hunan food and Cantonese food? Obviously, this is not conducive to class extension and modification. Therefore, we can define this as follows:


First, we define an interface CookSkill, which contains the cook method. Then, every newly learned cooking technique must implement the CookSkill interface. Therefore, you do not need to change the code of Cooker, CookSkill, or other cuisines during expansion. You only need to add a class. In this way, we have met the OCP principle. Now, the driver's license test is booming, and the chef also got one. Well, I found that I had no car after I got my driver's license, so I had to borrow a friend to open the drive. We can define this as follows:


That is to say, this Cooker references the Firend drive method. However, we found that changing the Firend class will affect the Cooker class more or less (maybe a parameter, maybe a name or a return value ), at this time, we say that the coupling between the two classes is too high. Does not comply with the OCP principles. At this point, we can modify it as follows:


At this time, we found that the association between the Fiend class and the Cooker class is not so close, and we can also use a unit test to implement the DriveSkill interface to complete the Cooker test, instead of being bound by the Firend class. In this way, we have met the OCP's principle of being closed for modification. We can summarize:1. When should we consider the OCP principle?A. When your module has been extended, you should consider the OCP principle to provide new functions.B. When the coupling between the two classes is too high, the OCP principle should be taken into account to ensure the stability of important modules.2. How to meet OCP principlesThe OCP principles can be met by using abstraction such as interfaces. In terms of function expansion, define an interface to accept new functions. In terms of module protection, define an interface to reduce coupling.

3. Relationship between OCP principles and other principlesThe principle of openness and closure is idealistic and is the ultimate goal of object-oriented design. Therefore, the implementation methods of the open and closed principles have always been subject to the painstaking efforts of the object-oriented design masters to study the implementation methods of the open and closed principles. LSP, DIP, ISP, Abstract Class, And Interace, can be seen as the implementation method of the open and closed principle.

The graduation project is a uml-based educational administration system, which is now in the webpage design stage.

You must be a dynamic educational administration system. What else should you do? Html javascript and jsp must be used. The only thing that deserves discussion is UML-based. Why is it based on UML? UML is a set of design diagram specifications.

How to create a management system

What kind of system do you want to do?
 

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.