0
\$\begingroup\$

I have wrote a small project for Android. It's an app which lets you to take a photo (or find one with Flickr) and save it in the cloud (Cloudinary).

Link to the GitHub repository.

Should MVP layers be completely separated?

In the repository layer I have two DbPhoto classes (one for local database [SQLite] and one for cloud database [Firebase]), that are converted to Photo class in model. Photo class is used also in View. Should View has its own class?

DbPhoto for local database:

package org.chyla.photoapp.Repository.LocalDatabase.GreenDao.detail;

import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.ToOne;

@Entity(
        indexes = {
                @Index(value = "dbUserId,title,description,url", unique = true)
        }
)
public class DbPhoto {

    @Id
    private Long id;

    @NotNull
    private Long dbUserId;

    @ToOne(joinProperty = "dbUserId")
    private DbUser dbUser;

    @NotNull
    private String title;

    @NotNull
    private String description;

    @NotNull
    private String url;

/** Used to resolve relations */
@Generated(hash = 2040040024)
private transient DaoSession daoSession;

/** Used for active entity operations. */
@Generated(hash = 1256174471)
private transient DbPhotoDao myDao;

@Generated(hash = 1342142122)
public DbPhoto(Long id, @NotNull Long dbUserId, @NotNull String title,
        @NotNull String description, @NotNull String url) {
    this.id = id;
    this.dbUserId = dbUserId;
    this.title = title;
    this.description = description;
    this.url = url;
}

@Generated(hash = 95134667)
public DbPhoto() {
}

public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getUrl() {
    return this.url;
}

public void setUrl(String url) {
    this.url = url;
}

// ... (generated functions)

DbPhoto for cloud database:

package org.chyla.photoapp.Repository.CloudDatabase.Firebase.detail;

public class DbPhoto {

    public String title;
    public String description;
    public String url;

    public DbPhoto() {
    }

    public DbPhoto(String title, String description, String url) {
        this.title = title;
        this.description = description;
        this.url = url;
    }

}

Photo class for model and view:

package org.chyla.photoapp.Main.Model.objects;

import android.support.annotation.Nullable;

import java.net.URL;

public class Photo {

    private final String title;
    private final String description;
    private final URL url;

    public Photo(String title, @Nullable String description, URL url) {
        this.title = title;
        this.description = description;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        if (description == null) {
            return "";
        }
        else {
            return description;
        }
    }

    public URL getUrl() {
        return url;
    }

}

I'm open to every suggestion improvements for my project.

\$\endgroup\$
1
\$\begingroup\$

Default description

public String getDescription() {
    if (description == null) {
        return "";
    }
    else {
        return description;
    }
}

Depending on how this method is used, it may make sense to take in a default value here, and return the default if the field is null, this may simplify code using this class.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ What about the responsibilities of that method? Why method that gets something should have parameters - is this second responsibility? Could you provide any example of this practice to see it "in action"? \$\endgroup\$ – Adam Sep 11 '17 at 20:25
  • \$\begingroup\$ For validating you can check getDescription(null) == null, whereas for displaying getDescription("No description") would assure there is a default \$\endgroup\$ – jrtapsell Sep 11 '17 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.