Object-Oriented design principles

Source: Internet
Author: User

Seven design principles of object-oriented

1. Opening and closing principle

2, the Richter replacement principle

3. Single Duty principle

4. Interface Isolation principle

5. Dependency Inversion principle

6. Dimitri Principle

7. Combination

/

The principle of aggregation multiplexing

Knowledge Point Correlation

Learning object-oriented design patterns,

is the key to the object-oriented thinking,

Through the subtle case of the Master class,

We can

Broaden your awareness.

Before we learn the seven principles of object-oriented design, we should have enough knowledge of the basic encapsulation, inheritance, and polymorphic ideas,

It is also necessary to have sufficient coding ability for abstract classes and interfaces, because the design pattern is a comprehensive application of the above knowledge points.

Other than that

Before touching the specific design pattern,

The seven-object-oriented design principles will let you know that

The design pattern appears

Inevitability and meaning.

1.

The exact meaning of each design idea is as follows:

First, the overall understanding of the seven design ideas.

First, the opening and shutting principle:

This one is in the first place to understand,

It's meant to be

Open to expansion,

Close for modification

Explain that,

We

The written code cannot be modified because of changes in demand. We can address the changing needs in a way that adds code.

Of course, this is an ideal state, in reality, we should try to narrow this change.

To explain the meaning of this principle,

We use the reverse thinking way to think.

If every change in demand goes

Modify the original code,

The original code has the risk of being modified,

Of course there are intentional and unintentional modifications,

will lead to the risk of failure of the original functioning function,

This is likely to unfold a terrible butterfly effect,

Increase the maintenance effort.

In the final analysis, the open and closed principle in addition to the surface of the strong scalability, in the enterprise more value is the maintenance costs.

So

The opening and closing principle is the first principle of design pattern, its subtext is: control the risk of change of demand, reduce maintenance cost.

There are several principles that serve this principle.

Second, the Richter replacement option:

The implication of this principle is that a subclass can replace its parent class anywhere. Explain that this is polymorphic premise that I

Many of the so-called flexibility behind them are changes in requirements that change the instantiation class to complete without changing the declaration type. Of course

Inherited traits seem natural enough to satisfy this condition.

But this is more about inherited application issues,

We must ensure that our subclasses and parent classes are accurate.

The subtext of the Richter replacement principle is: Try to use precise abstract classes or interfaces as much as possible.

Iii. single principle of responsibility:

The meaning of a single duty is:

Class has a single responsibility and causes the class to change for a single reason. Explain, this is also the premise of flexibility,

If we split the class into the smallest functional unit,

It's a lot easier to combine and reuse,

If a class does too much, in combination, it will inevitably produce unnecessary methods, which is actually a kind of pollution.

For example, when we draw a pattern, we use "dots" to make up a diagram and a "straight line" to make a diagram, which is more flexible? It must be "point", it can draw any shape, and the straight line can only draw a pattern with straight lines, it is at least unable to draw a circle.

The subtext of a single responsibility is to split to the smallest unit and solve the problem of reuse and composition.

Four, the principle of interface isolation:

The principle of interface isolation can be said to be a necessary means of a single duty,

It means to use a single, functional interface as much as possible.

Instead of using functionally complex, comprehensive interfaces.

Well understood, interfaces are meant to be implemented by subclasses,

If subclasses want to achieve a single function, then the interface must also meet the function of a single.

Instead

If the interface is fused with multiple unrelated methods,

Then its subclasses are forced to implement all the methods,

Although some methods are not available at all. This is the interface pollution.

The subtext of the interface isolation principle is: Split, starting from the interface.

V. The principle of dependency inversion:

To understand the dependency inversion principle,

The traditional solution must be understood first.

The initial program of the face object,

The callee is dependent on the caller.

That is, the caller decides what method the caller has,

What is the way of realization,

This structure will pay a great price when the demand changes, even overturn the rewrite.

The principle of dependency inversion is

Requires both the caller and the callee to rely on the abstract

, so that there is no direct connection and contact between the two, and that the change of one party does not affect the change of the other.

In fact, dependency inversion and the previous principles are mutually reinforcing, emphasizing the importance of abstraction.

The subtext of dependency inversion is: For abstract programming, decoupling calls and callee.

Vi. Principles of Dimitri:

Dimitri Principles

Requires as much encapsulation as possible to be as independent as possible, using a low-level access modifier. This is a typical embodiment of encapsulation characteristics.

If a class exposes too many methods and fields that are private,

will make the caller very confused.

and will cause unnecessary judgment code for the class.

So, we use the lowest possible access modifier,

Let the outside world not know our internal. This is also the basic idea of object-oriented. This is a feature of the Dimitri principle, and it is not possible to understand more private information about classes.

Other than that

The Dimitri principle requires the direct contact between classes as little as possible,

Two classes of access,

Through a third mediation class

To achieve.

The subtext of Dimitri principle is: Do not talk to strangers, something to intermediary.

Vii. Combination

Aggregation multiplexing principle:

The implication of this principle is that if

Just to achieve the purpose of code reuse, use combinations and aggregations as much as possible instead of inheritance. Here we need to explain,

A composite aggregation simply refers to a method of another class, not the inheritance of the referenced class, and the descent is changed.

Inheritance is more coupled, such as a parent class later added to implement an interface or remove an interface, the subclass can be

Can be destroyed by a devastating compilation error,

But if it's just a combination of aggregates, just a method of referencing the class,

There is no such a huge risk, but it also enables reuse.

The subtext of the composite aggregation multiplexing principle is: I'm just using your method, we're not necessarily the same.

Object-Oriented design principles

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.