![Page 1: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/1.jpg)
Arif Akbarul HudaCoding | Writing | Sharing2013-now QISCUS
Twitter : @omayibMedium : www.medium.com/@omayib
“Repository Pattern for Mobile Applicaiton”
#Techtalk99
![Page 2: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/2.jpg)
High quality software(ISO 9126-1 software quality model)
*Ignore the Portability factor
![Page 3: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/3.jpg)
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. “
![Page 4: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/4.jpg)
![Page 5: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/5.jpg)
S.O.L.I.D.
● Single Responsibility● Open extention closed modificaiton● Liskov substitution principle● Interface Segregation● Dependency inversion
![Page 6: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/6.jpg)
The real case
![Page 7: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/7.jpg)
User can see the list of alumnus
![Page 8: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/8.jpg)
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 { } }); }
![Page 9: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/9.jpg)
Over Request Worked!
![Page 10: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/10.jpg)
I can put a Local Database
![Page 11: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/11.jpg)
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 { } }); }
![Page 12: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/12.jpg)
Managing data from local database
Managing data from local remote
Managing data from local cache
Managing view’s state
Managing activitie’s lifecycle
![Page 13: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/13.jpg)
Breakfast with “spaghetti code”
![Page 14: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/14.jpg)
Introducing a Repository Pattern
repositoryManager.loadAll()repositoryManager.save(..) RepositoryManager
CachedRepository
LocalRepository
RemoteRepository
![Page 15: an implementation of repository pattern for mobile application](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a688e077f8b9a4a258b5ed3/html5/thumbnails/15.jpg)
Go to Android StudioGo to Swift Playground
https://github.com/omayib/SocialAlumni/tree/repository
https://github.com/omayib/SwiftRepositoryPattern