SlideShare a Scribd company logo
1 of 58
Download to read offline
Quick Look at Design Patterns
in Android Development
Constantine Mars
Team Lead, Senior Developer @ DataArt
#gdg_dnipro_art
Patterns everywhere
#gdg_dnipro_art
What do “GOF Patterns” mean?
"Each pattern describes a problem which occurs over and over again in our environment,
and then describes the core of the solution to that problem, in such a way that you can use
this solution a million times over, without ever doing it the same way twice"
- Christopher Alexander
#gdg_dnipro_art
Pattern structure
1. Pattern name
2. Problem
3. Solution
4. Consequences
#gdg_dnipro_art
Bricks and clay
1. Interface
2. Instantiation
3. Subtype - Type - Supertype
4. Dynamic binding
5. Polymorphism
6. Encapsulation
#gdg_dnipro_art
Principles
1. Class versus Interface Inheritance
2. Program to an interface, not an implementation
3. Favor object composition over class inheritance
4. Aggregation -> having or being part of
5. Acquaintance -> knows of (association, using)
6. Delegation
7. Inheritance versus Parameterized Types (generics, templates)
#gdg_dnipro_art
Applications of reuse
1. Toolkits - code reuse -> you write the main body of the application and
call the code you want to reuse
2. Frameworks - design reuse -> you reuse the main body and write the
code it calls
Pattern categorieS8 - GOF
Be together, not the same!
#gdg_dnipro_art
Creational Patterns
- Create objects
- Hide creation logic
- Do not use operator “new”
Give program more flexibility
to decide which objects to create
#gdg_dnipro_art
Structural Patterns
Class and object composition
Inheritance, interfaces and ways to
compose objects = obtain new
functionalities
#gdg_dnipro_art
Behavioral Patterns
It’s all about communication
#gdg_dnipro_art
A Mesh of XXIII
#gdg_dnipro_art
Table of Design Patterns by Vince Huston :)
Illustration from http://www.vincehuston.org/dp/
#gdg_dnipro_art
Organizing patterns space by GOF
Free your creativity while patterns work for you :)
Creational Patterns
#gdg_dnipro_art
Factory method
Define an interface for creating an object, but let subclasses decide which
class to instantiate
#gdg_dnipro_art
Abstract Factory
Captures how to create families of related product objects without
instantiating classes directly
Image from https://sourcemaking.com
#gdg_dnipro_art
Builder
Separate the construction of a complex object from its representation so
that the same construction process can create different representations
#gdg_dnipro_art
In Android
new AlertDialog.Builder(this)
.setTitle("Metaphorical Sandwich Dialog")
.setMessage("Metaphorical message to please use the spicy mustard.")
.setNegativeButton("No thanks", new DialogInterface.OnClickListener() { …
}})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {...}})
.show();
Builder
#gdg_dnipro_art
Prototype
Specify the kinds of objects to create using a prototypical instance, and
create new objects by copying this prototype
#gdg_dnipro_art
Singleton
Ensure a class only has one instance, and provide a global point of access it
#gdg_dnipro_art
In Android
public class ExampleSingleton {
private static ExampleSingleton instance = null;
private ExampleSingleton() { // customize if needed }
public static ExampleSingleton getInstance() {
if (instance == null) { instance = new ExampleSingleton(); }
return instance;
} }
ExampleSingleton.getInstance();
Singleton
Building is under construction :)
Structural Patterns
#gdg_dnipro_art
Adapter
Convert the interface of a class
into another interface clients
expect. Adapter lets classes
work together that couldn't
otherwise because of
incompatible interfaces.
#gdg_dnipro_art
public class TribbleAdapter extends RecyclerView.Adapter {
private List mTribbles;
public TribbleAdapter(List tribbles) { this.mTribbles = tribbles; }
@Override public TribbleViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(R.layout.row_tribble, viewGroup, false);
return new TribbleViewHolder(view); }
@Override public void onBindViewHolder(TribbleViewHolder viewHolder, int i) { viewHolder.configure(mTribbles.get(i));}
@Override public int getItemCount() { return mTribbles.size(); }
}
Adapter
#gdg_dnipro_art
Bridge
Allows separate class
hierarchies to work
together even as they
evolve independently
Image from https://sourcemaking.com
#gdg_dnipro_art
Composite
Captures the essence of
recursive composition in
object-oriented terms.
#gdg_dnipro_art
Decorator
Captures class and
object relationships that
support embellishment
by transparent
enclosure
#gdg_dnipro_art
Facade
Provide a unified interface to a set of interfaces in a subsystem. Facade
defines a higher- level interface that makes the subsystem easier to use
#gdg_dnipro_art
public interface BooksApi {
@GET("/books")
void listBooks(Callback<List> callback);
}
RestAdapter restAdapter =
new RestAdapter.Builder()
.setConverter(new CustomGsonConverter(new Gson()))
.setEndpoint("http://www.someurl.com")
.build();
return restAdapter.create(BooksApi.class);
Facade
#gdg_dnipro_art
Proxy
Provide a surrogate or placeholder for another object to control access to it.
All about communication, change and interchange :)
Behavioral Patterns
#gdg_dnipro_art
Command
Abstract class to provide
an interface for issuing a
request. The basic
interface consists of a
single abstract operation
called "Execute."
#gdg_dnipro_art
public class MySpecificEvent { /* Additional fields if needed */ }
eventBus.register(this);
public void onEvent(MySpecificEvent event) {/* Do something */};
eventBus.post(event);
Command
#gdg_dnipro_art
Chain of Responsibility
Chain the receiving objects and pass the request along the chain until an
object handles italgorithm in an object
#gdg_dnipro_art
Iterator
General interface for access and traversal
#gdg_dnipro_art
Mediator
Define an object that encapsulates how a set of objects interact
#gdg_dnipro_art
Memento (Token)
Without violating encapsulation, capture and externalize an object's
internal state so that the object can be restored to this state later
#gdg_dnipro_art
Flyweight
Use sharing to support large numbers of
fine-grained objects efficiently.
#gdg_dnipro_art
Observer
Define a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically
#gdg_dnipro_art
apiService.getData(someData)
.observeOn(AndroidSchedulers.mainThread())
.subscribe (/* an Observer */);
Observer
#gdg_dnipro_art
State
Allow an object to alter its behavior when its internal state changes. The
object will appear to change its class
#gdg_dnipro_art
Strategy
Encapsulating an algorithm in an object
#gdg_dnipro_art
Interpreter
Define a represention for
language grammar along
with an interpreter that
uses the representation to
interpret sentences in the
language
#gdg_dnipro_art
Visitor
Refer generally to classes
of objects that "visit"
other objects during a
traversal and do
something appropriate
Android Patterns
Bald, mad and superperformant! =)
#gdg_dnipro_art
Dependency Injection
#gdg_dnipro_art
@Module
public class AppModule {
@Provides SharedPreferences provideSharedPreferences(Application app) { return
app.getSharedPreferences("prefs", Context.MODE_PRIVATE); }
}
@Component(modules = AppModule.class)
interface AppComponent { … }
@Inject SharedPreferences sharedPreferences;
Dependency Injection
#gdg_dnipro_art
Model-View-Controller (MVC)
● Model: your data classes.
● View: your visual classes
● Controller: the glue between the two
#gdg_dnipro_art
Model-View-ViewModel (MVVM)
● Model: your data classes.
● View: your visual classes
● ViewModel: exposes properties
● Binder: generates ViewModel properties
#gdg_dnipro_art
Model-View-Presenter (MVP)
● Model - interface defining the data
● Presenter - retrieves data from
repositories (model), and formats it for
display in the view
● View - passive interface that displays data
(the model) and routes user commands
(events) to the presenter
#gdg_dnipro_art
Other Patterns...
Loaders,
AsyncTasks,
Broadcast Receivers,
Intents,
Media Framework = Facade,
Object Pools,
Batching and caching
And many other...
UI Design Patterns
Have very similar rules, but are yet different
#gdg_dnipro_art
Links and books on GOF
- Joshua Kerievsky - Refactoring To Patterns
- Bruce Eckel - Thinking in Patterns with Java
- Eric Freeman - Head First Design Patterns
- Erich Gamma - Design Patterns: Elements
of Reusable Object-Oriented Software
- https://sourcemaking.com/design_patterns
- https://dzone.com/refcardz/design-patterns
- https://github.com/iluwatar/java-design-patterns
#gdg_dnipro_art
Links and books on Android Patterns
- Android Design Patterns https://unitid.nl/androidpatterns/
- Alex Lockwood http://www.androiddesignpatterns.com/
- Android Performance Patterns
https://www.youtube.com/playlist?list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE
- TutsPlus
https://code.tutsplus.com/articles/introduction-to-android-design-patterns--cms-20808
- Ray Wenderlich
https://www.raywenderlich.com/109843/common-design-patterns-for-android
#gdg_dnipro_art
Links and books on UI Patterns
- Material Design Guidelines
https://material.io/guidelines/material-design/introduction.html#
- UI Patterns http://ui-patterns.com/patterns
- UI Pttrns https://pttrns.com/android-patterns
“Patterns are everywhere -
from enterprise applications
to games and even devices,
the most important is to see them
Alexey Rybakov, DataArt Technical Evangelist
Constantine Mars
@ConstantineMars
+ConstantineMars
Q&A
Thank you!

