Software Design patterns on Android English

56
Software Design Patterns on Android Pedro Vicente Gómez Sánchez - Mobile Software Engineer, Tuenti Technologies. @pedro_g_s , @TuentiEng [email protected]

description

Softaware Design Patterns on Android - English version.

Transcript of Software Design patterns on Android English

Page 1: Software Design patterns on Android English

Software Design Patterns on AndroidPedro Vicente Gómez Sánchez - Mobile Software Engineer, Tuenti Technologies.

@pedro_g_s , @TuentiEng

[email protected]

Page 2: Software Design patterns on Android English

http://corporate.tuenti.com/es/dev/blog - @TuentiEnghttp://corporate.tuenti.com/es/jobs

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 3: Software Design patterns on Android English

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

Page 4: Software Design patterns on Android English

“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

Page 5: Software Design patterns on Android English

● 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

Page 6: Software Design patterns on Android English

DISCLAIMER

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 7: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 8: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez

Page 9: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez

Page 10: Software Design patterns on Android English

What is a Software Design Pattern?

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 11: Software Design patterns on Android English

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

Page 12: Software Design patterns on Android English

What defines a pattern?

● Name.● Problem to solve.● Structure.● Consequences.

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 13: Software Design patterns on Android English

What types of patterns exists?

Creation Structure

Behaviour

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 14: Software Design patterns on Android English

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

Page 15: Software Design patterns on Android English

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

Page 16: Software Design patterns on Android English

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

Page 17: Software Design patterns on Android English

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

Page 18: Software Design patterns on Android English

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

Page 19: Software Design patterns on Android English

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

Page 20: Software Design patterns on Android English

Patterns on Android: Adapter

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 21: Software Design patterns on Android English

Patterns on Android: MementoName: 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

Page 22: Software Design patterns on Android English

Patterns on Android: Memento

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 23: Software Design patterns on Android English

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.

Page 24: Software Design patterns on Android English

Patterns on Android: Chain of Responsibility

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 25: Software Design patterns on Android English

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

Page 26: Software Design patterns on Android English

ACTIVITIES ARE NOT CONTROLLERS!!!

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 27: Software Design patterns on Android English

Problem to solveRender video

information inside a ListView

Data origin Use cases

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 28: Software Design patterns on Android English

Renderer

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

Page 29: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

RendererName: 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.

Page 30: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

RendererStructure: 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.

Page 31: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

RendererBuilder: 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.

Page 32: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

Builder

Page 33: Software Design patterns on Android English

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.

Page 34: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Template method

Page 35: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

RendererDelegate: 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

Page 36: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

RendererDelegate: With this implementation change you will see how the new implementation will be based on this schema.

Page 37: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Renderer

Page 38: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Worth?

Page 39: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Resultado?

Page 40: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Page 41: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Renderer

Page 42: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Renderer

Page 43: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

One adapter to rule them all?

Page 44: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

https://github.com/pedrovgs/Renderers

Page 45: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

RepositoryName: 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.

Page 46: Software Design patterns on Android English

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.

Page 47: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Why?

Page 48: Software Design patterns on Android English

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.

Page 49: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

Repository

Page 50: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

Repository

Page 51: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

CommandName: 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.

Page 52: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Command Use case

Page 53: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

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

Page 54: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - [email protected]

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.

Page 55: Software Design patterns on Android English

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s

Questions?

Page 56: Software Design patterns on Android English

http://corporate.tuenti.com/es/dev/blog - @TuentiEnghttp://corporate.tuenti.com/es/jobs

Software Design Patterns on Android - Pedro V. Gómez Sánchez - @pedro_g_s