简体   繁体   中英

How can I use same methods in different classes type ArrayList?

I'm doing a project that should be stored in two differents text files. Let say I have 2 classes Person and Activity, each with only these attributes in common: id and isActive. But there are also many that are not common.

Also I have 2 classes ArrayList type:

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 see both classes type ArrayList RegitryPerson and RegistryActivigy have same methods, but some used different kind of object.

I just don't wanna have almost same code in differents classes. Can I use an interface or abstract class? and most important, How can implement that?. Or I am complicating everything?

Thanks.

You can follow Program to an interface, not implementations design principle here.

Create an interface say Entity that would be implemented by both Person and Activity

Entity.java

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

This interface would be implemented by both Person and 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) {
        ...
    }
    ...
}

Now create a parent Class 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
}

Now you can extend this Registry class to both of your implementations, ie, RegistryPerson and RegistryActivity

RegistryPerson.java

public class RegistryPerson extends Registry {
    ..
}

RegistryActivity.java

public class RegistryActivity extends Registry {
    ..
}

PS: All of the classes listed above many contain more common methods. This is just to give you basic introduction to this design principle.

I had the same idea as rD but used generics to create the registry. I also moved the List inside the class. Methods to read from file and add the registry could be handled by other classes. I try to avoid inheritance and abstract classes at all costs. Interfaces are generics are quite powerful.

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<>();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM