See also lightening talk: simple design
A frequently encountered dialog is: "Why is a class introduced here? The string is good, simple design "or" a switch/case should be done. What should I do with a subclass ?"
The fact is usually the opposite,Introducing a new type or subclass is easier than using a string or switch/case.. Let me explain it.
A simple design criterion is whether it is easier to understand. our understanding of things is built on concepts or models. for example, to create a simple supermarket package storage system, you can store packages and retrieve packages,No further requirements.The concept involved may include a package, a locker, and a credential for getting the package. In this case, an empty class is introduced in my design.Public class package {}To represent the package, another empty classPublic class ticket {}To represent the creden. You told me that strings should be used because they are simpler and there will be no new requirements. I disagree. Let's look at the two designs.CodeFor example, saving a package:
- Public ticket put (package ){...}
- Public String put (string package ){...}
Use:
- New Cabinet (). Put (New Package ());
- New Cabinet (). Put ("Some package ");
Even if there is no need for expansion, even if the first design has two more empty classes than the second design, the first design is simpler than the second one because it is easier to understand.. It is consistent with the concept and vocabulary of saving packets in my head and does not need to be translated. when using a string, I have to translate it into a package and a credential after seeing the code. No one even explained this code to me. I still don't know whether to translate it, it hinders my understanding, so it is more complicated. this is the first angle
Ubiquitous language
The second aspect is communication issues. when describing the requirements of the storage package system, we will inevitably mention the concept of packages and creden. tester will certainly use such words to write test cases, but it will suddenly disappear from the code, there are only strings left. You tell me this is a simple design, and you are a confusing design.