How can I use the same methods in different classes like ArrayList?

I am making a project that should be stored in two different text files. Let's say I have 2 classes Person and Activity, each of which contains only these attributes: id and isActive. But there are many who are not common.

I also have 2 types of ArrayList:

public class RegistryPerson extends ArrayList<Person> {
     public void add(Person obj){
              ....
     }
     public boolean isDuplicate(Person obj){
         for(Person p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Person search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfilePerson);
     } 
     public void activate(Person obj){
              obj.setActivate;
     }
     //more methods
}

.

public class RegistryActivity extends ArrayList<Activity> {
     public void add(Activity obj){
              ....
     }
     public boolean isDuplicate(Activity obj){
         for(Activity p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Activity search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfileActivity);
     } 
     public void activate(Activity obj){
              obj.setActivate;
     }
     //more methods
}

Both classes have the same methods.

As you can see, both classes of type ArrayList RegitryPerson and RegistryActivigy have the same methods, but some use a different type of object.

I just don't want to have almost the same code in different classes. Can I use an interface or an abstract class? and most importantly, how to implement this ?. Or am I complicating things?

Thank.

+4
2

, .

interface say Entity, Person, Activity

Entity.java

public interface Entity {
    public Boolean equals(Entity e);
    //other common methods
}

Person, Activity

Person.java

public class Person implements Entity {
    ...
    @Override
    public boolean equals(Entity e) {
        ...
    }
    ...
}

Activity.java

public class Activity implements Entity {
    ...
    @Override
    public boolean equals(Entity e) {
        ...
    }
    ...
}

Registry

Registry.java

public class Registry extends ArrayList<Entity> {
     public void add(Entity obj){
              ....
     }
     public boolean isDuplicate(Entity obj){
         for(Entity p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Entity search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfilePerson);
     } 
     public void activate(Entity obj){
              obj.setActivate;
     }
     //more methods
}

, .. RegistryPerson RegistryActivity

RegistryPerson.java

public class RegistryPerson extends Registry {
    ..
}

RegistryActivity.java

public class RegistryActivity extends Registry {
    ..
}

P.S.: . , .

+3

, rD, . . . . .

interface IdObject {
    int getId();
}

class Registry<T extends IdObject> {
    private List<T> list = new ArrayList<T>();

    public void add(T obj){
        list.add(obj);
    }

    public boolean isDuplicate(T obj){
        for(T t: list){
            if(obj.equals(t)){
                return true;
            }
        }
        return false;
    }
    public T search(int id){
        for(T t: list){
            if(t.getId() == id)){
                return t;
            }
        }
        return null;
    }
}

class Example {
    Registery<Person> personRegistery = new Registry<>();
    Registery<Activity> activityRegistery = new Registry<>();
}
+2

Source: https://habr.com/ru/post/1651478/


All Articles