简体   繁体   English

从枚举(Java)实例化/返回时的类型安全接口

[英]Type-safe interface when instantiated/returned from enum (Java)

Suppose we have an Automobile object:假设我们有一个汽车对象:

public interface Automobile {
}

We also have a couple of different cars (is-a), one with an auto transmission and one with a manual transmission:我们还有几款不同的汽车(is-a),一款配备自动变速箱,一款配备手动变速箱:

public class FordExplorer implements Autmomobile {
    public static void shiftIntoPark() {
    }
}

public class Porsche911 implements Autmomobile {
    public static void shiftInto1stGear() {
    }
}

Now, let's say we have an enum Garage, which stores these Automobiles:现在,假设我们有一个 enum Garage,它存储这些汽车:

public enum Garage {
    FORD {
        public Automobile getCar() {
            return new FordExplorer();
        }
    }, PORSCHE {
        public Automobile getCar() {
            return new Porsche911();
        }
    };

    abstract public Automobile getCar();
    }

}

Is there a "best practice" for this implementation (or one similar to it) that is type-safe (ie so that the return type of the getCar() method for the FORD only includes the shiftIntoPark() method and, likewise, the return type of the same method for the PORSCHE only includes the shiftInto1stGear() method)?此实现(或类似实现)是否有类型安全的“最佳实践”(即,FORD 的 getCar() 方法的返回类型仅包括 shiftIntoPark() 方法,同样, PORSCHE 相同方法的返回类型仅包括 shiftInto1stGear() 方法)?

The type returned by Garage.getCar is Automobile, it's not going to expose any of these park methods. Garage.getCar 返回的类型是Automobile,它不会公开任何这些park 方法。 Since the shift methods are static you're not going to get polymorphic behavior out of them, they have to be called on the specific classes.由于 shift 方法是静态的,因此您不会从中获得多态行为,因此必须在特定类上调用它们。

It is likely you are expecting the thing using the Automobile to be too smart about how it's using the Automobile, thereby defeating the purpose of using an interface.您很可能期望使用汽车的东西在使用汽车方面过于聪明,从而违背了使用界面的目的。 The point of using interfaces or superclasses like this is so that the program can manipulate objects at a high level without caring about implementation details, letting the different object implementations take care of themselves.像这样使用接口或超类的目的是让程序可以在高层操作对象而无需关心实现细节,让不同的对象实现自己照顾自己。 An analogous toy example might be an Automobile that exposed a park method, where that park method was implemented differently by the specific subclass according to its specific needs.一个类似的玩具示例可能是一个汽车,它公开了一个 park 方法,其中 park 方法由特定的子类根据其特定需要以不同的方式实现。

(Or alternatively get rid of the interface altogether, since driving a Mustang is not anything like driving a Corolla it might not make sense for them to share an interface.) (或者完全摆脱界面,因为驾驶野马与驾驶卡罗拉完全不同,他们共享界面可能没有意义。)

Interfaces and superclasses help provide a high-level interface and hide implementation details so they can be contained locally and don't leak out all over the rest of the program.接口和超类有助于提供高级接口并隐藏实现细节,以便它们可以在本地包含并且不会泄漏到程序的其余部分。 Work out how you want the object to be used, which defines the interface you want to expose, and what you want the object to take care of on its own.确定您希望如何使用该对象,它定义了您希望公开的接口,以及您希望该对象自行处理什么。

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

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