OO design principles-Summary of OO design principles and design processes

Source: Internet
Author: User

I have published five articles on OO design principles. Here I will summarize how these five principles are applied in our design process,

This is a summary of my reading and learning many blog posts and materials. It is for your reference only.

I. Oo (Object-Oriented) Design Basics

Object-oriented (OO): it is based on the object concept, object-centric, class and inheritance as the construction mechanism, making full use of interfaces and Polymorphism to provide flexibility,

To understand, understand, and describe the objective world and design, and build corresponding software systems.

Object-oriented features: Although various object-oriented programming languages are different from each other, we can see their support for the basic features of object-oriented programming,

That is, "abstraction, encapsulation, inheritance, and polymorphism ":

-Abstraction. do not consider details first.

-Encapsulation to hide internal implementations

-Inheritance: reuse existing code

-Polymorphism: rewrite object behavior

Object-Oriented Design Model: It is a "good object-oriented design". The so-called "Good object-oriented design" is something that can meet the needs of "responding to changes,

Improve reuse. Object-Oriented Design Patterns Describe software design, so they are independent of programming languages,

The final implementation of the Object design pattern still needs to be expressed in the object-oriented programming language. The object-oriented design mode is not like algorithm skills,

It can be copied and used. It is based on an empirical understanding of "Object-Oriented.

The above section describes the concepts and relationships of object-oriented and design patterns. During our design, we fully understand and

The four basic features of OO are used for design. Therefore, you must be familiar with and master the object-oriented technology before design,

I will not introduce it in detail here, but for the design pattern, it provides us with a reference model for the design, while mastering the Object-Oriented Design Pattern

The premise is to first master the "Object-Oriented" technology.

Ii. Oo (Object-Oriented) design goals

ScalabilityExtensibility: with new requirements, new performance can be easily added to the system without affecting the existing performance or bringing new defects.

ModifiableFlexibility: When the code of a system is to be modified, the existing structure of the system is not damaged, and other components are not affected.

OptionalPluggability: You can replace some code in the system with other classes of the same interface without affecting the system.

 

Iii. Five Principles of OO design and Their Relationships 3.1 Summary of OO design principles

The five principles of OO design are described in detail in the previous article,

I will not explain it in detail here. The following is a brief summary.

※Single Responsibility Principle: For a class, there should be only one reason for its change.

A single class is an excellent design. The ambiguous responsibilities will make the Code look awkward,

There is a risk of aesthetic and ugly system errors.

※Open and closed principle: software entities (classes, modules, functions, and so on) should be scalable but unchangeable.

The core idea of implementing the open and closed principle is to abstract programming, rather than specific programming, because the abstraction is relatively stable.

Make classes dependent on fixed abstractions, so modifications are closed. Through Object-oriented Inheritance and polymorphism mechanisms,

In addition, it can inherit abstract classes by overwriting their methods to change inherent behaviors and implement new expansion methods, so they are open.

"Demand is always changing" without changing software, so we need to use the closed open principle to close changes to meet our needs,

At the same time, it can also maintain the stability of the software's internal packaging system, without being affected by changes in demand.

※Dependency inversion principle: dependency abstraction. Do not depend on specifics.

Image extraction stability determines the system stability, because abstraction remains unchanged and relies on abstraction as the essence of object-oriented design,

It is also the core of the principle of dependency inversion. Dependency on abstraction is a general principle, while in some cases, it is inevitable to follow the details of the limit,

The trade-off between abstraction and concrete must be weighed. methods are not the same layer. The dependency on abstraction means programming interfaces rather than implementing programming.

※Lee's replacement principle: child types must be replaced with their parent types.

The liskov replacement principle focuses on the Inheritance of abstraction and polymorphism. Therefore, only the liskov replacement principle is followed,

In order to ensure that inheritance reuse is reliable. The implementation method is interface-Oriented Programming: abstract the common part as a class interface or abstract class,

Extractabstractclass is used to override the parent class in the subclass to implement the same responsibilities in the new method. The liskov replacement principle can

This ensures that the system has good scalability and implements a multi-state abstract mechanism to reduce code redundancy and avoid type discrimination during runtime.

※Interface isolation principle: Multiple customer-related interfaces are better than one common interface.

There are two main means of separation: 1. Delegate separation, by adding a new type to delegate the customer's request, isolate the direct dependency between the customer and the interface,

But it will increase the system overhead. 2. Multi-inheritance separation: it is better to implement Customer requirements through multi-inheritance through interfaces.

 

Below are the two principles that have not been mentioned before, and they are also important principles to be considered during design.

※Dimit rule: do not directly interact with other classes that do not communicate with each other.

If the two classes do not need to communicate with each other directly, the two classes should not interact directly. If a class needs to be called

A method can forward the call through a third party. The premise of the dimit rule is that in the design of classes, each class should be as much as possible.

Reduce the access permissions of members. Its fundamental idea is to emphasize the loose coupling between classes.

※Merging/aggregation Reuse Principle: Use merging/aggregation whenever possible, and do not use inheritance whenever possible.

