Spring Series (3/2)-improved interaction

Source: Internet
Author: User

In the previous article, we wrote a proxy class that can implement some functions, but as a prototype of the dynamic proxy class, there is still a problem. Let's improve the original class as follows:

/// <Summary>
/// Proxy class, inherited from Aclass. this is required. Otherwise, proxyaclass1 cannot use Aclass. the improvement here mainly refers to the delegate that needs to be cut in and passed in using construction parameters, which is conducive to the Dynamic Construction of instances.
/// </Summary>
Public class proxyaclass1: Aclass
{

Private Aclass _ target;
Private list <interactordelegate> _ interactatalist = NULL;
Private list <interactordelegate> _ interactatb1list = NULL;
Private list <interactordelegate> _ interactatb2list = NULL;
Private list <interactordelegate> _ interactatc1list = NULL;
Private list <interactordelegate> _ interactatc2list = NULL;
Private list <interactordelegate> _ interactatdlist = NULL;
Private list <interactordelegate> _ interactatelist = NULL;
/// <Summary>
/// Constructor. You need to input the target class instance.
/// </Summary>
/// <Param name = "target"> </param>
Public proxyaclass1 (Aclass target, list <interactordelegate> A, list <interactordelegate> B1,
List <interactordelegate> B2, list <interactordelegate> C1,
List <interactordelegate> C2, list <interactordelegate> D,
List <interactordelegate> E)
{
_ Target = target;
This. _ interactatalist =;
This. _ interactatb1list = b1;
This. _ interactatb2list = B2;
This. _ interactatc1list = C1;
This. _ interactatc2list = c2;
This. _ interactatdlist = D;
This. _ interactatelist = E;
}
/// <Summary>
/// Execution method example. The code here is the same as that in the previous article.
/// </Summary>
Public override void save ()
{
//
}
}
Public class testclass
{
Public static void test ()
{
Aclass A = new Aclass ();

List <interactordelegate> theas = new list <interactordelegate> ();
Theas. Add (New interactordelegate (pa_interactata ));

Proxyaclass1 Pa = new proxyaclass1 (A, theas ,.....);
//......
}

Static bool pa_interactatb1 (object target, object sender)
{
// Do something
Return true;
}

Static bool pa_interactata (object target, object sender)
{
// Do something
Return false;
}
}

// The red code can actually be completed dynamically, because we know the constructor and the proxy class name, and we can use the reflection mechanism to dynamically create instances of this class. Up to now, this proxy class has been completely dynamically generated, and can be used in 1st articles or emit il command injection methods. Although it seems that AOP programming is not professional enough, it can be fully implemented in terms of functions.

Why doesn't it look professional? Let's take a closer look at this application method, which is somewhat awkward, mainly including the following four points:

1) No factory for creating a proxy class. If you need to create a factory on your own every time, first, it will make it inconvenient for users to use it, and second, you need to know too much about it, it is also a professional task to instantiate the proxy class. It is better to have a single responsibility for the factory class.

2) Although the delegation is simple, it is generally not authentic to attach a responsibility class, such as a log class or a security check class.

3) the method for adding the entry point delegate is also a bit clumsy.

4) deciding whether to continue execution depends on the control of the proxy class. Although in the previous implementation, we use the return value as a control method, sometimes, we also hope to delegate the Decision-Making Right to the external parties with the responsibility class for direct completion, so that the control is more flexible.

In the next article, we will continue to improve some of the above issues and strive for better implementation.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.