PureMVC_AppFacade_Android
这库是一个轻量级MVC为Android软件从头开始架构的,特点是轻、解耦、模块化,业务分离,简单实用
该库的设计主要有 Facade、Observer等,以及热插拔特性,充分给了我开发者注重业务开发逻辑注意力,而不用在意逻辑的分离与耦合
How To Get Started:
第一步 初始化自定义模块控制类:用于注册模块和解注册模块
AppModuleController:
初始化和创建 AppModuleController, 它继承自 Controller
public class AppModuleController extends Controller {
@Override
public void registerAllModules() {
super.registerAllModules();
//这里注册对应模块
this.addOnceModuleClass(RunsUserLoginModule.class);
this.addOnceModuleClass(RunsHomePageModule.class);
}
@Override
public void unRegisterAllModules() {
super.unRegisterAllModules();
//这里解注册对应模块(最好位置与注册对应)
this.removeModule(RunsUserLoginModule.class.getName());
this.removeModule(RunsHomePageModule.class.getName());
}
}
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding = null;
public MainActivity() {
super();
//初始化调用 注册模块到模块管理类
Facade.getInstance().init(new AppModuleController());
}
}
第二步 创建模块文件夹目录(如图)
至于每个模块分别是什么职责一目了然
第三步 创建对应的功能模块,比如登录、主页等
RunsUserLoginModule
初始化和创建 RunsUserLoginModule, 它继承自 Module, 注册该模块需要的Mediator,ViewModel, 以及移除解注册.
public class RunsUserLoginModule extends Module {
@Override
public void onRegister() {
this.registerMediatorClass(RunsUserLoginMediator.class);
this.registerMediatorClass(RunsUserRegisterMediator.class);
//
this.registerViewModelClass(RunsUserLoginViewModel.class);
}
@Override
public void onRemove() {
this.removeAllMediator();
this.removeAllViewModel();
}
}
public class RunsUserLoginMediator extends Mediator {
@Override
public void initializeMediator() {
super.initializeMediator();
}
@Override
public void handleNotification(INotification notification) {
super.handleNotification(notification);
String notificationName = notification.getName();
if (notificationName.equals(Runs.BIND_VIEW_COMPONENT)) {
Object object = notification.getObject();
if (null != object) {
this.setViewComponent(object);
Toast.makeText((Context)object, Runs.BIND_VIEW_COMPONENT, Toast.LENGTH_SHORT).show();
}
return;
}
if (notificationName.equals(Runs.USER_LOGIN_NOTIFICATION)) {
...
return;
}
if (notificationName.equals(Runs.USER_REGISTER_NOTIFICATION)){
...
return;
}
if (notificationName.equals(Runs.USER_LOGOUT_NOTIFICATION)) {
...
return;
}
}
@Override
public String[] listNotificationInterests() {
return new String[]{
Runs.BIND_VIEW_COMPONENT,
Runs.USER_LOGIN_NOTIFICATION,
Runs.USER_LOGOUT_NOTIFICATION,
Runs.USER_REGISTER_NOTIFICATION };
}
}
public abstract class Runs {
public static final IFacade FACADE = Facade.getInstance();
//RunsUserLoginMediator
public static final String BIND_VIEW_COMPONENT = "BIND_VIEW_COMPONENT";
public static final String USER_LOGIN_NOTIFICATION = "USER_LOGIN_NOTIFICATION";
public static final String USER_LOGOUT_NOTIFICATION = "USER_LOGOUT_NOTIFICATION";
public static final String USER_REGISTER_NOTIFICATION = "USER_REGISTER_NOTIFICATION";
//RunsUserRegisterMediator
public static final String BIND_REGISTER_COMPONENT = "BIND_REGISTER_COMPONENT";
public static final String USER_BIND_PHONE_NOTIFICATION = "USER_BIND_PHONE_NOTIFICATION";
public static final String USER_REGISTER_DONE_NOTIFICATION = "USER_REGISTER_DONE_NOTIFICATION";
}
public class RunsUserLoginViewModel extends ViewModel {
@Override
public void setModel(Object model) {
super.setModel(model);
}
@Override
public void initializeViewModel() {
super.initializeViewModel();
}
}
Usage:
1.根据注册名(Java中反射获取Class字符串)获取对应 Mediator(不常用)
String className = RunsUserLoginMediator.class.getName();
RunsUserLoginMediator mediator = (RunsUserLoginMediator)Runs.FACADE.getMediatorWithName(className);
2.根据注册名(Java中反射获取Class字符串)获取对应 ViewModel(常用)
String className = RunsUserLoginViewModel.class.getName();
IViewModel viewModel = Runs.FACADE.getViewModelWithName(className);
3.核心用法,消息派发.通过sendNotification方法,发出的消息会被对应监听的Mediator收听到而做对应的逻辑处理(核心)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Runs.FACADE.sendNotification(Runs.BIND_VIEW_COMPONENT, this);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runs.FACADE.sendNotification(Runs.USER_LOGIN_NOTIFICATION);
Intent intent = new Intent(MainActivity.this, RunsUserLoginActivity.class);
startActivity(intent);
}
});
activityMainBinding.register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runs.FACADE.sendNotification(Runs.USER_REGISTER_NOTIFICATION);
}
});
activityMainBinding.logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runs.FACADE.sendNotification(Runs.USER_LOGOUT_NOTIFICATION);
}
});
}
Download
You can download a jar from GitHub's releases page.
Gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.github.RunsCode:AppFacadeMVC:v1.0.0'
}
Maven:
jitpack.io
https://jitpack.io
com.github.RunsCode
AppFacadeMVC
v1.0.0