Open Closure principle OCP

Source: Internet
Author: User

The Open closure principle (Ocp,open Closed Principle) is the core of all object-oriented principles. The goal of software design itself is to package change and reduce coupling, and the open closure principle is the most direct embodiment of this goal. Other design principles, many times, serve to achieve this goal.

On the principle of open closure, the core idea is that software entities should be extensible and non-modifiable. In other words, the extension is open, and the modification is closed. Therefore, the open closure principle is mainly embodied in two aspects: open to expansion, meaning that there are new requirements or changes, the existing code can be extended to adapt to the new situation. Enclosing a modification means that once the class is designed, it can do its work independently, rather than making any modifications to the class. "Demand always changes", "No software in the world is constant", these statements are the most classical expression of software requirements. A key point of transmission is that for software designers, it is necessary to achieve flexible system expansion without the need to modify the original system. And how can this be done? Only dependent on abstraction. The core idea of open closure is to abstract programming, not specific programming, because the abstraction is relatively stable. Let the class depend on the fixed abstraction, so the modification is closed, but through the object-oriented inheritance and the polymorphism mechanism, can realize the inheritance of the abstract body, by covering its methods to change the inherent behavior, to implement a new extension method, so for the extension is open. This is the basic idea of implementing the principle of open closure, which is based on two basic design principles, which is the principle of Liskov substitution and synthesis/aggregation reuse. For classes that violate this principle, refactoring is required to improve, and the design patterns commonly used to implement are the template method mode and the strategy mode. Package change is an important means to realize this principle, and encapsulate the state of change frequently as a class.

Open Closure principle OCP

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.