SlideShare a Scribd company logo
1 of 54
Download to read offline
EventBus & MVP
The Chocolate & Peanut Butter of Decoupling
Android
By Jim Kirkbride
A little bit about myself
Jim Kirkbride
Twitter, github, etc: @jameskbride
Software Craftsman with Pillar Technology
Email: james.kirkbride@gmail.com
Why am I giving this
talk?
@jameskbride
Android has a lot of
power, but little
direction… @jameskbride
…here’s a chainsaw kids, have fun!
@jameskbride
..I’m gonna write an app!
@jameskbride
Android is a
“Framework”, not an
“Architecture”
@jameskbride
Without architecture this is what you get
@jameskbride
Common Android Problems
• The Framework gets in the way

• Making changes is hard

• Testing is painful

• Dealing with lots of nested callbacks
@jameskbride
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
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
Let’s look at an example
of what “no architecture”
looks like
How do we improve
this?
@jameskbride
DecouplingSeparation of Concerns
@jameskbride
Separating Concerns
with Model-View-Presenter@jameskbride
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
This sounds really
familiar…
@jameskbride
S
Single Responsibility Principle
OLID
Separation of Concerns
@jameskbride
Model View Presenter
@jameskbride
@jameskbride
Code Time (Finally!)
@jameskbride
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
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
Let’s apply MVP
Things to remember
about the View
@jameskbride
(Activities || Fragments ||
Custom Layouts) == View
@jameskbride
Your View should be
dumb
@jameskbride
Presenter View
@jameskbride
Decoupling
with EventBus
@jameskbride
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
This is a bad thing
because…
@jameskbride
Changes in highly-coupled code cause a
ripple effect
@jameskbride
Testing becomes harder
@jameskbride
How does EventBus
help?
@jameskbride
Remember:
Object-Oriented Development
isn’t really about “things”…
@jameskbride
It’s about messages
@jameskbride
Publisher/Subscriber
@jameskbride
Publishing Messages
public class EventBusPublisherDemo {
private EventBus eventBus;
public EventBusPublisherDemo(EventBus eventBus) {
this.eventBus = eventBus;
}
public void update() {
eventBus.post(new MessageEvent());
}
}
@jameskbride
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
Why are you still using
EventBus?
RxJava is the new hotness!
@jameskbride
Observer
@jameskbride
If your goal is to
decouple your code
Observer won’t help you
@jameskbride
Let’s Apply Pub/Sub
with EventBus
@jameskbride
Remember that first
slide?
@jameskbride
@jameskbride
@jameskbride
@jameskbride
@jameskbride
Questions?
@jameskbride
Thanks!
@jameskbride
References
• Chainsaw: https://pixabay.com/en/log-cutter-wood-cutter-tree-2192640/

• Texas Chainsaw Goat: https://www.flickr.com/photos/noiseprofessor/
13918551433

• Chocolate-Peanut Butter Cheesecake: https://www.flickr.com/photos/
djwtwo/5207974469

• Chocolate02.jpg: https://commons.wikimedia.org/w/index.php?
curid=48897

• Peanut Butter: https://commons.wikimedia.org/wiki/File:PeanutButter.jpg

• Chocolate: https://commons.wikimedia.org/wiki/File:Chocolate(bgFFF).jpg

• Pinky & The Brain: https://www.flickr.com/photos/jdhancock/3725688716
References Cont.
• Android Robot: http://www.android.com/branding.html

• Water Drop: https://commons.wikimedia.org/wiki/
File:Ripple_effect_on_water.jpg

• EventBus Publisher/Subscriber: https://github.com/
greenrobot/EventBus/blob/master/EventBus-Publish-
Subscribe.png

• Server is Down: https://www.flickr.com/photos/samazgor/
11807670403
Resources
• EventBusMVPDemo Application: https://github.com/
jameskbride/EventBusMVPDemo

• EventBus: http://greenrobot.org/eventbus/

• Model View Presenter Pattern: https://en.wikipedia.org/
wiki/Model%E2%80%93view%E2%80%93presenter
Shameless Plug:
Codemash Companion
@jameskbride

More Related Content

What's hot

Making operations visible - devopsdays tokyo 2013
Making operations visible  - devopsdays tokyo 2013Making operations visible  - devopsdays tokyo 2013
Making operations visible - devopsdays tokyo 2013
Nick Galbreath
 

What's hot (15)

DevOps 101 for data professionals
DevOps 101 for data professionalsDevOps 101 for data professionals
DevOps 101 for data professionals
 
