SlideShare a Scribd company logo
1 of 56
Download to read offline
Software Design Patterns on Android
Pedro Vicente Gómez Sánchez - Mobile Software Engineer, Tuenti Technologies.
@pedro_g_s , @TuentiEng
pgomez@tuenti.com
http://corporate.tuenti.com/es/dev/blog - @TuentiEng
http://corporate.tuenti.com/es/jobs
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Why talk about patterns when we
could be at the bar?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
“If I see again json parsing annotations inside a business logic domain
class, please, kill me.”
“If I see again other project without dependency injection, I will jump out
the window.”
“If I see again a class with 3000 lines of code, I will go to develop for
iOS.”
“If I see again a class with 3000 lines of code, I will stop working on
Android and I will change to iOS.”
“If I see again a project with more than 7 dependencies per class, I will
go to work from Nepal.”
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Pedro V. Gómez
● Introduction
● Problem to solve.
● Patterns on Android
● Renderer Pattern.
● Repository Pattern.
● Command Pattern.
● Some advices.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
DISCLAIMER
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Software Design Patterns on Android - Pedro V. Gómez Sánchez
Software Design Patterns on Android - Pedro V. Gómez Sánchez
What is a Software Design Pattern?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Software Design Patterns?
A Software Design Pattern is a possible solution to a problem.
Similar problems can be solved using a similar approach.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
What defines a pattern?
● Name.
● Problem to solve.
● Structure.
● Consequences.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
What types of patterns exists?
Creation Structure
Behaviour
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
What types of patterns exists?
● Creation:
○ Abstract Factory.
○ Builder.
○ Singleton.
● Behaviour:
○ Adapter.
○ Facade.
○ Bridge.
● Structure:
○ Command.
○ State.
○ Memento.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Pros
● Each pattern is a valid and already defined solution for a concrete
problem.
● Are useful when you want to reuse structures and avoid duplicity.
● Eases the extensibility of your design and accelerate long-term
developments.
● Decouples your code.
● Favors change.
● Reduces maintenance efforts.
● Usually, propose a better solution for responsibility assignations.
● Poses a common vocabulary.
● Solves already solved problems by other developers.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Cons
● Is not always easy to identify which pattern to apply.
● Can suppose a work overload in a short-term period.
● Apply a pattern is not always so easy as apply the structure.
● Can affect to the software performance.
● You can die by the refactoring illness if you only think in
patterns.
● Apply the wrong pattern can create an anti-pattern.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Problem to solve
● Show a list full of different videos.
● Video’s information will be provided by an external API.
● Video list information will be provided by an external API.
● We will show different video types:
○ Normal videos.
○ Popular videos.
○ Favorite videos.
○ Live videos.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Software Design Patterns on Android
Adapter
Chain of
Responsibility
Memento
Model View
Controller
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Adapter
Name: Adapter.
Problem to solve: Transform a class interface
into other interface that the client code wants to
use. Allows the already implemented class
usage with a different interface.
Our problem: Give some views to the Android
SDK to be rendered inside a ListView without
information about the future implementation.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Adapter
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Memento
Name: Memento.
Problem to solve: Capture and externalize the
internal state of an object - keeping the
encapsulation principle - and be able to restore
the object state in the future.
Our problem: Save the Android Activity state to
recover it once the phone changes the
orientation to portrait or landscape.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Memento
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Chain of Responsibility
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Name: Chain of Responsibility.
Problem to solve: Avoid to couple the sender of
a message from the receiver and be able to cut
the event processing from one of the receivers.
Our problem: Notify events to different
receivers in the application until one of them
decides to process the event.
Patterns on Android: Chain of Responsibility
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Patterns on Android: Model View Controller
Name: Model View Controller.
Problem to solve: Decouple the view
implementation from the model
implementation.
Our problem: Create an application where the
views implementation and the business logic
model don’t be coupled.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
ACTIVITIES ARE NOT CONTROLLERS!!!
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Problem to solve
Render video
information inside a
ListView
Data origin Use cases
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Name: Renderer.
Problem to solve: Decouple the rendering
process from our adapters and avoid
ViewHolder pattern.
Our problem: We have different video types to
render with different visual representations.
Normal, favorite, popular and live videos.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Structure: This pattern is a join of other three
patterns.
● Builder: Separate the complex object
instantiation from the object representation.
With this pattern the same construction
process can create different object
representations.
● Delegate: Pattern applied when a class needs
to delegate the implementation in other class
that works as helper.
● Template Method: Define an algorithm
skeleton separated by steps and define some
of them in the subclasses.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Renderer
Builder: Used to create or recycle a view in the getView method of your adapter.
This is not a simple creation process because the view could be recycled in
some cases.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Builder
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Template Method: Applying this pattern
we can declare the main rendering
algorithm and modify it, if need it, in the
subclasses implementations.
For example, a live video will be
represented with a play button that is
not able in the normal representation
and we can add that part of the
algorithm in the LiveVideoRenderer
implementation.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Template method
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Delegate: To apply the delegate pattern we are going
to delegate the recycling and rendering classic
adapter implementation into the
VideoRendererBuilder and into our new
VideoRenderer.
Renderer
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Delegate: With this implementation change you will see how the new implementation will
be based on this schema.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Worth?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Resultado?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Renderer
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
One adapter to rule them all?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
https://github.com/pedrovgs/Renderers
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Repository
Name: Repository.
Problem to solve: Abstract the data origin in a
system with different possible data sources.
Our problem: Abstract the data origin. I don’t
care about where the data comes from. A
memory storage system, a database or an
external API accessed using an API client are
just an implementation detail.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Repository
Different repository
implementations can offer
different benefits. The idea is
to abstract all the client code
to the data origin and be able
to change the repository
implementation as easy as
possible.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Why?
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Because I don’t care if the data comes
from an external API or a local DB!!!
That’s an implementation detail.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Repository
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Repository
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Command
Name: Command.
Problem to solve: Encapsulate a message like
an object being able to parametrize the clients
with different requests, add that messages into
a queue and support undo/redo mechanism.
Our problem: Model our use cases abstracting
the execution process from itself and abstracting
the delivery mechanism code from our model.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Command Use case
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Command
Your model should be between the interactor
and the repository. An interator doesn’t
implements business logic, only use our model
to stimulate it and delegate the implementation
to the business logic domain.
This is just an example, I’m not going to desing
all the business model xD
Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
Some advices
● Implement one model by domain.
● Avoid code duplicity.
● Refactor day by day.
● Apply the single responsibility principle.
● Defer the important decisions.
● Think into the framework like a delivery mechanism, not the
architecture of your application.
● Work from the use case.
● Create testable code.
● Write code for your mates not for the machine.
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
Questions?
http://corporate.tuenti.com/es/dev/blog - @TuentiEng
http://corporate.tuenti.com/es/jobs
Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

