Hi all.
I have made an irc bot in java and he's working fine.
But now I want to make a plugin system.
It's meant to organise the code and make it easier to add other functions.
I have absolutely no idea of how to start with it.
How can I do this?
Printable View
Hi all.
I have made an irc bot in java and he's working fine.
But now I want to make a plugin system.
It's meant to organise the code and make it easier to add other functions.
I have absolutely no idea of how to start with it.
How can I do this?
What you need to do, is define an interface (or set of them) that all plugins must comply with. Then, you use reflection to create instances of the plugin.
For example, the interface definition...
A plugin that complies with the interface...Code:public interface IRCPlugin {
public void doSomething();
}
Next, you need a system that allows you to load the plugins without recompiling. Here is a code frgament that demonstrates the idea. In this fragment, it is assumed a hot load is not necessary (i.e., must restart the program to pick up new plugins). If hot load is desired, you can use a custom ClassLoader to load them dynamically.Code:public class IRCPluginA implements IRCPlugin {
public void doSomething() {
// ...put the unique code here to do something
}
}
Code:
// obviously, you want to get the name of the plugin from a command line, environment variable, properties file, or whatever rather than hard coding like this
IRCPlugin plugin =
(IRCPlugin) Class.forName("IRCPluginA").newInstance();
plugin.doSomething();
I don't really get it.
Could you explain it a bit more?
Do you understand what interfaces are and their power? Do you understand basic reflection? Explain which parts you don't get.