How can we solve the problem of program flow control under Complicated Conditions?

Source: Internet
Author: User
Post http://www.cjsdn.com/post/view? Bid = 1 & id = 39182 & sty = 1 & TPG = 1 & age = 0 propose Program Problems frequently encountered in the design, that is, how to solve the control problem of program flow under complicated conditions. Here, I use reflection to provide a solution to give a key and call the function corresponding to the given key. In this way, the original problem is actually converted to the problem of using conditions to generate suitable keys. A feasible solution for generating keys is to convert these conditions into corresponding strings and then connect them.

The basic idea of the solution is to use hashmap to form a Hasse table of a function, mainly using the followingCodeDuplicate class:


Import java. util. hashmap;
Import java. Lang. Reflect .*;

Public class methodmap {
Private hashmap map;

Methodmap (){
Map = new hashmap ();
}
Public void addjumppoint (string key, method jumppoint ){
Map. Put (Key, jumppoint );
}

Public void removejumppoint (string key ){
Map. Remove (key );
}

Public void jumpto (string key, object OBJ, object [] ARGs ){
Try {
Method M = (method) map. Get (key );
M. Invoke (OBJ, argS );
} Catch (Java. Lang. illegalaccessexception e ){
E. printstacktrace ();
} Catch (Java. Lang. Reflect. invocationtargetexception e ){
E. printstacktrace ();
}
}

}

Source codeSee Attachment:

Methodmap.rar (1.55 K)

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.