Six principles of design patterns (6) -- six principles of open and closed Design Patterns

Source: Internet
Author: User

Six principles of design patterns (6) -- six principles of open and closed Design Patterns

Definition:

The objects (classes, modules, functions, etc.) in the software should be open to extensions, but the modifications should be closed.

Personal Understanding:

In general, the open/closed principle refers to various components contained in the software system, such as Modules, Classes, and Functions. Instead of modifying the existing code, introduce new features. In the open/closed principle, "open" refers to the feature extension that is open to components and allows Function Extension. In the principle of "open/closed ", the modification to the original code is closed, that is, the original code should not be modified.

Problem:

During the software life cycle, some unmodified code may be modified due to changes in requirements, maintenance, and upgrades, in this case, you need to test the unmodified parts.

Solution:

When the software needs to change, try to modify by extending the original entity instead of modifying the original code. That is to say, use abstraction to build the framework and implement extension details.

In fact, the entire definition of the open and closed principle is vague. It only describes how the objects in the software are opened for extensions, but not modified or disabled. The principle of opening and closing is an abstract summary of the first five principles. The first five principles are specific implementations of the principle. Therefore, if the principle of opening and closing is used, it is actually a bit false because it does not have a fixed pattern, however, the ultimate guarantee is to improve the reusability and maintainability of the program.

In connection with the SOLID (single responsibility principle, open and closed principle, the Lee replacement principle, the interface isolation principle and the dependency inversion principle) in the aforementioned Design Pattern principle, the six principles of design patterns, excluding the principle of open and closed design patterns, are implemented, and the principle of open and closed is also completed. However, it should be clear that the implementation of these principles is not true or false, but the implementation program. The top five principles of the design pattern described earlier have the following functions:

  • The single responsibility principle tells us that the class responsibilities should be single;
  • The Lee's replacement principle tells us not to break down the inheritance of classes;
  • The dependency inversion principle tells us to use interfaces;
  • The interface isolation principle tells us that the interface should be as simple as possible;
  • The dimit law tells us that the coupling between classes should be minimized.

Finally, I will explain how to follow these six principles. The adherence to these six principles is not a question of "yes" or "no", but a question of "more" or "less". That is to say, we generally do not mean whether or not we comply with the six principles, but the degree of compliance. Everything is too late. The same is true for the six design principles of the design model. The purpose of formulating these six principles is not to strictly abide by them, but to use them flexibly according to the actual situation. The degree of compliance with them is well designed as long as they are within a reasonable scope.


AS3 briefly introduces the concepts of upward and downward Conversion

Go to Baidu to check the Li's replacement principle. This is one of the six major design principles of the design model.

Single responsibility, Lishi replacement (replacement), dependency inversion, interface isolation, meter rule, and open/closed Principle

Or simply put, you need to understand the unification of as3.0, the two branches, and event-driven

In a unified manner, all display objects belong to the flash. display. DisplayObject class.
It is divided into two major branches.
The first branch is flash. display. InteractiveObject (Interactive object) and non-interactive object.
The second branch is flash. display. DisplayObjectContainer (container object) and non-container object.

The use of up-to-Down conversion and Down conversion is common when using events.

For example

Mc. addEventListener (MouseEvent. CLICK, onClick_func );

Function onClick_func (_ evt: MouseEvent): void {
Var _ mc: MovieClip = _evt.tar get as MovieClip;
// In this event processing function, _evt.tar get as MovieClip uses the downward conversion to transfer
// Convert the flash. display. DisplayObject type to its subclass flash. display. MovieClip
}

Up Conversion

For example

Function setX (_ obj: DisplayObject): void {
_ Obj. x = 100;
}

The parameter of this function uses the DisplayObject of the parent class as the type, which indicates that the parameter passed in by calling this function can be a subclass of all displayobjects, such as MovieClip, SimpleButton, such as TextField (text box ).

The parent class of the subclass is applied based on the upward or downward conversion of interfaces.



What is the principle of opening/closing

The "open-closed" principle refers to the "open to expansion and closed to modification" principle. It refers to the software design, when a software entity is extended, try not to modify the original software entity. In other words, it is extended without modifying the original software entity. This software design method is actually a kind of goal, it takes into account the software maintenance and other factors. If a software system can achieve this goal, it is scalable and easy to maintain. In software design, many design patterns are applied to achieve this goal.

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.