Simple Form (1)
-- Form event model (top)
Author: csdn Liu tie Meng
Small order:
The biggest challenge at work is not the mission impossible, but the need to maintain a quiet and balanced mind to focus on your work, one side of the confrontation between the company system, the social economy and the interpersonal environment on this mentality-this is a contradiction that cannot be solved forever.
Body:
As I mentioned in the previous article, building bricks and bricks won't become architects for many years-they still only build walls. Similarly, the heap control is too many to become a programmer-it can only piece together the form. The key to being an architect is to learn the structure of the building. The key to being a programmer is to understand the structure of the program. Today, let's say goodbye to the heap controls on the form and analyze the relationship between the forms and controls on the form, especially the origin of events and event fire and Event Response).
Event Origin
In traditional object-oriented concepts, there is no event, and some are field and method ). So how did the incident come from? In traditional object-oriented programming, if one class wants to call the method of another class, the programmer has two methods:
1. directly call the method name of another class in the method of a class. It looks like this:# Include <iostream>
Class
{
Public:
Void method ()
{
STD: cout <"This Is A." <STD: Endl;
}
};
Class B
{
Public:
Void method (a Arg)
{
Arg. Method (); // B is closely coupled with
}
};
Int main (INT argc, char * argv [])
{
A;
B;
B. method ();
Return 0;
}
The advantage of doing so is that the program structure is quite simple, but the price for this "simple" is that the coupling between classes is too tight, so that the program is almost non-elastic and scalable-so there is another method.2. Keep a function pointer in the main call class, point the pointer to a function, and then call methods of other classes in this function.Woo ~~~ You may ask, "Why not let this pointer direct to other class methods and use an intermediate function as a stepping stone ?" Well, the answer is: The function pointer cannot point to a member function of the Class-A detailed explanation in "C ++ must know". If you want to dig deeper, you can also refer to the in-depth exploration of the C ++ object model. This is what we often call "indirect reference" or the well-known "callback function": D is like the following:
# Include <iostream>
Class
{
Public:
Void method ()
{
STD: cout <"This Is A." <STD: Endl;
}
};
Typedef void (* functionpointer) (); // define a function pointer as a type"
Class B
{
Public:
Functionpointer funpointer; // declare a function pointer member, which must be consistent with the type of the called method.
Void method ()
{
Funpointer (); // call the target method through the function pointer. Class B is not explicitly coupled with any class.
}
};
Void function () // the function that will be called indirectly
{
A;
A. Method ();
}
Int main (INT argc, char * argv [])
{
B;
B. funpointer = function; // bind the member pointer to the function. The class-class coupling problem does not exist.
B. Method (); // main function --> stepping stone function --> called Function
Return 0;
}
This model is really good! It is also widely used in the C/C ++ world. However, the time passes along. the advent of the net era and the insecure pointer (the mother of program crash and Memory leakage: p) are increasingly criticized. C # eventually gave up the pointer-rather, it was a "imprisoned" pointer. Although the pointer is abandoned, C # does not give up this indirect call to reduce the coupling between classes. How does Microsoft do it? In the past, Microsoft added a new data type-delegate (delegate) for the. NET Framework ). As a new type of referenced data, delegation is a type. Since it is a class, it cannot be directly used as a class member, so it can only be used as a delegate instance (it sounds really nonsense ~~ But many beginners are stuck here ). As a member of the class, the delegated instance is indeed like a function pointer. Therefore, I have to correct another widely spread error. Some people say that the delegate is a function pointer upgrade or the delegate is a "Super function pointer". In fact, it should be said that the delegated instance is a function pointer upgrade and delegate. the instance is a "super function pointer ". For the title "Super function pointer", the delegated instance is well deserved. In addition to a method like a function pointer (a function is encapsulated in a class and called a "method"), the function greatly surpasses the function pointer, which is embodied in:
- Each function pointer can only be attached to one target function. The delegated instance can use the overloaded + = Operator to hook n multiple methods and call it multicast delegate ".
- Function pointers have a variety of "common faults" of pointers, and delegate as A. Net hosting class becomes very safe and tame.
- Function pointers cannot point to member functions of the class, but can point to member functions of the class. This is not the credit of delegation. The root cause is that C # is a fully object-oriented language, all functions must be encapsulated in the class to become member functions (there are no global functions scattered outside the class). If the delegate cannot point to the member function, what is the purpose of the delegate? P
If you want to check your delegation, I recommend that you read the dig-up text "delegation in simple words" below. OK. The new indirect call model by C # looks like this:
// = <The true meaning of water> = produced, http://blog.csdn.net/FantasiaX
Using system;
Delegate void mydelegate (); // The method for declaring the delegate type is not the same as that for declaring a regular class
// It looks more like declaring "a function pointer", which is the source of obfuscation I mentioned above.
// In terms of semantics, this sentence is consistent with the previous C ++ code using typedef to define a function pointer as a type.
Class
{
Public void method (string name)
{
Console. writeline ("Hello, {0 }! ", Name );
}
}
Class B
{
Public mydelegate deleinstance; // declare a mydelegate as a member
Public void method ()
{
If (this. deleinstance! = NULL) // security check, which cannot be done by function pointers.
{
This. deleinstance ();
}
}
}
Class Program
{
Static void function () // serves as a stepping stone function and is attached to the delegated instance.
{
A A = new ();
A. Method ("Tim ");
}
Static void main (string [] ARGs)
{
B = new B ();
B. deleinstance + = new mydelegate (function); // create and bind an instance. The returned values and parameter types of the bound function must be exactly the same as those of the delegate.
B. Method ();
}
}
You may ask: Why don't you design the delegate to be the same as the method type of A, and then declare an instance in Class B? In this case, can we simply call the method of the Class A instance by setting aside the "function as a stepping stone? Good idea! However, consider the following question: Class A may have hundreds of parameters and return value types based on customer needs and business logic needs, are you planning to develop several hundred types of Delegation for Class? Even if you have developed it, Will B-class designers be happy to provide such a large number of delegated instances dedicated to Class A for Class B statements? If a programmer who creates Class B accepts hundreds of delegates for Class A, what about the delegates for classes C to Z? Do not accept-unless A and B are two sides; accept-the length of Class B code is not estimated to be lower than that of the CCTV building! Moreover, designers of Class A, Class B, and other classes may not work together in a company, how can we "collude" to do such a huge project that coupling classes? In fact, the incident is a further upgrade of Microsoft's concept of delegation. In terms of code, events only take a small step forward on the basis of delegation, but from the perspective of program logic, events are a huge step forward in thinking! Why? In addition, the next loop is decomposed.To be continue
For more information, see form (II) -- form event model (II)
Legal disclaimer:This article is protected by intellectual property law. If any organization or individual needs to repost this article, it must ensure the integrity of the article (any omission or modification without the permission of the author shall be deemed as an infringement ). If you need to reprint the document, be sure to indicate the source of the article
CsdnTo protect the rights and interests of the website, please note that the author of the article is
Liu tiemeng(Http://blog.csdn.net/FantasiaX) and send an email to the no_sound@hotmail.com indicating the position and purpose of the article. This legal statement must be reproduced together. Everyone is responsible for protecting intellectual property rights. Thank you!