************Preface:
Start with a question: "What is a wildcard "? Solution: generics are type abstract classes. What is the role: "Through abstraction and encapsulation of types, We can greatly reduce the amount of code ".
From the beginning, the word "abstract" is constantly repeated in the ear. However, before you start a project, especially before you try to use object-oriented thinking to create a project, "abstraction" is always a word in your mind. In a specific project, how do we make the courseware Abstract Step by Step? -- Inheritance, generic ......
************The relationship between "generic" and Abstraction:
- Figure 1: Dal layer class diagram when no frame is used
Note:
① Same: This layer class graph has a lot of repeated code, and the methods in the class have 90% similarity.
② Different: the input and returned objects of each class are different.
- Figure 2: Dal layer class diagram using ooframework (as shown in figure 3)
Note:
Abstract Public methods into the repositorybasedal class and provide an interface externally. Each specific class has its own interface. In this example, It is iuserdal (which is also an externally exposed Interface), which is used to expand the methods belonging to the userdal class. Common methods are inherited from the repositorybasedal class.
The parameter is T and tcollection, which is the main character "generic" today ".
This policy solves the problems in Figure 1. It also paves the way for the framework.
- Figure 3: Structural Diagram of the Dal Layer
************Self-solution:
At the beginning, I did not understand generics. You cannot understand the meaning of "type abstraction". However, after a project experience, you can now get a general understanding of "generic ". In fact, "generics" play the same role as abstract classes and abstract methods, except that they are used to abstract the things in the brackets behind classes or methods.
************Benefits:
① Provide services for different objects: with generics, the same code serves different objects.
② Better Abstraction: Without generics, we can only abstract some classes and abstract methods. But with "generics", we can abstract both input parameters and returned values.
③ Reduce code duplication: abstraction can reduce a large amount of code. "generic" is a small part of abstraction, so "generic" must be learned.
④ The Only Way to object orientation: the three elements of object orientation are based on abstraction, while "generic" is a small part of abstraction. Therefore, "generic" is the only way to object orientation.
⑤ The beginning of architecture: to do a good job of architecture, object-oriented must be mastered, and "generic" is an important indicator of measuring whether or not object-oriented.
************Summary:
I once demonstrated the architecture of the system in "Data room charging system cooperation edition (I) -- framework setup", but the teacher wrote in his comment:"
In this system, you cannot see that you are using generics !" At that time, I didn't quite understand why the teacher said that I didn't use generics. I did not know why I had forgotten to draw generics in the framework diagram after I returned this summary. Maybe I just forgot it at the time, but now it seems that I did not forget it at the beginning, but I did not understand generics at all. Correct the blog image here:
Computer Room fee system cooperation edition (4)-generic solution