I'm developing an Android application and it requires a several steps registration process. Because the amount of data I need to collect is big I have create one activity and a different fragment for each step. For example in the first step/fragment the user has to provide basic information (name, gender, email), in the second step/fragment education information (university, degree, dates attended) and in the final step/fragment experience information (companies, title, dates). As soon as the user completes correctly all steps, I send all the information in a single JSON request to my web service.
My question is: what would be the design or design pattern that better models this problem in an elegant, readable and testable way?. The approach I currently use, which I consider is not good at all and potentially can create memory leaks in the app is this:
class Registration {
// Contains basic user information such as name, gender, etc
private User user;
// Each Education instance contains info such as university, degree, etc
private List<Education> studies;
// Each Experience instance contains info such as company name, title, etc
private List<Experience> workExperience;
private static Registration currentRegistration;
public static Registration getCurrent() {
if (currentRegistration == null) {
currentRegistration = new Registration();
}
return currentRegistration;
}
/* There are getter and setter methods for each field */
}
As you can see I have created the Registration class with the required fields and a static reference instance that represents the current registration which is provided through the getCurrent method to each fragment which call it from its onCreate method and set information of that step thought the corresponding setter method. When the user has entered all data I convert this POJO into JSON and make the network request.
Another approach could be to pass the Registration instance from fragment A to B for which I'd need to implement the serializable interface but I do not like this approach.
The Builder and JavaBean patterns have come to my mind but I see them more appropriate when I have to initiliase the object in a single block statements. Any recommendation about how can I model this problem properly? :)
Aucun commentaire:
Enregistrer un commentaire