Android 消息总站 设计思路

项目是组件化模式,这里记录下项目消息总站设计思路

目录

1、接口模式

[2、Viewmodel 模式](#2、Viewmodel 模式)

[3、LiveDataBus 模式](#3、LiveDataBus 模式)

[3、EventBus 模式](#3、EventBus 模式)


1、接口模式

消息总站:MessageCenter 单利模式

复制代码
public class MessageCenter {
    private static MessageCenter instance;
    private final List<Notifiable> observers = new ArrayList<>();

    private MessageCenter() {
    }

    public static synchronized MessageCenter getInstance() {
        if (instance == null) {
            instance = new MessageCenter();
        }
        return instance;
    }

    public void addObserver(Notifiable observer) {
        observers.add(observer);
    }

    public void removeObserver(Notifiable observer) {
        observers.remove(observer);
    }

    public void notifyObservers(String message) {
        for (Notifiable observer : observers) {
            observer.onNotify(message);
        }
    }
}

Notifiable 接口

复制代码
public interface Notifiable {
    void onNotify(String message);
}

使用:

发送消息

复制代码
MessageCenter.getInstance().notifyObservers("测试消息");

接收消息

实现接口:implements Notifiable

添加观察者:MessageCenter.getInstance().addObserver(this);

实现回调方法:

复制代码
@Override
public void onNotify(String message) {
    
}

2、Viewmodel 模式

使用 LiveDataLifecycle 来实现消息中心

实现步骤:

定义消息中心

复制代码
public class MessageViewModel extends ViewModel {

    private final MutableLiveData<String> message = new MutableLiveData<>();

    public void sendMessage(String msg) {
        message.setValue(msg);
    }

    public LiveData<String> getMessage() {
        return message;
    }
}

为了在应用程序级别共享 ViewModel,我们需要定义一个 ViewModelProvider.Factory

复制代码
public class ViewModelFactory implements ViewModelProvider.Factory {

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(MessageViewModel.class)) {
            return (T) new MessageViewModel();
        }
        throw new IllegalArgumentException("Unknown ViewModel class");
    }
}

创建一个单例类来管理 ViewModelProvider,以便在组件化模式下使用

复制代码
public class ViewModelProviderManager implements ViewModelStoreOwner {

    private static ViewModelProviderManager instance;
    private final ViewModelStore viewModelStore = new ViewModelStore();
    private final ViewModelProvider.Factory factory;

    private ViewModelProviderManager(Application application) {
        this.factory = new ViewModelFactory();
    }

    public static synchronized ViewModelProviderManager getInstance(Application application) {
        if (instance == null) {
            instance = new ViewModelProviderManager(application);
        }
        return instance;
    }

    @Override
    public ViewModelStore getViewModelStore() {
        return viewModelStore;
    }

    public ViewModelProvider.Factory getFactory() {
        return factory;
    }
}

使用:

创建messageViewModel

复制代码
   messageViewModel = new ViewModelProvider( ViewModelProviderManager.getInstance(AppProvider.getInstance().getApp()),
                ViewModelProviderManager.getInstance(AppProvider.getInstance().getApp()).getFactory()
        ).get(MessageViewModel.class);

发送消息

复制代码
messageViewModel.sendMessage("测试消息");

接收消息

复制代码
       MessageViewModel messageViewModel = new ViewModelProvider( ViewModelProviderManager.getInstance(AppProvider.getInstance().getApp()),
                ViewModelProviderManager.getInstance(AppProvider.getInstance().getApp()).getFactory()
        ).get(MessageViewModel.class);

        messageViewModel.getMessage().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                
            }
        });

3、LiveDataBus 模式

使用:

发送消息

复制代码
LiveDataBus.get().with("key_test").setValue("123456");

接收消息

复制代码
        LiveDataBus.get()
                .with("key_test", String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                    }
                });

3、EventBus 模式

使用:

注册

复制代码
EventBus.getDefault().register(this);

@Subscribe(threadMode = ThreadMode.POSTING)
public void onLocationMessageEvent(LocationMessageEvent event) {

}

发送

复制代码
EventBus.getDefault().post(new LocationMessageEvent(location));

粘性事件
EventBus.getDefault().postSticky(new LocationMessageEvent(location))}
相关推荐
巧克力芋泥包15 小时前
前端使用阿里云图形验证码;并且与安卓进行交互
android·前端·阿里云
Just_Paranoid18 小时前
【MQTT】基于 Android 设备接入物联网平台最佳实践
android·mqtt·eclipse·iot·paho·mqtt.fx
alexhilton21 小时前
深入理解withContext和launch的真正区别
android·kotlin·android jetpack
TDengine (老段)1 天前
TDengine 转换函数 TO_JSON 用户手册
android·大数据·数据库·json·时序数据库·tdengine·涛思数据
q***42821 天前
SpringCloudGateWay
android·前端·后端
卫生纸不够用1 天前
Appium-锁屏-Android
android·appium
阿拉斯攀登1 天前
安卓工控机 OTA 升级方案(SpringBoot+MQTT)
android·spring boot·物联网·iot
顾林海1 天前
从0到1搭建Android网络框架:别再让你的请求在"路上迷路"了
android·面试·架构
花花鱼1 天前
android room中实体类变化以后如何迁移
android
Jomurphys1 天前
设计模式 - 适配器模式 Adapter Pattern
android