Seven Essential object-oriented design principles for programmers (I)

Source: Internet
Author: User
Highlights

I am dealing with object-oriented objects every day. But when we apply object-oriented objects, do we feel that our own object-oriented technology applications are quite reasonable? Is it well understood? Is the application in place? Is it suitable for use? Is it in line with the software development trend?

The above series of problems didn't confuse you! The object-oriented applications I want to talk about also have the object-oriented principle. The programming quality of a programmer can be displayed from dozens of lines of code in a small example. Object-Oriented Programming is the basis of object-oriented programming. If you want to follow the object-oriented programming principles, your programming level will be greatly improved!

Thoughts

As you can imagine, why do you usually write a short program for you during the interview? What is the significance of this? What is the subject's question purpose? How should we display our code quality?

I think we should think about this issue during the interview, because in this way we can better cope with the interview, better show our talent, the company prefers to hire you, it is easy for you to pay higher salaries.

The answer is: a short piece of code shows the quality of our code. It is not very good to go to the interview to implement simple functions, but to present your own programming thinking to others (for the cultivation of programming thinking, please refer:What is the gap between "cainiao" programmers and "Great God" programmers?), And the most important thing is to show the maintainability and reusability of the software's functions, because this programming is the crystallization of your own thinking, and the later maintenance of programming brings great benefits, therefore, maintainability and reusability of software functions are the potential of software development. This aspect of software is the most important!



With these two images, you can recognize their importance, because they are top experts in the industry and their ideas are so high, shouldn't we study the idea of giants? The answer is yes.

Personal Opinion:

Software reuse has many advantages, such as improving software development efficiency, improving software quality, saving development costs, and improving system maintainability.

The purpose of object-oriented design reuse is to enable maintainability reuse.

In the object-oriented design, maintainability reuse is based on the object-oriented design principles. These design principles are the reuse principles first, following these design principles can effectively improve the reusability of the system and the maintainability of the system.

The object-oriented design principles and design patterns are also the compass for the rational reconstruction of the system. refactoring is based on the existing functions of the software, by adjusting the program code to improve the quality and performance of the software, the design mode and architecture of the program become more reasonable, and the scalability and maintainability of the software are improved.


Overview of the seven principles of object-oriented principles:


Note: The importance levels shown in 1, 2, 3, 4, and 5

Common object-oriented design principles include seven. These principles do not exist in isolation. They are mutually dependent and complementary.

The following is a one-to-one analysis of the seven object-oriented Principles 1. Open and closed principles

1. Definition of the open/closed Principle: A software entity should be open to extensions and closed to modifications. That is to say, when designing a module, the module should be extended without being modified, that is, the behavior of the module should be changed without modifying the source code.

2. Analysis of the Principle of opening/closing:

(1) the principle of open/closed was proposed by Bertrand Meyer in 1988. It is one of the most important principles in object-oriented design.

(2) in the definition of the open/closed principle, a software entity can refer to a software module, a local structure composed of multiple classes, or an independent class.

(3) abstraction is the key to the principle of openness and closure.

(4) The principle of openness and closure can also be described by a more specific "encapsulation principle of variability" and the principle of variability encapsulation (principle of encapsulation of variation, EVP) it is required to find and encapsulate the variable factors of the system.

3. Example of the open/closed principle:

A graphic interface system provides buttons of different shapes. The client code can program these buttons. Users may need to use different buttons to change their requirements. The original design scheme is as follows:

Figure (1)

The system is now restructured to meet the requirements of the open and closed principles.


Figure (2)

Comparative Analysis

Figure (1): A client method calls the addition class directly, but I want to add a subtraction class, you will find that you have to change the code in the addition class when adding a subtraction class (implemented using the switch statement), which violates the "Open and Close principle", so we should re-refactor it.

As shown in figure (2), we add a parent class of the operation class in this figure, so that we do not need to modify the client class when adding or subtracting classes.

Summary of the principle of opening and closing: In the face of requirements, changes to the program are made by adding new Code, rather than changing the original code. Next: Seven Essential object-oriented design principles for programmers (2)



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.