Injecting with multiple implementations
Injecting with multiple implementations can let you choose the most suitable way to do your job.
public interface MyService {
void sayHelloTo(CommandSender sender);
void sayGoodBye(CommandSender sender);
}public class MyServiceA implements MyService {
@Override
public void sayHelloTo(CommandSender sender) {
sender.sendMessage("hello world A!!!");
}
@Override
public void sayGoodBye(CommandSender sender) {
sender.sendMessage("good bye A!!!");
}
}public class MyServiceB implements MyService{
@Override
public void sayHelloTo(CommandSender sender) {
sender.sendMessage("hello world B!!!");
}
@Override
public void sayGoodBye(CommandSender sender) {
sender.sendMessage("good bye B!!!");
}
}註冊
Example with command node:
When you use
/service hello true, the service will execute via ServiceA, when you use/service hello falseor/service hello, the service will execute via ServiceB.
Example with scenario
let's say you have multiple ways to let user choose how to save your data.
Mapping object for config:
define StorageService
implement YAML storage service
implement MySQL storage service
register as below
Usage from any singleton
Multiple implementations without key
LogService as example:
Create multiple implementation.
register in main class
Usage:
最后更新于