简体   繁体   中英

Java interface access different classes by calling same interface

I want to use java interface in a way that i will make a call defining interface in my other class like 'private SoapURL soapURL;' and than i can access any class's method for example : i want to use login:-

private SoapURL soapURL;
SoapUrl = LoginSoap ();

String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();

Is there any way to do something like this. I am sorry i am not very good with Object Oriented principles but if there is a solution for my problem i would like to know it. Thanks in advance.

public interface SoapURL {
    public String getNameSpace();
    public String getUrl();
    public String getSoapAction();
    public String getMethodName();
    public String getTag();
}

LoginSoap class

public class LoginSoap implements SoapURL {

    @Override
    public String getNameSpace() {
        return "https://host.com/MobileWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/MobileWFC/MobileWS.asmx";
    }

    @Override
    public String getSoapAction() {
        return "https://host.com/MobileWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }

    @Override
    public String getTag() {
        return "Login Activity";
    }
}

SignUpSoap class

public class SignUpSoap implements SoapURL {

    @Override
    public String getNameSpace() {
        return "https://host.com/MobileWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/MobileWFC/MobileWS.asmx";
    }

    @Override
    public String getSoapAction() {
        return "https://host.com/MobileWFC/UserRegister";
    }

    @Override
    public String getMethodName() {
        return "UserRegister";
    }

    @Override
    public String getTag() {
        return "SignUp Activity";
    }
}

ResetPasswordSoap class

public class ResetPasswordSoap implements SoapURL {

    @Override
    public String getNameSpace() {
        return "https://host.com/MobileWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/MobileWFC/MobileWS.asmx";
    }

    @Override
    public String getSoapAction() {
        return "https://host.com/MobileWFC/UserPasswordReset";
    }

    @Override
    public String getMethodName() {
        return "UserPasswordReset";
    }

    @Override
    public String getTag() {
        return "Forget Password Activity";
    }
}

Just do, for example:

SoapURL example = new LoginSoap();
String a = example.getTag();

a should be equal to "Login Activity"

Your implementation looks correct. To make use of it, you can do this in main:

SoapURL reset = new ResetPasswordSoap();
System.out.println(reset.getUrl());

This is a method of minimizing coupling in large systems. And reduces dependency between objects by making use of a common interface for groups of objects that work together. You might be new at Object oriented principles, but you are one step ahead of the game already

To pass it to a function, you do:

public JPanel resetPass(SoapURL reset) {
    ...
}

// In main:
JPanel resetPassPanel = resetPass(reset);

The main use of Interface is polymorphism , or the ability to perform the same operation on a number of different objects, which is exactly what you wanted in your scenario

Your approach is absolutely fine , just a modification needed

private SoapURL soapURL;
//SoapUrl = LoginSoap (); // This line should be replaced with the Below line
soapURL=new LoginSoap();

String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();

Since LoginSoap , SignUpSoap , ResetPasswordSoap classes are implemented classes of SoapURL Interface , thus reference variable of SoapURL can store Object of any of these child classes

soapURL=new LoginSoap();//soapURL.someMethod will call method of LoginSoapClass
soapURL=new SignUpSoap();// will call method of SignUpSoap class
soapURL=new ResetPasswordSoap();

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