Both composition and aggregation are special types of association. Aggregation indicates a weak relationship of ownership;

Merging is a strong relationship of ownership, reflecting the strict relationship between the part and the whole, and the same as the overall life cycle.

The preferential use of the merging or aggregation principle will help to keep each class encapsulated and concentrated on a single task. This class and class inheritance

Hierarchies will maintain a small pattern and are unlikely to grow into an uncontrollable giant.

3.2 Relationship Between OO design principles

1. The key step for implementing the "open-close" principle is abstraction. The inheritance relationship between the base class and the subclass is an abstract embodiment.

Therefore, the Li's replacement principle is a standard for specific steps to achieve abstraction.

Violation of the Li's replacement principle means violation of the "open-close" principle, and vice versa.

2. The "Open-Close" principle is related to the dependency reversal principle as the goal and means. If the principle of open/closed is the goal and the principle of dependency reversal

Is a means to reach the "open and closed" principle. If we want to achieve the best "open and closed" principle, we should try our best to abide by the dependency reversal principle,

The principle of dependency inversion is the best criterion for "abstraction.

3. The Li's replacement principle is the basis of the reversal principle, which is an important supplement to the Li's replacement principle.

4. The interface separation principle is also an important means to ensure the "On-Off" principle.

5. For a single responsibility principle, I personally think it is better to do it as much as possible. The more simple the responsibility is, the easier it is to implement the "open-close" and "LI Shi" replacement.

Iv. Relationship between OO design principles and objectives

1. extensibility: allows a new class with the same interface to replace the old class and reuse the abstract interface.

The client relies on abstract interfaces rather than a specific implementation class, so that this class can be replaced by another specific class,

Without affecting the client. The following principles Enable Scalability.

※On/off Principle

※Lee's replacement principle

※Dependency reversal Principle

※Principles of synthesis/aggregation multiplexing

2. modifyable flexibility: the module is relatively independent and the communication is as few as possible. In this way, when a module is modified, it has little impact on other modules.

The following principles can be modified.

※On/off Principle

※Dimit Law

※Interface isolation principle

3. Alternative pluggability: When a part no longer meets the requirements, you can pull out the old part and insert the new part.

The following principles can be used as a replacement.

※On/off Principle

※Lee's replacement principle

※Dependency reversal Principle

※Principles of synthesis/aggregation multiplexing

V. Oo (Object-Oriented) design process 1. Analysis style and function classification. 2. Class abstraction based on functions.

※Class abstraction-in this step, we can perform class abstraction based on the "single responsibility principle.

The class features are single and clear as much as possible.

※Encapsulate change points-use encapsulation to create the demarcation layer between objects, so that the designer can modify it on one side of the demarcation layer,

Without adverse effects on the other side, so as to achieve loose coupling between layers.

3. Design Abstract base classes and interface classes.

※The basic class abstraction and interface definition must follow the "interface separation principle" to abstract interfaces.

※Do not always pay attention to details when designing interfaces and basic classes. Remember to program interfaces rather than implementations.

※The abstract base class and the derived class must meet the requirements of the "Lee's replacement principle.

4. Determine the coupling relationship between classes. 4.1 what is the basis for determining the degree of coupling?

※Simply put, the coupling degree is determined based on the stability of the requirement.

※For high stability requirements that are not easy to change, we can design various types into tightly coupled ones,

This can improve efficiency, and we can also use some better technologies to improve efficiency or simplify code.

※If the demand is very likely to change, we need to fully consider the coupling problem between classes. We can come up with various

There are various ways to reduce coupling, but to sum up, there is nothing more than adding abstract layers to isolate different classes,

This abstraction level can be an abstract class, a specific class, an interface, or a group of classes.

We can sum up the idea of reducing Coupling Degree in one sentence: "programming for interfaces, rather than programming for implementations.

※When determining the coupling relationship of classes, we recommend that you consider the "dimit rule" and "merging/aggregation Reuse Principle ".

4.2 how to reverse the dependency?

※Abstract coupling is the key to dependency inversion. Abstract Coupling Relationships always involve the inheritance of specific classes from abstract classes,

In addition, it is necessary to ensure that the class can be changed to its subclass in any reference to the base class. Therefore, the Li's replacement principle is the basis of the dependency reversal principle.

※Dependent on Abstraction: We recommend that you do not rely on a specific class, that is, all dependencies in the program should end with abstract classes or interfaces. Try:

(1) No variable should hold a pointer or reference pointing to a specific class.

(2) No class should be derived from a specific class.

(3) No method should overwrite any implemented methods in its base class.

5. Use the Five Principles of OO design to further optimize the design.

※Does the class abstraction and functions meet the "single Responsibility Principle"

※Whether the inheritance relationship and base class references meet the requirements of the "Li's replacement principle" and "Dependency inversion principle"

※Whether the interface and the base class are "interface isolation principles"

※Whether the "open-close principle" is met in general"

 

In general, the five principles of design should be fully considered in object-oriented design, but they are not mandatory.

As a result, the performance and resource consumption of the designed system can be analyzed and designed according to the specific situation.

 

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.