lundi 4 octobre 2021

User class and signleton in andorid

I am working on a social app (it's just a learning project) and now I am having a hard time with my user class. I used the singleton pattern for my user class because I thought that can be useful access to the logged user's info from any activity but in this way how I can represent my user's friend? I've come up with this solution but I am not sure that this is the best way, can someone give me some advice? Thanks in advance

public class User {
    private static User user;
    private String email;
    //other attribute....
    private List<User> friends;
    private User(String name, String surname, String birthdate, String thumbnail, String email) {
        super(name, surname, birthdate, thumbnail);
        this.email = email;
        this.friends = new ArrayList<>();
    }
    public void initUser(String name, String surname, String birthdate,
                         String thumbnail, String email) {
        user = new User(name,surname,birthdate,thumbnail,email);

    }
    public static User getInstance(){
        if(user == null)
            user = new User();
        return user;
    }
    public User getFriend(String email){
        for (User u : friends)
            if (u.getEmail().equals(email))
                return u;
        return null;
    }
    public void addFriend(String name, String surname, String birthdate,
                          String thumbnail, String email){
        friends.add(new User(name,surname,birthdate,thumbnail,email));
    }
}

Aucun commentaire:

Enregistrer un commentaire