More Related Content

Similar to Quick look at Design Patterns in Android Development

P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 

Similar to Quick look at Design Patterns in Android Development (20)

Creational Design Patterns
Creational Design PatternsCreational Design Patterns
Creational Design Patterns
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
 
Software System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptxSoftware System Architecture-Lecture 6.pptx
Software System Architecture-Lecture 6.pptx
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
 
Dark side of Android apps modularization
Dark side of Android apps modularizationDark side of Android apps modularization
Dark side of Android apps modularization
 
Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014Dependency injection Drupal Camp Wrocław 2014
Dependency injection Drupal Camp Wrocław 2014
 
Design Patterns in Cocoa Touch
Design Patterns in Cocoa TouchDesign Patterns in Cocoa Touch
Design Patterns in Cocoa Touch
 
Gang of Four in Java
Gang of Four in Java Gang of Four in Java
Gang of Four in Java
 
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
 
MVC pattern for widgets
MVC pattern for widgetsMVC pattern for widgets
MVC pattern for widgets
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
Green dao
Green daoGreen dao
Green dao
 
Android architecture
Android architecture Android architecture
Android architecture
 
Design patterns
Design patternsDesign patterns
Design patterns
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
 
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for XamarinDesktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
Desktop Developer’s Guide to Mobile with Visual Studio Tools for Xamarin
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design patterns in brief
Design patterns in briefDesign patterns in brief
Design patterns in brief
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 

