4. "general reusable solution to a commonly occurring problems within
a given context in software design."
What is Design Pattern?
5. ● A design pattern works best when it is
used as a common language in your
team.
● reduced maintenance efforts
● Eases the extensibility of your design
and accelerate long-term
developments.
Why do we need them?
6. ● a class should have only one responsibility.
● use interfaces to achieve dependency inversion properly.
● Don’t Repeat Yourself - avoids duplication in code
some design principles we should mention...
7. ● static factory method
● singleton
● viewholder
● observer
● mvp
What I care most ...
9. public static MyFragment newInstance(int index)
{
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
code
10. ● Android supports it!
● prevent us to have multiple instance of an object.
SingleTon
11. ● static member
This contains the instance of the singleton class.
● private constructor
This will prevent anybody else to instantiate class.
● static public method
This provides the global point of access to the Singleton object and
returns the instance to the client calling class
What do you need?
12. public class Singleton {
private static Singleton mInstance = null;
private Singleton(){
}
public static Singleton getInstance(){
if(mInstance == null)
{
mInstance = new Singleton();
}
return mInstance;
}
code
14. when one object changes state, all of its dependents are notified
and updated automatically.
observer
15. Adapter pattern helps us to translate one interface to a compatible
one.
Adapter Pattern
16. everything about how the interface works is separated from how
we represent it on screen.
mvp
17.
18. Model is a data access layer such as database API or remote server
API.
mvp
19. The view, usually implemented by an Activity (it may be a
Fragment, a View… depending on how the app is structured), will
contain a reference to the presenter.
mvp
20. ● The presenter is responsible to act as the middle man between
view and model. It retrieves data from the model and returns it
formatted to the view.
● unlike the typical MVC, it also decides what happens when you
interact with the view.
mvp
21. In an application with a good layered architecture, this model
would only be the gateway to the domain layer or business logic.
mvp