React Native: Expectations vs Reality
React Native: Expectations vs RealityReact Native: Expectations vs Reality
React Native: Expectations vs Reality
 
Getting CI right for SQL Server
Getting CI right for SQL ServerGetting CI right for SQL Server
Getting CI right for SQL Server
 
Software Craftsmanship @ Ntnu
Software Craftsmanship @ NtnuSoftware Craftsmanship @ Ntnu
Software Craftsmanship @ Ntnu
 
Making operations visible - devopsdays tokyo 2013
Making operations visible  - devopsdays tokyo 2013Making operations visible  - devopsdays tokyo 2013
Making operations visible - devopsdays tokyo 2013
 
Angular vs React - Devoxx BE 2017
Angular vs React - Devoxx BE 2017Angular vs React - Devoxx BE 2017
Angular vs React - Devoxx BE 2017
 
Devops : Automate Your Infrastructure with Puppet
Devops : Automate Your Infrastructure with PuppetDevops : Automate Your Infrastructure with Puppet
Devops : Automate Your Infrastructure with Puppet
 
How Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better DeveloperHow Functional Programming Made Me a Better Developer
How Functional Programming Made Me a Better Developer
 
Testing for Logic App Solutions | Integration Monday
Testing for Logic App Solutions | Integration MondayTesting for Logic App Solutions | Integration Monday
Testing for Logic App Solutions | Integration Monday
 
G suite apps
G suite appsG suite apps
G suite apps
 
What if we put the end user in the middle?
What if we put the end user in the middle?What if we put the end user in the middle?
What if we put the end user in the middle?
 
Refactoring page objects The Screenplay Pattern
Refactoring page objects   The Screenplay Pattern Refactoring page objects   The Screenplay Pattern
Refactoring page objects The Screenplay Pattern
 
Agile Scrum with virtual teams
Agile Scrum with virtual teamsAgile Scrum with virtual teams
Agile Scrum with virtual teams
 
Customer Ops: DevOps <3 customer support
Customer Ops: DevOps <3 customer supportCustomer Ops: DevOps <3 customer support
Customer Ops: DevOps <3 customer support
 
Improving Dashboards with open content sharing
Improving Dashboards with open content sharingImproving Dashboards with open content sharing
Improving Dashboards with open content sharing
 

Similar to EventBus and MVP: The Chocolate and Peanut Butter of Decoupling Android

The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwist
Mark Fayngersh
 

Similar to EventBus and MVP: The Chocolate and Peanut Butter of Decoupling Android (20)

From Idea to App (or “How we roll at Small Town Heroes”)
From Idea to App (or “How we roll at Small Town Heroes”)From Idea to App (or “How we roll at Small Town Heroes”)
From Idea to App (or “How we roll at Small Town Heroes”)
 
Pain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr SugakPain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr Sugak
 
Yeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web appsYeoman AngularJS and D3 - A solid stack for web apps
Yeoman AngularJS and D3 - A solid stack for web apps
 
Testing Big in JavaScript
Testing Big in JavaScriptTesting Big in JavaScript
Testing Big in JavaScript
 
Continuous Integration, the minimum viable product
Continuous Integration, the minimum viable productContinuous Integration, the minimum viable product
Continuous Integration, the minimum viable product
 
The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwist
 
Fewd week4 slides
Fewd week4 slidesFewd week4 slides
Fewd week4 slides
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
 
Make it compatible
Make it compatibleMake it compatible
Make it compatible
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
 
Using and contributing to the next Guice
Using and contributing to the next GuiceUsing and contributing to the next Guice
Using and contributing to the next Guice
 
DevOps Pipelines and Metrics Driven Feedback Loops
DevOps Pipelines and Metrics Driven Feedback LoopsDevOps Pipelines and Metrics Driven Feedback Loops
DevOps Pipelines and Metrics Driven Feedback Loops
 
jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture jQquerysummit - Large-scale JavaScript Application Architecture
jQquerysummit - Large-scale JavaScript Application Architecture
 
Quick start with AngularJS
Quick start with AngularJSQuick start with AngularJS
Quick start with AngularJS
 
DCEU 18: App-in-a-Box with Docker Application Packages
DCEU 18: App-in-a-Box with Docker Application PackagesDCEU 18: App-in-a-Box with Docker Application Packages
DCEU 18: App-in-a-Box with Docker Application Packages
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
 
Reactive data analysis with vert.x
Reactive data analysis with vert.xReactive data analysis with vert.x
Reactive data analysis with vert.x
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

EventBus and MVP: The Chocolate and Peanut Butter of Decoupling Android