Six principles of object-oriented design (II)

Source: Internet
Author: User

Ood basically has six principles, but they are actually complementary. That is to say, some principles need to be implemented by others. The six principles are as follows:

1) Open-Close principle (OCP): the principle of open-close is that the design should have good support for expansion, while the modification should be strictly limited. This is the most important and abstract principle. Basically, the reusable software we call is developed based on this principle. Other principles also provide a path for its implementation.

(2) liskov substituition principle (LSP), which is a strict principle. The rule is that "the subclass must be able to replace the base class; otherwise, it should not be designed as its subclass ." That is to say, subclass can only expand the base class, rather than hide or override the base class. If you need to design this, you should refer to the following two methods to replace:

1.

2.

3) dependence inversion principle (DIP) depends on the switching principle. "design depends on abstraction rather than concrete ". In other words, we should use abstraction to think about the design, rather than first dividing what classes I need, because these are specific. What are the benefits of doing so? Human thinking itself is actually very abstract. When we analyze the problem, we do not consider the details at once, but abstract the entire problem, therefore, abstract design is in line with human thinking. In addition, this principle will support OCP very well. Abstract-oriented design makes it possible for us to not rely too much on implementation, so that expansion becomes possible, this principle is also the cornerstone of another article design by contract.

4) interface segregation principle (ISP), "dividing large interfaces into multiple small Interfaces", the benefits of doing so are obvious. I don't know if it is necessary to continue the description, in order to save space, in fact I just made a small summary of these principles, if you need more in-depth understanding of it, it is recommended to see "Java and mode", a masterpiece of MS MVP! Pai_^

5) Composition/aggregation Reuse Principle (CARP), designers should first consider combination/aggregation, rather than inheritance (because it is intuitive, the first impression is "oh, this is oo "). This is the so-called "favor composition over inheritance". In practice, combination/aggregation will bring greater benefits than inheritance, So priority should be given.

6) Law of Demeter or least knowlegde principle (low level or LKP), the Demeter principle or least knowledge principle, which was first formally applied in the Demeter system and therefore defined as the Demeter law. It is about "one object should know as few other objects as possible ". That is, another rule about loosely-coupled.

Well, the above is an introduction to the six principles (or rules). An in-depth study of these principles is just the way to get the design pattern. After a deep understanding, we can start to look at the design patterns. The design patterns are the application of these rules. The famous design patterns include the Gang of Four and gof) in addition, there are many other famous models. You can study them slowly. If you can generate one or two models by yourself, it would be great to prove that you are also a master! Pai_^

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.