[ASP. NET Quick Start 7] leading role and supporting role
-- Oo ideology polymorphism, interface and Delegation
Once upon a time, our father-in-law received a picture of a wonderful life: "upstairs and downstairs, an electric phone number". Thirty years later, the electric phone number has already become a daily necessities for our common people, but there seems to be more content for a better life. The heavy seven continents of the earth's crust plate slowly moved, the tall and straight Himalayas slowly increased, the world is changing, the only thing that remains unchanged is change.
■ Paradox of software development-turning changes into plans
In the process of software development activities, it is often entangled in a paradox: no code is written, and no code is written.
People's thinking is very casual. Different people or different stages of the same person may have different views on the same thing, the distance varies. Randomly adding changes often causes project abortion or great delays in the construction period.
The reason for software engineering is to try to save our dead brain cells and the RMB. CIM/ISO bureaucracy, each stage requires scientific management and evaluation. If the quality is poor, rework will be performed, and every construction activity will be conducted only once. The difficulty lies in the difficulty of quantitative computing for development activities. The so-called evaluation is often the competition of everyone on the wine table. some errors in the early stages of the activity are infinitely magnified in the future. The commercial-driven RUP is a bit confused, it is required that Einstein's rough first stool should be spelled out in the shortest time and handed over to the user. After confirmation, the first stool should be continuously transformed on this basis until the user is satisfied with the nth stool. A large number of users are required to participate, however, users are not software engineers. It is difficult for them to grasp complex activity plans. In short, both of them have defects, and correction errors and iterative development are inevitable. Therefore, at the beginning of the software architecture design, we must consider adapting to changes in demand and minimize the impact of changes.
The OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (Inheritance and synthesis) → designing and implementing classes and instances. In this process, the rational use of some features of the OO language will affect the software's adaptability to demand changes, and its proficiency is often the benchmark for evaluating software developers.
■ Stay away from Switch-polymorphism and abstract classes as much as possible
The basic idea of OO is to regard objects with States and methods as basic particles composed of systems, and describe the behavior of systems by the InterAction (or communication) between particles. Methods usually define method interfaces and implementations in the scope of a class and are used to modify the class status, such as Titanic. mobile (direction), then the method is a secondary role of the class, for class services, that isClass Method.
Class methods may have multiple forms. As long as the signature (Return Value and parameter list) are different, we can use method overloading to unify the concept (method name), such as Titanic. mobile (direction) and Titanic. moving (direction, water flow), the implementation of the two conditions is different, but as a user of the class concept, Titanic is moving. In the process-oriented era, the switch statement is often responsible for the process of method polymorphism.
At the beginning of a structured language, the focus of the violent attacks on the unstructured language was actually on a statement-Goto, which destroyed the organizational structure of the program, as if it was the source of all evil; like this, after more than a decade, when the OO language became popular, people also found a statement that was a rigid poison. It was a switch statement. The switch statement is a typical structured programming idea. The common attributes of many modules are mapped to a point in the Value Field of the Flag variable, which is simply arranged in one piece, as shown in the following example:
Switch (ship type)
{
Case "merchant ship"
Merchant's mobile computing method; break;
Case "warship"
Mobile Computing Method of warships; break;
}
In the above example, the problem with the switch statement is that it kills the structural nature between many marine mobile computing methods, and lacks the necessary abstraction for similar methods. The consequence is that the bloated figure is everywhere. Once the demand changes, we have to search around and make corrections one by one.
Here the moving method is actually a shipCommon Methods of class Systems. As a class system user, He must want to unify the concept (method name) like heavy load, and identify whether the merchant ship or the warship is moving through the method signature, so we should adjust it as follows:
Void controls mobility (SHIP)
{
Ship. Mobile;
......
}
War Ship Bismarck = new war ship ();
Merchant Ship Titanic = new merchant ship ();
Control mobile (Bismarck );
Control mobile (Titanic );
In the control mobile (Titanic) method, we replaced ship with the merchant ship's moving method. in fact, this is the Lee's replacement principle mentioned in "think with six", that is, the child type can replace the base type. The polymorphism of the class method is implemented by the upload (upcast) mechanism, 7-1. As with the heavy load, it is automatically completed by the OO language.
The upload mechanism has a very intuitive benefit. If the basis class temporarily adds a race boat subclass, we can easily expand the original software without modifying the existing code, that is"Open-closed" Principle(OPen-CLosedPRinciple ). The following code does not affect the mobile control method:
Race ship magelyn = new race ship ();
Control mobile (magelun );
The function extension does not affect the control of the mobile method, because the control of the mobile method only depends on the base class of the ship class. If a public method of a class system is set up, it is definedAbstract class(Abstract), Other classes only depend on the abstract class. As long as the method interface of the abstract class remains unchanged, the internal adjustment of the inherited subclass does not affect the external use, the maximum degree is the stability of the external architecture and the class system (in this case, the dependency relationship. As an abstract class, it has two important features: one is that the method is not implemented only by the interface definition, and the other is that the subclass (non-abstract class inheritance) must implement all methods of the abstract class. Abstract classes cannot be instantiated, and there is no need to instantiate them.
■ I am the main character-interface and Delegation
OO ideas generally emphasize that class is the main character, and the method is the supporting role of class, especially the abstract class system. The base class has a strong coupling relationship with all sub-classes. If it falls down, people will be lost, the team is hard to bring. Once the abstract class needs to be changed (the public method for adding, deleting, and modifying classes), the impact on a software architecture is no less than the pity Russia has on the rise of Ukraine's natural gas price in Europe, it may bring about domino effect.
When a method is not just an abstraction of Public behaviors of multiple class systems, we must escalate the supporting role Method to the leading role so that it can control the relevant class systems, the method is to separate public method interfaces and define them separatelyInterface)And then implement them one by the relevant classes. In this case, we can call itMethod class. All external relationships with them are no longer built on abstract classes but interfaces, 7-2. When defining an interface, be sure not to make it too bloated. The dependence of a class on another class should be based on the minimum interface. Complex interfaces can be separated by multiple inheritance or delegation of interfaces.
The reason why we upgraded our approach from a secondary role to a leading role step by step is that we want to isolate the unstable components in our needs and use the relatively stable components as the framework to construct the system. The general concept has a much larger life cycle than a special one, for example, a job> A President> Fang Xingdong. Therefore, the concept must be abstract rather than specific, that isDependency inversion principle(DEpendenceINversionPRinciple), the "open-closed" principle mentioned above is its extension.
In addition, although the method changes from a secondary role to the main role, its implementation is still in the scope of this class. Sometimes the method is more out of the box. In addition to defining the method signature and notifying them of the Start message, the delegate class has no control over the implementation of the method. It even knows who is the main character today after it comes to power. This isDelegate(Delegate. The essence is that the class directly references the method of another class. As long as the delegate method is consistent with the delegate method interface, the purpose is to completely separate the interface and Implementation of the method.
Class merchant ship {// delegate class
Private int power;
Private delegate int resistance calculation delegate (); // defines the delegate
Public void Movement (resistance calculation delegate resistance)
{
Int force = power-resistance (); // reference delegate
......
}
}
Class test {// delegate class
Private int wind resistance calculation (){......};
Static void main ()
{
Merchant Ship Titanic;
Titanic mobile (New Resistance Calculation delegate (wind resistance calculation) // delegated instance
}
}
The delegation can be understood as follows: in the previous example, the merchant's class is Zhang San, the test class is Li, the trustee's lawyer, and Zhang San (the merchant's class) said that I would like to Sue Wang Wu (the resistance calculation commission). Li's chest beat: no problem. I will handle this matter according to the civil procedure (calculation of wind resistance.