[Turn] The six principles of design mode--open and close principle (OCP)

Source: Internet
Author: User

Original address: http://www.cnblogs.com/muzongyan/archive/2010/08/05/1793454.html

Open Closed Principle is the most basic design principle in the Java world, which guides how we build a stable and flexible system.

Defined:

A software entity such as classes, modules, and functions should be open to extensions and closed for modification.

Softeware entities like classes,modules and functions should is open for extension but closed for modifications.

The opening and closing principle means that a software entity should be extended to achieve change, rather than modifying existing code to achieve change.

Software entities include the following sections:

    • A module in a project or software product that is broken down by certain logical rules
    • Abstractions and classes
    • Method

The open and closed principle is a principle for the future of the software entity to constrain the current development design.

Note: The opening and closing principle of open, to modify the closed, does not mean that no changes, low-level module changes, must have high-layer modules to be coupled, otherwise is an isolated meaningless code fragment.

Types of Changes:

    • Logical change
    • Sub-module changes
    • Visible trying to change

The basic path of a project should be this: project development, reconstruction, testing, production, operations, the reconstruction of the original design and code can be modified, operations to minimize the original code modification, maintain the purity of historical code, improve the stability of the system.

The importance of opening and closing principle:

    • Influence of opening and closing principle on testing

Open and close principle but keep the original test code still can run normally, we just need to test the extended code.

    • The principle of opening and closing can improve reusability

In object-oriented design, all logic is composed of atomic logic, rather than implementing a business logic independently in a class. Only then can the code be reused, the smaller the granularity, the greater the likelihood of being reused.

    • Open and close principle can improve maintainability
    • Requirements for object-oriented development

How to use the open/closed principle:

    • Abstract constraints

First, through the interface or abstract class constraints extension, the extension is bounded, not allowed to appear in the interface or abstract class does not exist in the public method;

Second, parameter types, reference objects use interfaces or abstract classes as much as possible, rather than implementing classes;

Thirdly, the abstraction layer is kept as stable as possible, and modifications are not allowed once determined.

    • Meta data (metadata) control module behavior

Metadata is the data used to describe the environment and data, in layman's terms, configuration parameters, parameters can be obtained from the file, can also be obtained from the database.

The spring container is a typical example of the behavior of a meta-data control module, where the ultimate is control inversion (inversion of controls)

    • Develop a project charter

In a team, it is very important to establish a project charter, because the regulations specify the conventions that all personnel must abide by, and the agreement is better than the configuration for the project.

    • Package changes

The package of changes contains two meanings:

First, the same changes are encapsulated in an interface or abstract class;

Second, the different changes are encapsulated into different interfaces or abstract classes, and there should be no two different variations in the same interface or abstract class.

[Turn] The six principles of design mode--open and close 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.