Understanding Mobile Architecture Patterns: MVC, MVP, MVVM, Flutter, AAC and Communication Mechanisms
This article reviews Android mobile architecture patterns—including MVC, MVP, MVVM, Flutter-derived framework, and Android Architecture Components—explaining their evolution, layer separation, and communication mechanisms such as object holding, interface contracts, and router-based dispatch, while providing illustrative code examples.
The author, an Android developer, outlines the core ideas behind mobile architecture, noting that while mobile projects are often small, applying solid architectural principles can improve code elegance and reduce coupling.
He traces the evolution of Android architectural patterns from early MVC to MVP, the Flutter-inspired framework, and Google’s Android Architecture Components (AAC/MVVM), emphasizing that the underlying concepts remain similar despite changing names.
Flutter, as described here, is a React‑Native‑derived framework that separates View, Model, Store, Dispatcher, and Action, using a routing mechanism for communication rather than direct object references.
AAC is introduced with a link to the official documentation, and example source code demonstrates its MVVM‑like structure.
The article identifies three primary communication strategies in mobile architectures: object holding (tight coupling), interface‑based communication (moderate decoupling), and router‑based messaging (full decoupling). Code snippets illustrate each approach.
Example of router‑based communication using a Dispatcher:
public class X implements Dispatcher.IReceiver{
Model model;
public X() {
model = new Model();
Dispatcher.getDispatcher().register(this);
}
public void clearX() {
model = null;
Dispatcher.getDispatcher().unregister(this);
}
@Override
public void onReceive(Action action) {
if(action.name.equals("login")) {
model.postLogin((User) action.data, new Model.Callback() {
@Override
public void onResponse(int result) {
if(result == 1) {
// 登录成功
Dispatcher.getDispatcher().sendEvent(new Action("login-success", null));
} else {
// 登录失败
Dispatcher.getDispatcher().sendEvent(new Action("login-failed", null));
}
}
});
}
}
}Supporting Action class:
public class Action
{
public String name; // 执行动作,比如“登录”
public T data; // 数据,比如username, password
public Action(String name, T data) {
this.name = name;
this.data = data;
}
}Dispatcher implementation handling registration, unregistration, and event distribution:
public class Dispatcher {
private static Dispatcher dispatcher = new Dispatcher();
private List
receivers = new ArrayList<>();
private Dispatcher(){}
public static Dispatcher getDispatcher() { return dispatcher; }
public void register(IReceiver receiver) {
if (receivers.contains(receiver)) {
throw new IllegalStateException("receiver has been registerd yet!");
} else {
receivers.add(receiver);
}
}
public void unregister(IReceiver receiver) {
if (receivers.contains(receiver)) {
receivers.remove(receiver);
}
}
public void sendEvent(Action action) {
if (action != null && action.name != null && action.name.length() > 0) {
for (IReceiver r : receivers) {
if (r != null) {
r.onReceive(action);
}
}
}
}
public interface IReceiver {
void onReceive(Action action);
}
}The author concludes that mobile architecture can be viewed as an MVX (Model‑View‑X) pattern, where the X layer handles business logic, and that the choice of communication mechanism—object, interface, or router—determines the degree of decoupling and maintainability.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.