More Related Content

What's hot

Clean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design SimpleClean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design SimpleLemi Orhan Ergin
 
Global Day of Coderetreat'14 - Istanbul Event
Global Day of Coderetreat'14 - Istanbul EventGlobal Day of Coderetreat'14 - Istanbul Event
Global Day of Coderetreat'14 - Istanbul EventLemi Orhan Ergin
 
Goals Of Software Design - The main goals
Goals Of Software Design - The main goalsGoals Of Software Design - The main goals
Goals Of Software Design - The main goalsparag
 
Agile Development | Agile Process Models
Agile Development | Agile Process ModelsAgile Development | Agile Process Models
Agile Development | Agile Process ModelsAhsan Rahim
 
Tdd 4 everyone full version
Tdd 4 everyone full versionTdd 4 everyone full version
Tdd 4 everyone full versionLior Israel
 
Mendix Essentials Presentatie Gerolf Roovers26/08/2011
Mendix Essentials Presentatie Gerolf Roovers26/08/2011Mendix Essentials Presentatie Gerolf Roovers26/08/2011
Mendix Essentials Presentatie Gerolf Roovers26/08/2011Mendix
 
No silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineeringNo silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineeringArun Banotra
 
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkJoseph Yoder
 
Waterfall, Spiral and iterative model
Waterfall, Spiral and iterative modelWaterfall, Spiral and iterative model
Waterfall, Spiral and iterative modelSandesh Jonchhe
 
