patternjavaModerate
User search implementation
Viewed 0 times
userimplementationsearch
Problem
In my Java code I have these three classes. Basically I have a User class, and also a GUI class that holds
The
I noticed that both the user class and the
I don't think the way I structured this is good for Java programming practices. I think some sort of encapsulation or something will be better but I'm not sure what I can do here.
I need the user object and GUI object to also be separate for modularity purposes.
Does anyone see a better way of writing the below code?
BaseUser.java
User.java
```
package sord.object;
import java.util.Date;
import android.content.Context;
import sord.common.DateTimeFunctions;
public class User extends BaseUser {
private String password;
private String phoneNumber;
private String websiteLink;
private String statusText;
private String bioText;
private Date dateJoined;
private Date dateLastActive;
public User(int userid, String firstName, String lastName,
String password, String emailAddress, String phoneNumber,
String websiteLink, String statusText, String bioText, int roleId,
String dateJoined, String dateLastActive, int active,
String picURL, Context context) {
this.userid = userid;
this.firstname = firstName;
this.lastname = lastName;
this.password = password;
this.emailaddress = emailAddress;
this.phoneNumber = phoneNumber;
this.websiteLink = websiteLink;
this.statusText = statusTex
TextViews and other things (GUI related) for the properties that show on the screen.The
SearchUser is just a object that has the GUI properties. One of these per user object.I noticed that both the user class and the
SearchUser class share the same properties, so I took them out and made a BaseUser class, and then extended it to have that.I don't think the way I structured this is good for Java programming practices. I think some sort of encapsulation or something will be better but I'm not sure what I can do here.
I need the user object and GUI object to also be separate for modularity purposes.
Does anyone see a better way of writing the below code?
BaseUser.java
package sord.object;
public class BaseUser {
int userid;
String firstname;
String lastname;
String emailaddress;
int roleId;
Boolean active;
String picURL;
}User.java
```
package sord.object;
import java.util.Date;
import android.content.Context;
import sord.common.DateTimeFunctions;
public class User extends BaseUser {
private String password;
private String phoneNumber;
private String websiteLink;
private String statusText;
private String bioText;
private Date dateJoined;
private Date dateLastActive;
public User(int userid, String firstName, String lastName,
String password, String emailAddress, String phoneNumber,
String websiteLink, String statusText, String bioText, int roleId,
String dateJoined, String dateLastActive, int active,
String picURL, Context context) {
this.userid = userid;
this.firstname = firstName;
this.lastname = lastName;
this.password = password;
this.emailaddress = emailAddress;
this.phoneNumber = phoneNumber;
this.websiteLink = websiteLink;
this.statusText = statusTex
Solution
Designing is art we learn from experience. Object oriented paradigm is "natural" way to explain things.
SearchUser extending BaseUser is wrong naturally, isn't it ? (because SearchUser is not special type of user so can't be subclass) You can have has-a-relation ship there (i.e. SearchUser can have member which is object BaseUser )
BaseUser variables should be private; use methods to access them ( if and when required ).
can be rewritten as
Always program through interface; so later you can change your mind with minimal changes.
Design is something you always do on white paper and change many times.
SearchUser extending BaseUser is wrong naturally, isn't it ? (because SearchUser is not special type of user so can't be subclass) You can have has-a-relation ship there (i.e. SearchUser can have member which is object BaseUser )
BaseUser variables should be private; use methods to access them ( if and when required ).
private ArrayList placementList = new ArrayListcan be rewritten as
private List placementList = new ArrayListAlways program through interface; so later you can change your mind with minimal changes.
Design is something you always do on white paper and change many times.
Code Snippets
private ArrayList placementList = new ArrayList<PlacementGUI>private List<PlacementGUI> placementList = new ArrayList<PlacementGUI>Context
StackExchange Code Review Q#43767, answer score: 10
Revisions (0)
No revisions yet.