When we first came into contact with object-oriented thinking, I think the first concept we came into contact with should be" Class ", We have been discussing profound issues such as how to design classes and how to implement classes, but have we ever thought about what is called" classes "and what is the essence of classes ?. According to the introduction in most object-oriented books, a class is a data structure that encapsulates data and operations. I don't think everyone is satisfied with this answer.
So what is a class? Before discussing this issue, we will first discuss the origins of classes. " Class "The corresponding word in English is" Class ", If you flip the English dictionary, you can find that the original meaning of" class "is" Type, classification of... (or classification) ". The concept of class should first come from the taxonomy, which means to classify objects (the class may not be accurate, and you are welcome to correct the class ), for example, biology divides biology into animals and plants according to a certain standard, and then classifies animals into different types, such as fish, crawling, and amphibious animals, based on other standards, as shown in:
Speaking of this, you may be excited: the original object-oriented class is classification, which is great! I am best at this! Don't be too happy. Who knows what the object-oriented classification standards are? Is it a biological standard, or is it possible to climb a tree? Different standards lead to different classification results, as shown in:
Suppose we need to write a bullet-coated fish (also known as Xihu fish). The English name is goby. It is a kind of fish that can climb the land and go to trees. It is said that it is extremely delicious and has been said by sea people ), how to write? Is it too easy? Code In minutes: 1 ' Fish
2 Public Class Fish
3
4 End Class
5
6 ' Tree-crawling fish
7 Public Class Climbablefish
8 Inherits Fish
9
10 End Class
11
12 ' Whitebait
13 Public Class Goby
14 Inherits Climbablefish
15
16 End Class
After finishing the course, it seems to be a perfect solution to the problem. However, at this time, a classification standard has been added, including the fish that can be eaten and the fish that cannot be eaten (sharks can eat, but they have never tasted better, let's assume that sharks cannot eat them.) What should we do? Stupid, write another" Eatablefish I don't have the class. Let the cute bullet-coated fish derive from the fish you can eat. I like the fish I like most! Slow! Before doing this, I want to find out the question: Which class does eatablefish derive from? Derived from the climbablefish class? Can all the fish that can be eaten climb trees? If it is derived from fish, isn't it true that fish that can climb trees cannot eat them? Why does Microsoft not support multi-inheritance in. Net? Forget it, or switch to the Java camp. A weak brother came up with the following sentence: It seems that Java does not support multiple inheritance. What should I do? Is there no way to solve this problem?
It seems that we can't determine the object-oriented category by means of classification. Are we wrong? But the inheritance of classes mentioned above in many textbooks is "is a" (a) relationship. Is "Fish that can eat, bullet coated fish" Is "The fish that can climb the tree can read it well. Wrong! We are all misled by textbooks! What are object-oriented concerns? The focus is on object behavior. Object-oriented uses behavior to classify objects! Why can a derived class replace a base class (replacement principle) in an object-oriented class is not because the derived class is a base class, but because the derived class has the same behavior as the base class, when the behaviors of the derived class and the base class are inconsistent, the derived class is still a base class. (What do you do if someone denies this? Someone shouted: Kill him !), However, at this time, the derived class removes the behavior of the base class and violates the replacement principle, which is also the origin of the disgusting design. Therefore, for object orientation, we should pay attention to "act as" and use the "act as" standard to classify objects, all the pseudo standards such as "is a" are thrown into its home.
Someone else quit: Tell me what the attribute is! Right. What is the attribute action? If you have such doubts, Please carefully consider whether the attribute can be considered as the two methods of getxxx and setxxx. As for how the fields are stored, I will not say much about them, let's go back and think about it. Some things belong to the development platform and have done a lot of work for us, but we just don't know.
Well, the question is a bit too much. We should discuss how to use "act as" to classify objects.
The Code is as follows: 1 // Cute fish Interface
2 Public Interface Ifish
3 {
4 }
5
6 // Cute tree crawling Interface
7 Public Interface Iclimbable
8 {
9 }
10
11 // Interface
12 Public Interface Ieatable
13 {
14 }
15
16 // The bullet-coated fish is playing.
17 // I want to play fish
18 // I want to play a tree Crawler
19 // I want to play delicious food. Nobody seems willing to play this.
20 Public Class Goby: ifish, iclimbable, ieatable
21 {
22 }
Master! How can I act as a fish? Master.
It seems that our experts are still not getting rid of the "is a" attack! We don't want fish! What we want is behavior! What determines the behavior? It depends on the behavior expected in your class. For example, I need an object that can provide swimming behavior, you can abstract the "iswimable" Action (this word may not be correct), and then seek to implement the object of this action (interface inversion principle ).
Some may have some questions about how actions inherit from each other (interface inheritance )? For example: 1 // I am a data source provider
2 Public Interface Idatasource: idisposable
3 {
4 }
5
Think about it, is it inheritance? Is it "is? No! I don't know if you have ever played boxing or other combat games. You need to know that continuous keys can be combined into a big trick. In some cases, the target user or theoretical consumer, it must be that the object provides both of the above actions at the same time. This is often the case when software is designed. If it is not clearly identified, it is very likely that the action that should be split will be used as a combination of boxing (interface isolation principle ), this is the beginning of chaos and the reason for restructuring.
We have always categorized objects from the perspective of "is a", but think carefully about what is the standard of "is? How can we determine an object "is a" and another object? Do you basically rely on speculation or experience? This is why software design has always been regarded as an artistic act. The following figure shows my point of view. For more information, see:
The arrow indicates the action of the object. The action we care about refers to the action that falls within the scope of the system or the action that the system cares about.
Now, let's write it here today. I will discuss in detail how to split actions and how to design topics in the future.