More from Constantine Mars

More from Constantine Mars (17)

Mobile Applications Architecture - GDG Ternopil' Architecture Components Meetup
Mobile Applications Architecture - GDG Ternopil' Architecture Components MeetupMobile Applications Architecture - GDG Ternopil' Architecture Components Meetup
Mobile Applications Architecture - GDG Ternopil' Architecture Components Meetup
 
Dagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' CornerDagger 2 - Ciklum Speakers' Corner
Dagger 2 - Ciklum Speakers' Corner
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT Talk
 
Jump into cross platform development with firebase
Jump into cross platform development with firebaseJump into cross platform development with firebase
Jump into cross platform development with firebase
 
Android Wear 2.0 - New Level of Freedom for Your Action - GDG CEE Leads Summi...
Android Wear 2.0 - New Level of Freedom for Your Action - GDG CEE Leads Summi...Android Wear 2.0 - New Level of Freedom for Your Action - GDG CEE Leads Summi...
Android Wear 2.0 - New Level of Freedom for Your Action - GDG CEE Leads Summi...
 
Android Wear 2.0 - Great Changes Upcoming This Fall - GDG DevFest Ukraine 2016
Android Wear 2.0 - Great Changes Upcoming This Fall - GDG DevFest Ukraine 2016Android Wear 2.0 - Great Changes Upcoming This Fall - GDG DevFest Ukraine 2016
Android Wear 2.0 - Great Changes Upcoming This Fall - GDG DevFest Ukraine 2016
 
Dagger2 - IT NonStop Voronezh 2016
Dagger2 - IT NonStop Voronezh 2016Dagger2 - IT NonStop Voronezh 2016
Dagger2 - IT NonStop Voronezh 2016
 
DeviceHive Android BLE Gateway
DeviceHive Android BLE GatewayDeviceHive Android BLE Gateway
DeviceHive Android BLE Gateway
 
Scrum Overview
Scrum OverviewScrum Overview
Scrum Overview
 
Android Wear 2.0, Awareness API - GDG Dnipro Post I/O 2016
Android Wear 2.0, Awareness API - GDG Dnipro Post I/O 2016Android Wear 2.0, Awareness API - GDG Dnipro Post I/O 2016
Android Wear 2.0, Awareness API - GDG Dnipro Post I/O 2016
 
RxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtRxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArt
 
Android Wear 2.0 - IT NonStop Dnipro
Android Wear 2.0 - IT NonStop DniproAndroid Wear 2.0 - IT NonStop Dnipro
Android Wear 2.0 - IT NonStop Dnipro
 
Android N Security Overview - Mobile Security Saturday at Ciklum
Android N Security Overview - Mobile Security Saturday at CiklumAndroid N Security Overview - Mobile Security Saturday at Ciklum
Android N Security Overview - Mobile Security Saturday at Ciklum
 
Study Jam: Android for Beginners, Summary
Study Jam: Android for Beginners, SummaryStudy Jam: Android for Beginners, Summary
Study Jam: Android for Beginners, Summary
 
Pebble Watch Development
Pebble Watch DevelopmentPebble Watch Development
Pebble Watch Development
 
Xamarin Forms in Action
Xamarin Forms in ActionXamarin Forms in Action
Xamarin Forms in Action
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 

Recently uploaded

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (6)

Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 

Quick look at Design Patterns in Android Development