Java reflection before the contact more, but only the technical aspects of thinking, this time mainly from a business perspective 思考java反射在业务模块设计中如何发挥更高效的作用,以提高生产率 .
1. Business Requirements
In order to process the bank slips, the payer, payee and correspondent Bank and business information are extracted, and each bank is not in a uniform format. Therefore, the format of each bank needs to be preprocessed, normalized to the standard format, to be processed by subsequent modules.
This preprocessing format is designed in a variety of
2. Program Selection
| Practices |
Applicable Scenarios |
Skill Requirements |
| In the IF.. Else to add the branching logic |
For scenes with fewer branches, such as only a few |
Simple |
| Extract the branch logic as a handle, display registered to the handle map or list |
It is used to branch more scenes, but the change is not small, otherwise if the change frequently, registration is easy to miss out |
Need to have a certain business abstraction and interface design capabilities |
| Extract branch logic as a handle, automatically registered to map or list by reflection |
It is used to branch more scenes, but also to meet the frequently changing scenes |
Abstract Ability +java Reflection technology |
| Register to list? |
The Branch judgment logic is complex, can only use the inefficient traversal judgment |
|
| or sign up to map? |
Clear logic of branch judgment, especially the existence of keyword distinction |
|
3. Reflection-based technical solutions
The features of our scene:
- The Branch judgment logic is very clear, that is, according to each bank's type of the receipt of branch judgment.
- And there are new types that will add in.
So take the following approach
3.1. Abstract Formater interface, manage all interfaces via map
publicstaticnew HashMap<String,Formater>(); publicinterface Formater { void format(Resource resource); }
3.2. Auto-Registration via Java reflection
- get all implementation classes (using the reflections, this is an open source project, on GitHub has the source code)
- Instantiation (in the case of an inner class, pay particular attention to the instantiation method, which can be referenced by using new and reflection to produce an instantiated object of the Java inner Class)
- Sign in to map
Static{Reflections Reflections =NewReflections ("Prepare");//Package name, can reduce the search scopeset<class<? Extends formater>> subtypes = reflections. Getsubtypesof (Formater.class); for(class< extends formater> clazz:subtypes) {String typeName = Clazz.gettypename ();intIDX = Typename.lastindexof ("$");//inner class scene intIDX1 = Typename.lastindexof (".");if(idx<idx1) idx = idx1; String className = typename.substring (idx+1);//Extract the class name as the primary key Try{Formatermap.put (ClassName, Clazz.newinstance ()); }Catch(Exception e) {E.printstacktrace (); } } }
3.3. Simple Application Module invocation
//对银行回单进行预处理Formater formater = PrepareModule.formaterMap.get(resource.getResourceType());ifnull) formater.format(resource);//do next....
3.4. Extended branch is simple and error-prone
Later expansion is also relatively simple, only need to add a new method implementation class, do not have to worry about missing the registration problem.
staticpublicclass ChinaBankReceiptFormater implements Formater { @Override publicvoidformat(Resource resource) { //TODO } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Application Practice of Java Reflection