Home > Software design >  link a class with another class
link a class with another class

Time:06-07

Hello I am a student and I have a problem. I have two classes:

class User{
String uid;
String name;
byte[] upicture;
String pversion ;
public User(String uid,
           String name,
               byte[] upicture, String pversion ){
this.uid=uid;
       this.name=name;
       this.upicture=upicture;
       this.pversion=pversion;
}
....

I have a Model_user.class with ArrayList:

ublic class Model_user {
   private static Model_user theinstance = null;
   private ArrayList<User> user_list;
   public static synchronized Model_user getInstance() {
       if (theinstance == null) {
           theinstance = new Model_user();
       }

and a class Post:

class Post{
String comment;
String author;
Post(String comment, String author) {
        this.comment = comment;
        this.author = author;
    }
....

I have a Model_post.class with ArrayList:

ublic class Model_user {
   private static Model_user theinstance = null;
   private ArrayList<Post> user_list;
   public static synchronized Model_post getInstance() {
       if (theinstance == null) {
           theinstance = new Model_post();
       }

how can i link a post to a user? how can i view posts together with users? can I create two arrays in the Model_post, one for posts and one for users?can i use ArrayList<User,Post>?

CodePudding user response:

One approach in your example would be to make User a property of Post:

class Post{
    String comment;
    User author;
    Post(String comment, User author) {
        this.comment = comment;
        this.author = author;
}
  • Related