Sometimes some things can be written into one class or multiple classes. How can we take into account various constraints and find a suitable balance point? For example, if Python's own cgi. escape and HTMLParser. HTMLParser are mixed into Pascal's naming conventions, which of the two functions is encapsulated in this way? Sometimes some things can be written into one class or multiple classes. How can we take into account various constraints and find a suitable balance point?
For example, if Python's own cgi. escape and HTMLParser. HTMLParser are mixed into Pascal's naming conventions, which of the two functions is encapsulated in this way? Reply content:
It is easy to write unittest and never destroys the encapsulation.
Methodology:
- Use case Analysis
- Class Responsibility Collaborator)
Secondly, understand the object-oriented principles
- Abstract principles
- Follow the principles of high cohesion and low coupling
- Follow SOLIDPrinciples, especially the Open/closed principle and the Single responsibility principle)
Finally, we will continue to rebuild experiences.
In the early stage of demand analysis, the requirements and functions are divided into a little more detail. Then, divide the functions to be done into several heaps. A bunch of functions focus on something like database reading, database writing, interface interaction, and data computing.
You see, it's natural that the "class" thing isn't coming out.
Pay attention to two aspects during design.
First, the interface must be isolated. SOLID introduces two principles: single responsibility and interface isolation. A single responsibility is indeed difficult, and sometimes it is more cost-effective to break it. But interface isolation must be achieved. Interfaces are not isolated, so it is impossible to abide by the principles of open and closed, and Lee's principle.
Second, consider the principle of open and closed. The principle of opening/closing is the core idea of all software design. But it is really hard to strictly implement it. First, no one can take into account all possible changes in the beginning. Second, even if they are taken into account, the cost will remain high. Therefore, you can design some open and closed designs based on your experience with business and demand side within the acceptable workload range.
During development, we mainly rely on experience and skills for some refactoring.
One thing we can mention here is that some code specifications that seem boring are sometimes very helpful. For example, a method cannot exceed 30 rows, and a class cannot exceed 300 rows. When the number of rows exceeds the standard, you will naturally want to refactor and split sub-classes.
In addition, @ vczh mentioned that unittest prevails, which is also a good idea, especially in tdd practices. If the object is targeted, according to the object-oriented definition of the class, a group of objects with the same characteristics can be returned to a class, even if multiple classes share a group of attributes, classify a shared property group as an inheritance class, and then inherit the specific class as a subclass to implement the difference.
If you can classify all the categories and classify them into different categories, public attributes should not be properly processed. This issue must be discussed separately.
If the demand changes less frequently, it is based on the convenience of test.
As for the frequent changes in requirements, believe me, as long as you can run.
Therefore, I have always tried my best not to work on the business logic layer. I have always worked hard to write core functions. For example, for a typical back-end mvc project, I generally write m, at most write a little more interface between m and c. But I don't want to change the main components of c and v at all. At this time, I can write classes based on test after writing my interfaces. Just rely on feeling... This is why we need design patterns. You may want to refer to GoF (
"Design Patterns: Elements of Reusable Object-Oriented Software").
==================================
Well, I have no experience in writing large projects. Let's take a look at the answers from the above answers. SOLID (single function, open/closed principle, Lee's replacement, interface isolation, and dependency inversion) based on the business,
If you think about it from the perspective of an object,
It is nothing more than encapsulating concepts,
Concepts can be divided into objects,
Behavior and status,
Of course,
Some others think it is a complete concept,
It can also be divided,
Separate description,
It's a bit like painting. Class to encapsulate changes. Encapsulate the areas that are easy to change according to requirements, which also corresponds to the open and closed principle of the design mode (open to derivation and closed to modification)