Introduction to designpattern_ocp & dip

Source: Internet
Author: User

Open and closed Principle(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.


Core IdeaYes: software entities should be extensible and cannot be modified. That is to say, it is open to extensions and closed to modifications. Therefore, opening up for expansion means that when there are new demands or changesCodeTo adapt to new situations.


Basic Ideas: The core idea of open and closed programming is to abstract programming, rather than specific programming, because the 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.


Rule recommendations

    • The principle of open and closed is the most important design principle. The liskov replacement principle and the synthesis/aggregation Reuse Principle provide a guarantee for the implementation of the principle of open and closed.
    • You can reconstruct the template method mode and Strategy Mode to implement the design idea of closed modification and open extension.
    • Encapsulation changes are an important means to achieve the principle of openness and closure.

      It is encapsulated as an abstract, such as the ibankprocess interface in the banking business.
    • Refuse to abuse abstraction and only abstract the frequently changed parts. This experience can be obtained from the learning and application of design patterns.

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------

Dependency reversal Principle(Dip, depend invert Principle)


Core IdeaYes: It depends on abstraction, not on specifics. Or, you need to program the interface instead of implementing the Program (program to an interface, not an implementation ).


Basic Ideas: When we use interfaces and abstract classes to declare the types of variables, descriptions of return types of methods, and conversion of data types. To ensure this, a specific class should only implement the methods declared in the interfaces and abstract classes, and should not provide redundant methods.

The abstract type of the referenced object.:As long as a referenced object has an abstract type, the abstract type should be used wherever the object is referenced, including parameter type declaration, method return type declaration, and attribute variable type declaration.

Joint UseInterfaces andAbstract class: Because abstract classes have the advantages of providing default implementations, and interfaces have other advantages, it is a good choice to use them together. First, the declaration type is undertaken by the interface, but an abstract class is also provided to provide a default implementation for this interface. Other specific classes of the same abstract type can choose to implement this interface, or inherit from this abstract class. If you need to add a method to the interface, you only need to add the specific implementation of this method to this abstract class at the same time, because the subclass inherited from this abstract class will get this specific method from this abstract class.


Advantage: flexible system expansion. Disadvantage: a large number of classes are required.

 



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.