Implement a single Java class/interface, which ref

2019-07-21 21:20发布

Problem Statement: We have multiple class files (~200+), which each implement different methods and some use the methods from each other also to implement their own methods.

We wish to call multiple of these class methods (~20-30) in our code and have them deliver functionality like writing to a UI Web page and reading from a web page.

As we wish to make this process simple, we want a single class/interface which contains all these multiple class references (~200+), and we just need to import/include this single class/interface in our code and then using it to reference all the methods in the class files.

Question: Is the above possible? If so, how - by using a class or interface or something else?

I have provided examples and information of what we have tried here: Implement multiple classes in the same interface in Java?

Basically, we have tried

  1. Creating a Class which has all the other class declared in it
  2. Creating an interface, which has implementations across the multiple classes (the ~200+ mentioned above)
  3. Implemented a class and an interface

Nope of the ways seem to work.

As a gist of what we want in code, have provided an example below.

Example (this is not the right code or implementation, just wanted to provide an example of what we want to do in code):

I have 2 classes called "ABC_FamilyGivenName" & "ABC_DOBGender". I have created an interface "Common".

I want to use the methods in both the above classes in my code in class "Application".

With the current implementation, Java wants me to add an @Override to both the ABC_FamilyGivenName & ABC_DOBGender. This again creates an overhead, if we have ~500 such methods, and will thus need to override all ~500 in each of the c~200 class files??

***INTERFACE***:

public interface Common {
    void ABC_GivenNames();
    void ABC_FamilyNames();
    void ABC_Gender();
    void ABC_BirthDay();
}



***IMPLEMENTATION CLASSES***:

**ABC_FamilyGivenName**
public class ABC_FamilyGivenName extends Base implements Common {

    public void ABC_GivenNames(){
        // Implementation code
    }

    public void ABC_FamilyNames(){
        // Implementation code
    }
}

**ABC_DOBGender**
public class ABC_DOBGender extends Base implements Common {

    public void ABC_Gender(){
        // Implementation code
    }

    public void ABC_BirthDay(){
        // Implementation code
    }
}



**USE IMPLEMENTED CLASS IN CODE**:
public class Application extends Base {

    Common commonClass = new ABC_FamilyGivenName();
    /* *DO I NEED THIS? I THINK I DO, BUT CODE/JAVA SAYS I DO NOT*
     * Common commonClass = new ABC_DOBGender();
     */

    public void ELP_C0050_PassportDetails(){
        commonClass.ABC_GivenNames();
        commonClass.ABC_FamilyNames();
        commonClass.ABC_DOB();
        commonClass.ABC_Gender();
    }
}

1条回答
Anthone
2楼-- · 2019-07-21 22:15

So there is a lot more you can do with this, but this is a very simple example of what I was talking about. Instead of having an interface over them, plug them in.

//Component Classes

public class DOBGender {//Has the specific methods you want

}



public class FamilyGivenName {//Has the specific methods you want

}

//Then just plug them in to this class

public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person(DOBGender dobGender, FamilyGivenName fgn) {//Plug them in
        this.dobGender = dobGender;
        this.fgn = fgn;
    }

    public DOBGender getDobGender() {
        return dobGender;
    }

    public void setDobGender(DOBGender dobGender) {
        this.dobGender = dobGender;
    }

    public FamilyGivenName getFgn() {
        return fgn;
    }

    public void setFgn(FamilyGivenName fgn) {
        this.fgn = fgn;
    }
}

Like I said previously, This can be made in a much more complex way, but would also make coding of so many classes slightly easier for a user. If this is not the route you wanted to go, I can delete this one and take a new approach. If you want me to extend upon this idea, I can do that too.

I hope this helps.

EDIT:

This is what I am referring to if you just want to call Person and start using the component methods.

    public class Person {

    DOBGender dobGender;
    FamilyGivenName fgn;

    public Person() {
        dobGender = new DOBGender();
        fgn = new FamilyGivenName();
    }

    public DOBGender getDobGender() {
        return dobGender;
    }


    public FamilyGivenName getFgn() {
        return fgn;
    }

}
查看更多
登录 后发表回答