简体   繁体   English

Java中调用接口的具体实现类

[英]Calling specific implementation classes of an Interface in Java

I am trying to build a simple API, where I have an interface AnimalService and its implementation classes are LionImpl , TigerImpl , ElephantImpl .我正在尝试构建一个简单的 API,其中我有一个AnimalService接口,它的实现类是LionImplTigerImplElephantImpl
AnimalService has a method getHome() . AnimalService有一个方法getHome()
And I have a properties file which contains the type of animal I'm using like,我有一个属性文件,其中包含我正在使用的动物类型,

animal=lion

So based on the animal type I am using, when I call my API ( getHome() from AnimalService ), the particular implementation class's getHome() method should be executed.因此,基于我现在用的,当我(叫我的API动物类型getHome()AnimalService ),具体的实现类的getHome()方法应该被执行。

How can I achieve that?我怎样才能做到这一点?

Thanks in advance.提前致谢。

You can achieve this by creating a Factory class that houses an enum eg您可以通过创建一个包含枚举的 Factory 类来实现这一点,例如

public static AnimalServiceFactory(){

    public static AnimalService getInstance() { // you can choose to pass here the implmentation string or just do inside this class
        // read the properties file and get the implementation value e.g. lion
        final String result = // result from properties
        // get the implementation value from the enum
        return AnimalType.getImpl(result);
    }

    enum AnimalType {
        LION(new LionImpl()), TIGER(new TigerImpl()), etc, etc;

        AnimalService getImpl(String propertyValue) {
            // find the propertyValue and return the implementation
        }
    }
}

This is a high level code, not tested for syntax error etc.这是一个高级代码,未测试语法错误等。

You are describing how Java polymorphism works.您正在描述Java 多态性的工作原理。 Here is some code that corresponds to your description:以下是与您的描述相对应的一些代码:

AnimalService.java

public interface AnimalService {
    String getHome();
}

ElephantImpl.java

public class ElephantImpl implements AnimalService {
    public String getHome() {
        return "Elephant home";
    }
}

LionImpl.java

public class LionImpl implements AnimalService {
    public String getHome() {
        return "Lion home";
    }
}

TigerImpl.java

public class TigerImpl implements AnimalService {
    public String getHome() {
        return "Tiger home";
    }
}

PolyFun.java

public class PolyFun {
    public static void main(String[] args) {
        AnimalService animalService = null;

        // there are many ways to do this:
        String animal = "lion";
        if (animal.compareToIgnoreCase("lion")==0)
            animalService = new LionImpl();
        else if (animal.compareToIgnoreCase("tiger")==0)
            animalService = new TigerImpl();
        else if (animal.compareToIgnoreCase("elephant")==0)
            animalService = new ElephantImpl();

        assert animalService != null;
        System.out.println("Home=" + animalService.getHome());
    }
}

For more information, see https://www.geeksforgeeks.org/dynamic-method-dispatch-runtime-polymorphism-java/有关更多信息,请参阅https://www.geeksforgeeks.org/dynamic-method-dispatch-runtime-polymorphism-java/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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