I have written many articles on object-oriented design principles, but in my opinion, some principles of object-oriented design are correct, but not refined.
Most of the principles of orientation can be derived from three support points: Ensure orthogonal, control level, and information hiding.
In this article, we will discuss how to ensure the orthogonal nature.
Abstraction is the starting point of design, and abstract results can be a specific concept or logic. Orthogonal is associated with abstract results. To understand the orthogonal nature, let's take a look at the geometric interpretation of the word:
When the two straight lines are perpendicular to each other, we think the two straight lines are orthogonal. Otherwise, the two straight lines are not orthogonal.
This seems to be unrelated to the software.
But if we assume that the intersection is not two straight lines but two Cylindrical columns, we can see the difference between orthogonal and non-orthogonal. In orthogonal conditions, the maximum contact area of the two cylinder is always equal to the area of the cylindrical section. However, in non-orthogonal conditions, the contact area must be greater than the area of the cylindrical section, the larger the inclination, the larger the contact area.
If the two columns are wood, the larger the contact area, the larger the construction volume, the worse the replacement of wood.
The concept or logical relationship is orthogonal or not, and the effect is similar to the above.
Suppose we define two classes. XMLReader is responsible for reading the nodes in the XML file, and XMLDataHandler is responsible for processing the data read from the XML file. At this time, if the XMLDataHandler shows a method to read XML content based on XPath, the two classes will undoubtedly become non-orthogonal. Because reading this function exists in XMLReader and XMLDataHandler. In this case, the two locations are coupled with the XML structure. If the XML structure changes, both of these locations need to be changed.
Let us assume that we need to check the data integrity when reading XML and implement the xmlDataVerify () method. If () the data integrity check is also carried out in the method, so the two methods are not orthogonal. Because the validation rules must exist at the same time. Once the inspection rules change, they must be modified at the same time.
These kinds of non-orthogonal situations are sometimes called coupling and sometimes inadequate abstraction. However, the fundamental problem lies in the non-orthogonal concept or logic.
There are many non-orthogonal situations, but in summary, these situations can be roughly divided into two categories, which are related to the possible basic relationship between software concepts.
If we want to classify the basic relationships between concepts in software, we can roughly divide them into two categories: one is to define a hierarchical relationship, and the operations of different parts are actually overlapping, however, the specific degree is different. We call this relationship horizontal split. Second, we define the relationship between each other, that is, what you do, what I do, and what we call this relationship vertical split.
The concept of "layer" is generated by horizontal segmentation.
Typical examples include the OSI network model and the Windows GDI design. Here we will explain the design of Windows's GDI:
Windows has always stressed the concept of WYSIWYG, which means that the content displayed by the user on the screen should be consistent with the content displayed on the printer.
If applications (such as Word) are directly related to the characteristics of the monitor and even the printer, it is almost impossible to achieve this goal. To solve this problem, the method used in Windows is to set up a new level between a specific device and an application, that is, the new level is GDI.
In this case, the GDI and Driver layers do the same thing in essence: Describe the specified content to the specified page. However, the specific description method is different. GDI pays less attention to the device features (or only the common features of the device), while the driver pays attention to the unique properties of the device.
In essence, many design techniques add more layers to the software structure. As we often say, the Proxy and Facade modes implement the Open-Closed principle (OCP) principle.
Vertical segmentation generates modules or objects. The classic example is the MVC mode. Model, View, and Controller are actually different concepts, but they are related to each other. Therefore, these three relatively independent concepts need to be connected through a certain link.
During horizontal segmentation, the non-orthogonal representation is inconsistent at the abstract level. For example, there are still many things that must be done at the Driver level; During vertical segmentation, non-orthogonal representation is the existence of overlapping areas, for example, directly processing data in the View.
The concept or logic is only associated at the points that must be associated.
Full abstraction, the final result is usually the concept or logic of orthogonal, while the concept or logic of orthogonal is mostly the basis for coping with changes, testing, and reducing coupling.
Associated articles:
Design core task 2: Information Hiding
One of the core tasks of design: hierarchical control