Object-Oriented Design Principles and object-oriented design principles

Source: Internet
Author: User

Object-Oriented Design Principles and object-oriented design principles

Seven object-oriented design principles

 

1. Open and Close principles

 

2. Lee's replacement principle

 

3. Single Responsibility Principle

 

4. Interface isolation principle

 

5. Dependency inversion principle

 

6. dimit principles

 

7. Combination

/

Principle of aggregation Reuse

 

Knowledge Point Association

 

Learn the object-oriented design model,

Is the Key to in-depth Object-oriented Thinking,

Through a master-level case study,

We can

Broaden your understanding.

 

Before learning the seven principles of object-oriented design, we must have a sufficient understanding of basic encapsulation, inheritance, and polymorphism ideas,

You must have sufficient coding capabilities for abstract classes and interfaces, because the design pattern is a comprehensive application of the above knowledge points.

 

In addition,

Before getting started with specific design patterns,

Seven object-oriented design principles will let you know,

The design pattern appears

Inevitability and significance.

 

 

1,

The precise meaning of each design concept is as follows:

 

First, we need to understand these seven design ideas.

 

I. Principle of opening/closing:

This article is first understood,

It means

Open to expansion,

Disable Modification

.

The explanation is,

We

The written code cannot be modified as the requirement changes. We can solve the changing needs by adding code.

 

Of course, this is an ideal state. In reality, we should try to narrow down such changes as much as possible.

 

Let's explain the significance of this principle,

We adopt reverse thinking.

If each demand change goes

Modify the original code,

The original code may be modified incorrectly,

Of course, either intentionally or unintentionally modified,

This will cause the risk of failure of the original normal function,

This will probably show a terrible butterfly effect,

Greatly increased maintenance work.

In the end, in addition to the obvious scalability, the principle of open and closed is more important to enterprises in terms of maintenance costs.

So,

The principle of opening/closing is the first principle of the design model. Its subline is to control the risk of demand changes and reduce maintenance costs.

 

The following principles serve these principles.

 

Ii. Lee's replacement options:

 

This principle means that a subclass can replace its parent class anywhere. This is a prerequisite for polymorphism.

Many of the so-called flexibility behind them are the requirement changes that need to be completed by changing the instantiation class without changing the Declaration type. Of course,

The inherited features seem to naturally meet this condition.

But here we pay more attention to the application of inheritance,

We must ensure that our subclass and parent class are accurate.

 

The hidden line of the lean replacement principle is: use precise abstract classes or interfaces as much as possible.

 

Iii. Single responsibility principle:

The meaning of a single responsibility is:

Class has a single responsibility, and the reasons for class changes are single. This is also a flexible premise,

If we split the class into the smallest functional unit,

The combination and reuse are much simpler,

If a class does too many things, there will inevitably be unnecessary methods in combination, which is actually a pollution.

 

For example, when we draw a pattern, we use "points" to form a graph and "straight lines" to form a graph. Which one is more flexible? It must be a "point". It can draw any image, while a straight line can only draw a pattern with a straight line. It cannot draw a circle at least.

 

The sub-lines of a single responsibility are: split to the smallest unit to solve reuse and combination problems.

 

Iv. Interface isolation principles:

 

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

Its meaning is to try to use a single function interface,

Instead of using complex and comprehensive interfaces.

It is easy to understand that the interface is implemented for sub-classes,

If sub-classes want to achieve a single function, the interface must also meet the single function.

 

On the contrary,

If the interface integrates multiple irrelevant methods,

Then its subclass is forced to implement all methods,

Although some methods are useless at all. This is interface pollution.

 

The sub-lines of the interface isolation principle are: Split, starting from the interface.

 

V. Dependency inversion principle:

 

To understand the Dependency inversion principle,

You must first understand the traditional solution.

The initial program of the Surface object,

The caller depends on the caller.

That is, the caller determines the method of the caller,

What implementation methods are available,

This kind of structure will pay a lot of price when it needs to be changed, and even overwrite it.

 

The principle of dependency inversion is

Both the caller and the called are required to rely on abstraction.

In this way, there is no direct association or contact between the two parties. changes made by one party will not affect changes made by the other party.

 

In fact, dependency inversion and the previous principles complement each other and both emphasize the importance of abstraction.

 

The Hidden Lines of dependency inversion are: Abstract programming, decoupling calls, and callers.

 

Vi. dimit principles:

 

Dumit principles

Encapsulation is required as much as possible, as independent as possible, and low-level access modifiers are used as much as possible. This is a typical embodiment of encapsulation features.

 

If a class exposes too many private methods and fields,

It will make the caller very confused.

And it will cause unnecessary judgment code for the class.

Therefore, we use the lowest possible access modifier,

Make the outside world unaware of our interior. This is also the basic idea of object-oriented. This is a feature of the dimit principle and cannot understand more private information about the class.

 

In addition,

The dimit principle requires as few direct links between classes as possible,

Two types of access,

Through the third intermediary class

.

 

The hidden line of his principle is: he does not talk to strangers and has something to do with intermediary.

 

VII. Combination

Principles of aggregation reuse:

 

This principle means that if

It only achieves the purpose of code reuse. Try to use combination and aggregation instead of inheritance. Here you need to explain,

Composite aggregation only refers to the methods of other classes, without the inheritance of the referenced classes and changing the lineage.

 

 

The inheritance coupling is greater. For example, if a parent class is added to implement an interface or removes an interface, the subclass can

Can be damaged by compilation errors,

However, if it is only a combination of aggregation, it is just a method that references the class,

There will be no such huge risk, but it also achieves reuse.

 

The hidden line of the principle of combined aggregation reuse is: I just use your method, and we are not necessarily the same.

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.