Basic Principles of Software Design

Source: Internet
Author: User

(1) dot not repeat yourself
When similar code is found in two or more places, we need to abstract their commonalities to form a unique method, and then change the code in the existing place to make them suitable.
To call this new method.


(2) program to an interface, not an implement
The important idea in the Design Pattern focuses on interfaces rather than implementation, and relies on interfaces rather than implementation.


(3) command-Query
Query: When a method returns a value to respond to a problem, it is the nature of the query.
Command: When a method needs to change the state of an object, it is a command.
When designing an interface, ensure that the interface is single and that the behavior of the method is strict command or query.


(4) principle of least knowledge minimum knowledge Principle
For method M in object o, m can only access the methods of the following objects:
1, object 0.
2. component object directly related to O.
3. The object created or instantiated by method M.
4, as the parameter object of method M.
Final string outputdir = ctxt. getoptions (). getscratchdir (). getabsolutepath ();
Such a long string of details about other objects, detailed details, detailed details ...... Added coupling,
It makes the code structure complex and rigid, and is difficult to expand and maintain.


(5) single responsibility principle single Responsibility Principle
Its core idea is that a class only does one thing and completes it well. 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.
(6) principle of open/close Principle
With regard to the principle of closed development, the core idea is that modules are scalable and cannot be modified. That is to say, it is open to extensions and closed to modifications.
Open to expansion means that existing code can be expanded to adapt to new situations when there are new requirements or changes.
Closed modification means that once the class is designed, its work can be completed independently without any modification to the class.
(7) Dependency inversion principle (DIP)-Dependency inversion principle
High-level modules should not rely on the implementation of low-level modules, but on high-level abstraction.
For example, the wall switch should not depend on the light switch implementation, but on an abstract standard interface of the switch,
When we expand the program, our switch can also control different lights or even different appliances.
It seems that the browser does not depend on the Web server, but only on the HTTP protocol. This principle is too important. The division of labor in society,
Standardization is the embodiment of this design principle.

(8) Hollywood principle-Hollywood principles
The Hollywood principle is one sentence-"Don't call us, we'll call you .". It means Hollywood brokers don't want you to contact them,
Instead, they will contact you as needed. That is to say, all components are passive, and all components are initialized and called by the container.
The component is in a container and managed by the container.
Simply put, it is the relationship between control programs by containers, rather than directly controlling program code in non-traditional implementations. This is the concept of "control reversal:
1. Do not create an object, but describe how to create an object.
2. In the code, objects and services are not directly linked, but containers are responsible for connecting them together.


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: 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.