Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
EventBus and MVP: The Chocolate and Peanut Butter of Decoupling Android
1. EventBus & MVP
The Chocolate & Peanut Butter of Decoupling
Android
By Jim Kirkbride
2. A little bit about myself
Jim Kirkbride
Twitter, github, etc: @jameskbride
Software Craftsman with Pillar Technology
Email: james.kirkbride@gmail.com
9. Common Android Problems
• The Framework gets in the way
• Making changes is hard
• Testing is painful
• Dealing with lots of nested callbacks
@jameskbride
10. Root Causes
• All of the code lives in Activities and Fragments
• There are no layers, so everything knows about
everything
• Activities and Fragments call up the inheritance chain,
making testing difficult when all code lives there
• Logic is invoked on click listeners and life-cycle methods
@jameskbride
11. Remediations
• Pull as much code as possible out of Activities and
Fragments
• Create layers of abstraction
• Invert Control to Collaborators
• Favor Composition over Inheritance
@jameskbride
12. Let’s look at an example
of what “no architecture”
looks like
16. Separation of Concerns
“In computer science, separation of concerns (SoC) is a
design principle for separating a computer program into
distinct sections, such that each section addresses a
separate concern.”
https://en.wikipedia.org/wiki/Separation_of_concerns @jameskbride
22. Implement the Presenter
public class MainActivityPresenter {
MainActivityView view;
public MainActivityPresenter(MainActivityView view) {
this.view = view;
}
public void onNewDataReceived(MyData data) {
view.update(data);
}
}
interface MainActivityView {
void update(MyData data);
}
The View is injected
Presenter only interacts
with the View as an interface
@jameskbride
23. public classs MainActivity extends AppCompatActivity implements
MainActivityView {
MainActivityPresenter presenter;
@Override
public void onCreate(Bundle savedInstance) {
presenter = new MainActivityPresenter(this);
}
@Override
public void update(MyData data) {
//Update the view here
}
}
Implement the View
The Activity implements
the View interface
View method which is
invoked by the presenter
@jameskbride
30. Coupling
In software engineering, coupling is the degree of
interdependence between software modules; a measure of
how closely connected two routines or modules are; the
strength of the relationships between modules.
https://en.wikipedia.org/wiki/Coupling_(computer_programming) @jameskbride
38. Publishing Messages
public class EventBusPublisherDemo {
private EventBus eventBus;
public EventBusPublisherDemo(EventBus eventBus) {
this.eventBus = eventBus;
}
public void update() {
eventBus.post(new MessageEvent());
}
}
@jameskbride
39. public class EventBusSubscriberDemo {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
public void onStart() {
EventBus.getDefault().register(this);
}
public void onStop() {
EventBus.getDefault().unregister(this);
}
public static class MessageEvent { /* Additional fields if needed */
}
}
Subscribing to Messages
@jameskbride
40. Why are you still using
EventBus?
RxJava is the new hotness!
@jameskbride