C + + Design mode 2 (object oriented design principle)

Source: Internet
Author: User
Tags what interface

1. Change is the natural enemy of reuse!

The biggest advantage of object-oriented design is that it resists change

2. Re-understanding Object-oriented

Understanding Isolation Changes:

From the macro level, the object-oriented approach is more adaptable to software changes.

Can minimize the impact of changes.

Perform their duties

At the microscopic level, the object-oriented approach emphasizes the "responsibility" of each class.

(In the code example, each type of graphic, each implementing its own draw)

New types due to changes in demand should not affect the implementation of the original type-accountability.

What is the object?

From the language implementation level, the object is encapsulated with code and data.

From a specification level, an object is a series of public interfaces that can be used.

From a conceptual level, an object is an abstraction of some kind of responsibility.

3. Object-oriented design principles ( Principles are more important than specific models )

The pattern cannot violate the principle, the code calls the process, the technique far has no design principle important.

1) dependent lead principle (DIP)

High-level modules (stable) should not rely on the underlying modules (changes), both of which should be dependent on abstraction (stability).

Abstraction (stability) should not depend on implementation details (change), implementation details should depend on abstraction (stability)

Corresponds to the code example:

The High-level module (mainform) should not be dependent on the underlying module (LINE,REC), both of which should be dependent on the abstract (Shape)

Implementation details (LINE,REC) should be dependent on abstraction. Isolate the Change (LINE,REC)

2) Open closure principle (OCP)

Open to extensions, closed for changes.

The class module should be extensible, but not modifiable.

Corresponds to the code example:

There are changes in the first design method (52 rows, 89 rows, 120 rows);

The second design method is to increase the expansion, do not modify the source code.

3) single duty principle (SRP)

A class should have only one reason to cause it to change.

The direction of change implies the responsibility of the class.

See also follow-up bridge mode, decorator mode

4) Liskov replacement principle (LSP)

Subclasses must be able to replace their base class (IS-A)

Inherits the expression type abstraction.

5) Interface Isolation principle (ISP)

Client programs should not be forced to rely on methods that they do not use.

The interface should be small and complete.

6) Prioritize the use of object combinations rather than class inheritance

Class inheritance is usually "white box reuse", and the object combination is usually "black-box reuse."

Inheritance to some extent destroys the encapsulation, the subclass of the parent class coupling degree is high.

The combination of objects only requires a well-defined interface for the combined object, with a low degree of coupling.

7) Package Change Point

Use encapsulation to create a boundary layer between objects so that the designer can modify it on one side of the boundary layer,

It does not adversely affect the other side, thus achieving loose coupling between layers.

8) programming for interfaces, not for implementation

Instead of declaring a variable type as a specific class, you declare it as an interface.

The client does not need to know the exact type of the object, only the interface that the object has.

Reduce the dependence of the parts of the system, so as to achieve "high cohesion, loose coupling" type design scheme.

Corresponds to the code example:

Vector<line>, Vector<rec> is declared as a specific class, the client program needs to know the specific object;

Vector<shape*> is declared as an interface, the client needs only to know what interface (Shape) it has.

The principle of dependency inversion is to look at problems from different angles and complement each other.

C + + Design mode 2 (object oriented design principle)

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.