Software prototyping
Software prototypingSoftware prototyping
Software prototypingBirju Tank
 
Code quality as a built-in process
Code quality as a built-in processCode quality as a built-in process
Code quality as a built-in processElad Maimon
 
Some Myths in Software Development
Some Myths in Software DevelopmentSome Myths in Software Development
Some Myths in Software Developmentbryanbibat
 
Teaching Kids Programming
Teaching Kids ProgrammingTeaching Kids Programming
Teaching Kids ProgrammingLynn Langit
 
Six Steps to Conversation Driven Development
Six Steps to Conversation Driven DevelopmentSix Steps to Conversation Driven Development
Six Steps to Conversation Driven DevelopmentRasa Technologies
 
Getting Started With QA Automation
Getting Started With QA AutomationGetting Started With QA Automation
Getting Started With QA AutomationGiovanni Scerra ☃
 
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...DicodingEvent
 

What's hot (20)

Process model in Software engeneering
Process model in Software engeneering Process model in Software engeneering
Process model in Software engeneering
 
Clean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design SimpleClean Software Design: The Practices to Make The Design Simple
Clean Software Design: The Practices to Make The Design Simple
 
Global Day of Coderetreat'14 - Istanbul Event
Global Day of Coderetreat'14 - Istanbul EventGlobal Day of Coderetreat'14 - Istanbul Event
Global Day of Coderetreat'14 - Istanbul Event
 
Goals Of Software Design - The main goals
Goals Of Software Design - The main goalsGoals Of Software Design - The main goals
Goals Of Software Design - The main goals
 
Agile Development | Agile Process Models
Agile Development | Agile Process ModelsAgile Development | Agile Process Models
Agile Development | Agile Process Models
 
Tdd 4 everyone full version
Tdd 4 everyone full versionTdd 4 everyone full version
Tdd 4 everyone full version
 
SE chapter 5
SE chapter 5SE chapter 5
SE chapter 5
 
Mendix Essentials Presentatie Gerolf Roovers26/08/2011
Mendix Essentials Presentatie Gerolf Roovers26/08/2011Mendix Essentials Presentatie Gerolf Roovers26/08/2011
Mendix Essentials Presentatie Gerolf Roovers26/08/2011
 
Yogesh_Kadam
Yogesh_KadamYogesh_Kadam
Yogesh_Kadam
 
No silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineeringNo silver bullet essence and accidents of software engineering
No silver bullet essence and accidents of software engineering
 
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard WorkTaming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
Taming Big Balls of Mud with Diligence, Agile Practices, and Hard Work
 
Waterfall, Spiral and iterative model
Waterfall, Spiral and iterative modelWaterfall, Spiral and iterative model
Waterfall, Spiral and iterative model
 
Software prototyping
Software prototypingSoftware prototyping
Software prototyping
 
Code quality as a built-in process
Code quality as a built-in processCode quality as a built-in process
Code quality as a built-in process
 
Some Myths in Software Development
Some Myths in Software DevelopmentSome Myths in Software Development
Some Myths in Software Development
 
Teaching Kids Programming
Teaching Kids ProgrammingTeaching Kids Programming
Teaching Kids Programming
 
Mvp pattern
Mvp patternMvp pattern
Mvp pattern
 
Six Steps to Conversation Driven Development
Six Steps to Conversation Driven DevelopmentSix Steps to Conversation Driven Development
Six Steps to Conversation Driven Development
 
