Software Architecture Design Principles

Source: Internet
Author: User

1: single responsibility principle (SRP ):

An excellent System Design emphasizes the relationship between modules with low coupling and high cohesion. In object-oriented design, this rule is also applicable. Therefore, the first design principle of object-oriented design is: single Responsibility Principle (SRP, single responsibility principle ).

A single responsibility emphasizes the separation of duties. To some extent, understanding of responsibilities constitutes the key to designing the coupling relationship between different classes, therefore, the single responsibility principle is more or less a fundamental principle that must be considered in the design process.

The core idea of a single responsibility principle is:

One class is better to do only one thing. There is only one reason for its change.

The single responsibility principle can be seen as the extension of low coupling and high cohesion in the object-oriented principle. The responsibility is defined as the cause of change to improve cohesion to reduce the cause of change. Too many responsibilities may lead to more reasons for its changes. This will lead to dependency between duties and affect each other, greatly damaging their cohesion and coupling. A single responsibility usually means a single function. Therefore, do not implement too many function points for the class to ensure that the entity has only one reason for its change.

Therefore, the core of the SRP principle is to require only one change to the class. classes that violate this principle should be reconstructed, for example, separating duties in the fa-ade mode or proxy mode, use Basic methods such as extract interface, extract class, and extract method.

2: Open-Close principle (OCP)

In any case, the open and closed principle (OCP, open closed principle) is the core of all object-oriented principles. The goal of software design itself is to encapsulate changes and reduce coupling. The principle of openness and closure is the most direct embodiment of this goal. Other design principles are often used to achieve this goal. For example, to achieve the optimal and correct hierarchy of inheritance with the liskov replacement principle, we can ensure that the open and closed principles will not be violated.

The core idea of the closed development principle is:

The software entity should be extensible and unchangeable. That is to say, it is open to extensions and closed to modifications.

Therefore, the principle of openness and closure is mainly reflected in two aspects:

Open to expansion means that when there are new demands or changesCodeTo adapt to new situations.

Closed modification means that once the class is designed, its work can be completed independently without any modification to the class.

"Demand is always changing" and "no software in the world remains the same". These comments are the most classic expressions about software requirements. A key implication is that software designers must implement flexible system expansion without modifying the original system. How can we achieve this?

Only dependent on abstraction. The core idea of implementing open and closed programming is to abstract programming, rather than specific programming, because abstraction is relatively stable. Classes depend on fixed abstractions, so modifications are closed. Through Object-oriented Inheritance and polymorphism mechanisms, classes can inherit abstract bodies, by overwriting its methods to change the inherent behavior and implement new expansion methods, it is open to expansion. This is the basic idea of implementing the open and closed principle. At the same time, this mechanism is based on two basic design principles. This is the liskov replacement principle and the synthesis/aggregation Reuse Principle. We have discussed these two principles in other parts of this book and will discuss them in depth in the application reflection section.

Classes that violate this principle must be reconstructed for improvement. The design patterns commonly used for implementation mainly include the template method mode and the Strategy Mode. Encapsulation of changes is an important means to achieve this principle. It encapsulates the changing state into a class.

3: LSP)

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.