Successfully reported this slideshow.

Software Design patterns on Android English

140

Share

Loading in …3
×
1 of 56
1 of 56

More Related Content

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Software Design patterns on Android English

  1. 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. 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. 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. 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. 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. 6. DISCLAIMER Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  7. 7. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  8. 8. Software Design Patterns on Android - Pedro V. Gómez Sánchez
  9. 9. Software Design Patterns on Android - Pedro V. Gómez Sánchez
  10. 10. What is a Software Design Pattern? Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  11. 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. 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. 13. What types of patterns exists? Creation Structure Behaviour Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  14. 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. 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. 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. 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. 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. 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. 20. Patterns on Android: Adapter Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  21. 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. 22. Patterns on Android: Memento Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  23. 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. 24. Patterns on Android: Chain of Responsibility Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  25. 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. 26. ACTIVITIES ARE NOT CONTROLLERS!!! Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  27. 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. 28. Renderer Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com
  29. 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. 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. 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. 32. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Builder
  33. 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. 34. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Template method
  35. 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. 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. 37. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  38. 38. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Worth?
  39. 39. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Resultado?
  40. 40. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s
  41. 41. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  42. 42. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Renderer
  43. 43. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s One adapter to rule them all?
  44. 44. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s https://github.com/pedrovgs/Renderers
  45. 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. 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. 47. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Why?
  48. 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. 49. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Repository
  50. 50. Software Design Patterns on Android - Pedro V. Gómez Sánchez - pgomez@tuenti.com Repository
  51. 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. 52. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Command Use case
  53. 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. 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. 55. Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s Questions?
  56. 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

×