Android 基于PureMVC封装AppFacade高度解耦合框架(GitHub已更新)

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:

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    <dependency>
        <groupId>com.github.RunsCode</groupId>
        <artifactId>AppFacadeMVC</artifactId>
        <version>v1.0.0</version>
    </dependency>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,901评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,803评论 19 139
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 5,212评论 0 1
  • 北京时间8月8日21时19分,四川省阿坝州九寨沟县,发生了7.0级地震,震中位于北纬33.2度、东经103.82度...
    无畏的太阳阅读 4,383评论 0 3
  • 恶人。冷漠对人,内心戏3分钟, 还差得远呢
    忧郁系小星阅读 1,427评论 0 0

友情链接更多精彩内容