an implementation of repository pattern for mobile application
Transcript of an implementation of repository pattern for mobile application
Arif Akbarul HudaCoding | Writing | Sharing2013-now QISCUS
Twitter : @omayibMedium : www.medium.com/@omayib
“Repository Pattern for Mobile Applicaiton”
#Techtalk99
High quality software(ISO 9126-1 software quality model)
*Ignore the Portability factor
Maintainable
“Maintainability is impacted by code readability or complexity as well as modularization” ..
“Also the ability to verify (or test) a system, i.e. testability, is one of the subcharacteristics of maintainability. “
S.O.L.I.D.
● Single Responsibility● Open extention closed modificaiton● Liskov substitution principle● Interface Segregation● Dependency inversion
The real case
User can see the list of alumnus
On your activity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alumni_list);
// load the data directly from server OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://10.0.2.2:3000/alumnus").build();
client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); }
@Override public void onResponse(Call call, Response response) throws IOException { } }); }
Over Request Worked!
I can put a Local Database
Public void onCreate(){ //… ArrayList<Person> alumnus = loadDataFromLocal(); if (alumnus.isEmpty()){ loadDataFromServer(); }}
private ArrayList<Person> loadDataFromLocal(){ RealmResults<PersonPersistance> personPersistances = realm.where(PersonPersistance.class).findAll(); ArrayList<Person> alumnus = new ArrayList<>(); for (int i = 0; i < personPersistances.size(); i++) { PersonPersistance item = personPersistances.get(i); alumnus.add(new Person(item.getId(),item.getName(),item.getEmail(),item.getJob())); } return alumnus; }
private void loadDataFromServer() { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://10.0.2.2:3000/alumnus").build();
client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); }
@Override public void onResponse(Call call, Response response) throws IOException { } }); }
Managing data from local database
Managing data from local remote
Managing data from local cache
Managing view’s state
Managing activitie’s lifecycle
Breakfast with “spaghetti code”
Introducing a Repository Pattern
repositoryManager.loadAll()repositoryManager.save(..) RepositoryManager
CachedRepository
LocalRepository
RemoteRepository
Go to Android StudioGo to Swift Playground
https://github.com/omayib/SocialAlumni/tree/repository
https://github.com/omayib/SwiftRepositoryPattern