Dynamic loading of classes using reflection in Java

Source: Internet
Author: User
Tags reflection

//首先定义一个接口来隔离类:
public interface Operator
{
//  public java.util.List act(java.util.List params);
public java.util.List act(String content,String content2,java.util.List params);
}

According to the principle of design pattern, we can write different classes for different functions, each class inherits the operator interface, and the client only needs to program the operator interface to avoid a lot of trouble. such as this class:

import java.util.*;
public class Success implements Operator
{
public static void main(String[] args) {
List list = new ArrayList();
list.add("Success3");
Operator op = new Success();
System.out.println("act===" + op.act("Success1", "Success2", list));
}
//    public java.util.List act(java.util.List params)
public java.util.List act(String content, String content2,
java.util.List params) {
List result = new ArrayList();
result.add(content);
result.add(content2);
result.add(params);
return result;
}
}

Similarly, you can write another class:

import java.util.*;
public class Load implements Operator
{
public static void main(String[] args) {
List list = new ArrayList();
list.add("Load3");
Operator op = new Load();
System.out.println("act===" + op.act("Load1", "Load2", list));
}
//    public java.util.List act(java.util.List params)
public java.util.List act(String content, String content2,
java.util.List params)
{
List result = new ArrayList();
result.add(content);
result.add(content2);
result.add(params);
return result;
}
}

We can also write many other classes, but there's a problem, the interface is not instantiated, we have to manually control which class is specifically instantiated, which is very uncomfortable, if you can pass an argument to the application, let yourself choose to instantiate a class, execute its act method, then our work is much easier.

Fortunately, I use Java, only Java to provide such a reflection mechanism, or introspection mechanism, can achieve our unreasonable requirements. Write a configuration file emp.properties:

#成功响应

1000=success

#向客户发送普通文本消息

2000=load

#客户向服务器发送普通文本消息

3000=store

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.