通过注解在编译时自动生成工厂与生产者关系的配置绑定
-
Add repository to root
build.gradle
:buildscript { repositories { jcenter() } } allprojects { repositories { jcenter() } }
-
Add classpath to root
build.gradle
:dependencies { classpath "com.taoszu.configurer:plugin:1.1.7.3" }
-
Add plugin to project's
build.gradle
:apply plugin: 'com.taoszu.configurer'
- 定义基础接口类
public interface BaseProgramer {
void doProgram();
}
-
配置注解(两种方式)
- 在类增加注解Wokrer
@Worker(key = "android", module = "IT", baseClass = BaseProgramer.class ) Class AndroidProgramer implements BaseProgramer { @Override public void doProgram() { Log.e("Programer", "我是安卓程序🐒") } }
- 在类增加注解Factory
@Factory(module = "IT") public class ITFactory implements BaseFactory<BaseProgramer> { Map<String, BaseProgramer> baseProgramerMap; public ITFactory() { baseProgramerMap = new HashMap<>(); baseProgramerMap.put("android", new BaseProgramer() { @Override public void doProgram() { Log.e("Programer", "我是安卓程序🐒") } }); } @Override public BaseProgramer getWorker(String key) { return baseProgramerMap.get(key); } }
-
调用 FactoryHub.load() 初始化
-
获取Worker
ITFactory itFactory = (ITFactory) FactoryHub.getFactoryInstance("IT");
BaseProgramer androidProgramer = itFactory.getWorker("android");
androidProgramer.doProgram()