OCP and ocp

Source: Internet
Author: User

OCP and ocp
Open and closed Principle

Turn: http://baike.baidu.com/view/2493421.htm
Turn: http://dev.csdn.net/article/38/38826.shtm
OCP (Open Closed Principle) is the core of all object-oriented principles. The goal of software design itself is to encapsulate changes and reduce coupling. The principle of openness and closure is the most direct embodiment of this goal. Other Design Principles often serve to achieve this goal.
The core idea of the closed development principle is:
The software entity should be extensible and unchangeable. That is to say, it is open to extensions and closed to modifications.
Therefore, the principle of openness and closure is mainly reflected in two aspects:
Open to expansion means that existing code can be expanded to adapt to new situations when there are new requirements or changes.
Closed modification means that once the class is designed, its work can be completed independently without any modification to the class.
"Demand is always changing" and "no software in the world remains the same". These comments are the most classic expressions about software requirements. A key implication is that software designers must implement flexible system expansion without modifying the original system. How can we achieve this?
Only dependent on abstraction. The core idea of implementing open and closed programming is to abstract programming, rather than specific programming, because abstraction is relatively stable. Classes depend on fixed abstractions, so modifications are closed. Through Object-oriented Inheritance and polymorphism mechanisms, classes can inherit abstract bodies, by overwriting its methods to change the inherent behavior and implement new expansion methods, it is open to expansion. This is the basic idea of implementing the open and closed principle. At the same time, this mechanism is based on two basic design principles. This is the Liskov replacement principle and the synthesis/aggregation Reuse Principle. These two principles.
Classes that violate this principle must be reconstructed for improvement. The design patterns commonly used for implementation mainly include the Template Method mode and the Strategy Mode. Encapsulation of changes is an important means to achieve this principle. It encapsulates the changing state into a class.

Synthesis/aggregation Reuse Principle (CARP)Use merging/aggregation as much as possible, and do not use inheritance as much as possible
Definition: Use existing objects in a new object to make them a part of the new object. New objects reuse these objects by delegating them.
Added by me: NotePrinciples of merging/aggregation multiplexingAbstract is also required! For example, implementing dynamic function changes!
LSP Definition
If there is a class C object c1 for each type of P object p1, so that in all program P for C, after p1 is replaced with c1, if the behavior function of program P remains unchanged, C is the child type of program P.
The LSP principle clearly states that the IS-A relationship in OOD is in terms of behavioral functions. Behavior is not internal, private, but external and public, and is dependent on the customer program. Behavior is a concern of the software! The behavior functions of all derived classes must be consistent with those expected by the client program for their base classes.

Related Article

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.