且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android发布/订阅模式

更新时间:2023-01-02 21:29:49

我认为您说的是在应用程序中的类之间传递事件/消息.所以这个问题可能归结为这种模式的 Java 实现.

实际上并没有内置任何东西(即没有 event 系统类),让类传播事件/消息的最常见方法之一是 Listener 技术(参见***Vogel, IBM).

也有框架,从这个SO answer开始.>

如果您担心 Android 中线程/进程之间的异步消息,那么有 处理程序AsyncTasks 和(用于互process) Parcelables.

Is there a publish/subscribe pattern in android?

What I want to achieve is I have this class that can notify interested party of an event. Then the interested party can do whatever it needs.

Coming from a .net microsoft world, this sort of thing are build in.

Do android have something similar, or I have to write some thing like an observer pattern?

I think you are speaking in terms of passing events/messages among classes within your application. So this question probably goes down to Java implementation of such a pattern.

There's nothing actually already baked in (i.e. no event system class), one of the most common way to let a class spread an event/message is the Listener technique (see wikipedia, Vogel, IBM).

There are frameworks too, as from this SO answer.

If you are concerned about asynch messages between thread/processes in Android, then there are Handlers, AsyncTasks and (for inter-process) Parcelables.