"Open-close" principle (open-closed Principle) _ Design mode

Source: Internet
Author: User
1.1 "open-closed" principle (open-closed principle) 1.1.1 function

It instructs us to improve the scalability of our code! 1.1.2 Definition

(1) Open (open for extension)

The behavior of the module must be open, extensible, and not rigid.

(2) Closed (Closed for modification)

When you extend the functionality of a module, you should not affect or massively affect existing program modules.

In other words, it requires developers to be able to modify the existing functional code (source code or binary code, the binary executable version of the module, whether it is a linked library, DLL, or java. jar file, is implemented to extend the software functionality of the application system.

A software entity should be open to the extension and closed for modification. In other words, when designing a module, it should be allowed to be extended without modification. In other words, you should be able to change the behavior of the module without having to modify the source code. This principle is in fact "the principle of the closure of variability": Find a system of variable factors, packaging it. This principle is ambiguous to two points:

1 A variability should not be scattered in many corners of the code, but should be encapsulated into an object. The different representations of the same variability are ambiguous to the specific subclasses of the same inheritance hierarchy. Inheritance is seen as a way of encapsulating changes, and should not be considered as a way of generating special objects from generic objects.

2 One variability should not be mixed with another variability. (The inheritance structure of all class diagrams is generally not more than two layers, otherwise it means to mix two different variables together.) )

This principle is the general principle, and several others are the means and tools of this principle.

Key points of opening and closing principles:

1 Any system will change in its life cycle

2 change should be caused by adding new code instead of modifying the code that is already working

3 The key to OCP is abstraction, which should only be abstracted from those parts of the program that show frequent changes

4 The relationship between the interface and the customer is closer than the class and the customer that implements it

5 The Strategy pattern and template pattern are the most common methods to meet the OCP.

6 There is no model that is appropriate for all cases, and it is impossible to completely close, so you should anticipate the most likely types of change

7 reject immature abstraction as important as abstraction itself

8 from another point of view, is the so-called "on the principle of variability packaging." "The principle of variability encapsulation" means that a variability should not be scattered in many corners of the code, but should be encapsulated within an object, and the same variability of different representations implies a specific subclass of the same inheritance hierarchy; a variability should not be mixed with another variability, The inheritance structure of class diagram should not be more than two-layer 1.1.3 analysis of opening and closing principle

Abstraction is the key to the principle of opening and closing, and constructs abstract isolation changes. Most of the design patterns are in accordance with the open and closed principle, in each model to evaluate the advantages and disadvantages of the open and closed principle as an important evaluation basis to determine whether the system based on this model has good flexibility and scalability.

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.