Getting Started With QA Automation
Getting Started With QA AutomationGetting Started With QA Automation
Getting Started With QA Automation
 
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
Memulai Karir menjadi iOS Developer - Gilang ramadhan (Academy Content Writer...
 

Similar to Software Design patterns on Android English

World-Class Testing Development Pipeline for Android
 World-Class Testing Development Pipeline for Android World-Class Testing Development Pipeline for Android
World-Class Testing Development Pipeline for AndroidPedro Vicente Gómez Sánchez
 
Big Ball of Mud: Software Maintenance Nightmares
Big Ball of Mud: Software Maintenance NightmaresBig Ball of Mud: Software Maintenance Nightmares
Big Ball of Mud: Software Maintenance NightmaresGonzalo Rodríguez
 
Week 5 slides
Week 5 slides Week 5 slides
Week 5 slides AinaMarini
 
Frontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonFrontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonMariya James
 
really really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesreally really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesGiulio De Donato
 
Business management application
Business management applicationBusiness management application
Business management applicationPritam Tirpude
 
Introduction to Breadcrumb
Introduction to BreadcrumbIntroduction to Breadcrumb
Introduction to BreadcrumbSophia Guevara
 
Which android app development tools deserve your attention this year
Which android app development tools deserve your attention this year  Which android app development tools deserve your attention this year
Which android app development tools deserve your attention this year Moon Technolabs Pvt. Ltd.
 
Design pattern application
Design pattern applicationDesign pattern application
Design pattern applicationgayatri thakur
 
Learn Android app development in easy steps
Learn Android app development in easy stepsLearn Android app development in easy steps
Learn Android app development in easy stepsMobile Pundits
 
Awesome application in 2014
Awesome application in 2014Awesome application in 2014
Awesome application in 2014Codemotion
 
Porting business apps to Windows Phone
Porting business apps to Windows PhonePorting business apps to Windows Phone
Porting business apps to Windows PhoneMichele Capra
 
Seminar on android app development
Seminar on android app developmentSeminar on android app development
Seminar on android app developmentAbhishekKumar4779
 

Similar to Software Design patterns on Android English (20)

Effective Android UI - English
Effective Android UI - EnglishEffective Android UI - English
Effective Android UI - English
 
Dependency injection on Android
Dependency injection on AndroidDependency injection on Android
Dependency injection on Android
 
Working with Android TV - English
Working with Android TV - EnglishWorking with Android TV - English
Working with Android TV - English
 
Kata Contacts
Kata ContactsKata Contacts
Kata Contacts
 
World-Class Testing Development Pipeline for Android
 World-Class Testing Development Pipeline for Android World-Class Testing Development Pipeline for Android
World-Class Testing Development Pipeline for Android
 
Big Ball of Mud: Software Maintenance Nightmares
Big Ball of Mud: Software Maintenance NightmaresBig Ball of Mud: Software Maintenance Nightmares
Big Ball of Mud: Software Maintenance Nightmares
 
Week 5 slides
Week 5 slides Week 5 slides
Week 5 slides
 
Frontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed ComparisonFrontend Development vs Backend Development | Detailed Comparison
Frontend Development vs Backend Development | Detailed Comparison
 
Devraj_Nataraj_CV_PDF
Devraj_Nataraj_CV_PDFDevraj_Nataraj_CV_PDF
Devraj_Nataraj_CV_PDF
 
Vrushali_Resume
Vrushali_ResumeVrushali_Resume
Vrushali_Resume
 
really really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesreally really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfaces
 
Business management application
Business management applicationBusiness management application
Business management application
 
Introduction to Breadcrumb
Introduction to BreadcrumbIntroduction to Breadcrumb
Introduction to Breadcrumb
 
Which android app development tools deserve your attention this year
Which android app development tools deserve your attention this year  Which android app development tools deserve your attention this year
Which android app development tools deserve your attention this year
 
Android crash course
Android crash courseAndroid crash course
Android crash course
 
Design pattern application
Design pattern applicationDesign pattern application
Design pattern application
 
Learn Android app development in easy steps
Learn Android app development in easy stepsLearn Android app development in easy steps
Learn Android app development in easy steps
 
Awesome application in 2014
Awesome application in 2014Awesome application in 2014
Awesome application in 2014
 
Porting business apps to Windows Phone
Porting business apps to Windows PhonePorting business apps to Windows Phone
Porting business apps to Windows Phone
 
Seminar on android app development
Seminar on android app developmentSeminar on android app development
Seminar on android app development
 

Software Design patterns on Android English

  • 1. Software Design Patterns on Android Pedro Vicente Gómez Sánchez - Mobile Software Engineer, Tuenti Technologies. @pedro_g_s , @TuentiEng pgomez@tuenti.com
  • 2. http://corporate.tuenti.com/es/dev/blog - @TuentiEng http://corporate.tuenti.com/es/jobs Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 3. Why talk about patterns when we could be at the bar? Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 4. “If I see again json parsing annotations inside a business logic domain class, please, kill me.” “If I see again other project without dependency injection, I will jump out the window.” “If I see again a class with 3000 lines of code, I will go to develop for iOS.” “If I see again a class with 3000 lines of code, I will stop working on Android and I will change to iOS.” “If I see again a project with more than 7 dependencies per class, I will go to work from Nepal.” Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Pedro V. Gómez
  • 5. ● Introduction ● Problem to solve. ● Patterns on Android ● Renderer Pattern. ● Repository Pattern. ● Command Pattern. ● Some advices. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 6. DISCLAIMER Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 7. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 8. Software Design Patterns on Android - Pedro V. Gómez Sánchez
  • 9. Software Design Patterns on Android - Pedro V. Gómez Sánchez
  • 10. What is a Software Design Pattern? Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 11. Software Design Patterns? A Software Design Pattern is a possible solution to a problem. Similar problems can be solved using a similar approach. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 12. What defines a pattern? ● Name. ● Problem to solve. ● Structure. ● Consequences. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 13. What types of patterns exists? Creation Structure Behaviour Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 14. What types of patterns exists? ● Creation: ○ Abstract Factory. ○ Builder. ○ Singleton. ● Behaviour: ○ Adapter. ○ Facade. ○ Bridge. ● Structure: ○ Command. ○ State. ○ Memento. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 15. Pros ● Each pattern is a valid and already defined solution for a concrete problem. ● Are useful when you want to reuse structures and avoid duplicity. ● Eases the extensibility of your design and accelerate long-term developments. ● Decouples your code. ● Favors change. ● Reduces maintenance efforts. ● Usually, propose a better solution for responsibility assignations. ● Poses a common vocabulary. ● Solves already solved problems by other developers. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 16. Cons ● Is not always easy to identify which pattern to apply. ● Can suppose a work overload in a short-term period. ● Apply a pattern is not always so easy as apply the structure. ● Can affect to the software performance. ● You can die by the refactoring illness if you only think in patterns. ● Apply the wrong pattern can create an anti-pattern. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 17. Problem to solve ● Show a list full of different videos. ● Video’s information will be provided by an external API. ● Video list information will be provided by an external API. ● We will show different video types: ○ Normal videos. ○ Popular videos. ○ Favorite videos. ○ Live videos. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 18. Software Design Patterns on Android Adapter Chain of Responsibility Memento Model View Controller Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 19. Patterns on Android: Adapter Name: Adapter. Problem to solve: Transform a class interface into other interface that the client code wants to use. Allows the already implemented class usage with a different interface. Our problem: Give some views to the Android SDK to be rendered inside a ListView without information about the future implementation. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 20. Patterns on Android: Adapter Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 21. Patterns on Android: Memento Name: Memento. Problem to solve: Capture and externalize the internal state of an object - keeping the encapsulation principle - and be able to restore the object state in the future. Our problem: Save the Android Activity state to recover it once the phone changes the orientation to portrait or landscape. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 22. Patterns on Android: Memento Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 23. Patterns on Android: Chain of Responsibility Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Name: Chain of Responsibility. Problem to solve: Avoid to couple the sender of a message from the receiver and be able to cut the event processing from one of the receivers. Our problem: Notify events to different receivers in the application until one of them decides to process the event.
  • 24. Patterns on Android: Chain of Responsibility Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 25. Patterns on Android: Model View Controller Name: Model View Controller. Problem to solve: Decouple the view implementation from the model implementation. Our problem: Create an application where the views implementation and the business logic model don’t be coupled. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 26. ACTIVITIES ARE NOT CONTROLLERS!!! Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 27. Problem to solve Render video information inside a ListView Data origin Use cases Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 28. Renderer Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
  • 29. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer Name: Renderer. Problem to solve: Decouple the rendering process from our adapters and avoid ViewHolder pattern. Our problem: We have different video types to render with different visual representations. Normal, favorite, popular and live videos.
  • 30. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer Structure: This pattern is a join of other three patterns. ● Builder: Separate the complex object instantiation from the object representation. With this pattern the same construction process can create different object representations. ● Delegate: Pattern applied when a class needs to delegate the implementation in other class that works as helper. ● Template Method: Define an algorithm skeleton separated by steps and define some of them in the subclasses.
  • 31. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Renderer Builder: Used to create or recycle a view in the getView method of your adapter. This is not a simple creation process because the view could be recycled in some cases.
  • 32. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Builder
  • 33. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer Template Method: Applying this pattern we can declare the main rendering algorithm and modify it, if need it, in the subclasses implementations. For example, a live video will be represented with a play button that is not able in the normal representation and we can add that part of the algorithm in the LiveVideoRenderer implementation.
  • 34. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Template method
  • 35. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer Delegate: To apply the delegate pattern we are going to delegate the recycling and rendering classic adapter implementation into the VideoRendererBuilder and into our new VideoRenderer. Renderer
  • 36. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer Delegate: With this implementation change you will see how the new implementation will be based on this schema.
  • 37. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  • 38. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Worth?
  • 39. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Resultado?
  • 40. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  • 41. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  • 42. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  • 43. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s One adapter to rule them all?
  • 44. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s https://github.com/pedrovgs/Renderers
  • 45. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Repository Name: Repository. Problem to solve: Abstract the data origin in a system with different possible data sources. Our problem: Abstract the data origin. I don’t care about where the data comes from. A memory storage system, a database or an external API accessed using an API client are just an implementation detail.
  • 46. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Repository Different repository implementations can offer different benefits. The idea is to abstract all the client code to the data origin and be able to change the repository implementation as easy as possible.
  • 47. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Why?
  • 48. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Because I don’t care if the data comes from an external API or a local DB!!! That’s an implementation detail.
  • 49. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Repository
  • 50. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Repository
  • 51. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Command Name: Command. Problem to solve: Encapsulate a message like an object being able to parametrize the clients with different requests, add that messages into a queue and support undo/redo mechanism. Our problem: Model our use cases abstracting the execution process from itself and abstracting the delivery mechanism code from our model.
  • 52. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Command Use case
  • 53. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Command Your model should be between the interactor and the repository. An interator doesn’t implements business logic, only use our model to stimulate it and delegate the implementation to the business logic domain. This is just an example, I’m not going to desing all the business model xD
  • 54. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Some advices ● Implement one model by domain. ● Avoid code duplicity. ● Refactor day by day. ● Apply the single responsibility principle. ● Defer the important decisions. ● Think into the framework like a delivery mechanism, not the architecture of your application. ● Work from the use case. ● Create testable code. ● Write code for your mates not for the machine.
  • 55. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Questions?
  • 56. http://corporate.tuenti.com/es/dev/blog - @TuentiEng http://corporate.tuenti.com/es/jobs Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s