使用枚举作为实现的容器

发布于 2024-11-28 00:31:07 字数 1286 浏览 0 评论 0原文

我目前正在开展一个项目,我们必须在 3D 环境中表示一组向量。我们有几种不同的可视化实现。

我想到了可以将所有可视化类型捆绑在一个枚举中。我定义了一个接口 VectorVisualization 和几个实现该接口的实现。

现在,我已将以下枚举添加到 Interface 类中:

public interface VectorVisualization {

    public enum VectorVisualizationType {
       CYLINDER(new VectorVisualizationCylinder(), "Cylinder"),
       CONES(new VectorVisualizationCones(), "Cones"),
       FATCONES(new VectorVisualizationFatCones(), "Fat cones"),
       ARROWS(new VectorVisualizationArrows(), "Arrows");

       private final String label;
       private final VectorVisualization vis;

       VectorVisualizationType(VectorVisualization vis, String label) {
           this.vis = vis;
           this.label = label;
       }

       public VectorVisualization getVisualization() {
           return this.vis;
       }

       public String getLabel() {
           return this.label;
       }
   }

   void prepareVBO(GL gl, ArrayList<VectorData> vectors, VectorField field);
   void render(GL gl);
   void clearOldVBOS(GL gl);
}

该标签适用于 Gui 中的 JComboBox。所以我现在可以迭代枚举并获取不同类型的标签。另外,为了设置一个实现,我可以像这样使用枚举:

VectorVisualizationType.CYLINDER.getVisualization()

但这是一个好方法吗?或者这种方法有什么问题吗?当然,现在当您创建了一个新的实现时,您必须将其添加到枚举中。

感谢您的意见!

I'm currently working on a project where we have to represent a set of vectors in a 3D environment. We have several different visualization implementations.

I came to the idea, that I could bundle all the visualization types in an enum. I have defined an Interface VectorVisualization and several implementations which implement this interface.

Now I have added to the Interface class the following enum:

public interface VectorVisualization {

    public enum VectorVisualizationType {
       CYLINDER(new VectorVisualizationCylinder(), "Cylinder"),
       CONES(new VectorVisualizationCones(), "Cones"),
       FATCONES(new VectorVisualizationFatCones(), "Fat cones"),
       ARROWS(new VectorVisualizationArrows(), "Arrows");

       private final String label;
       private final VectorVisualization vis;

       VectorVisualizationType(VectorVisualization vis, String label) {
           this.vis = vis;
           this.label = label;
       }

       public VectorVisualization getVisualization() {
           return this.vis;
       }

       public String getLabel() {
           return this.label;
       }
   }

   void prepareVBO(GL gl, ArrayList<VectorData> vectors, VectorField field);
   void render(GL gl);
   void clearOldVBOS(GL gl);
}

The label is for a JComboBox in the Gui. So I can now just iterate over the enum and get the label of the different types. Also to set a Implementation I can use the enum like that:

VectorVisualizationType.CYLINDER.getVisualization()

But is this a nice way? Or are there any problems with that approach? Of course, now when you've created a new implementation you have to add this to the enum.

Thanks for your opinion!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-12-05 00:31:07

有趣的。我以前曾使用枚举类型来携带有用的元数据,但从未将其用于存储可执行代码片段。

也就是说,我在您的方法中看到的唯一问题是,正如您已经指出的那样,当您创建新的 VectorVisualization 实现时,您必须手动向枚举添加新条目。一般来说,我更愿意尽可能避免这种手动开销,但这实际上是个人喜好的问题。

如果您(以及与您一起处理此代码的其他人)意识到此限制并且不介意,那么我认为您的解决方案很好。

请注意,您当前的结构要求每个 VectorVisualization 都以线程安全的方式实现,因为只有一个实例会分发给通过枚举类型引用它的每个人。如果这是一个问题,您可以通过根据枚举存储实现类而不是实现实例来解决此问题,然后只需修改 getVisualization() 以创建关联实现类的新实例。叫。这将对 VectorVisualization 实现施加额外的限制,即每个实现都需要提供一个公共 0 参数构造函数来创建可用的实现实例。

Interesting. I've used enumerated types to carry around useful bits of metadata before, but never taken it quite so far as storing pieces of executable code.

That said, the only issue I see with your approach is that, as you have already noted, when you create a new VectorVisualization implementation you will have to manually add a new entry to the enumeration. Generally I prefer to avoid such manual overhead where possible, but really that's a matter of personal preference.

If you (and everyone else working on this code with you) are aware of this constraint and don't mind it, then I think your solution is fine.

Do note that your current structure requires every VectorVisualization to be implemented in a thread-safe manner, because there is only a single instance that is handed out to everyone who references it through the enumerated type. If this is an issue you could work around it by storing the implementation classes against the enumeration instead of implementation instances, and then simply modify getVisualization() to create a new instance of the associated implementation class when it is called. This would place an additional restriction against VectorVisualization implementations, that each one needs to provide a public 0-parameter constructor which creates a usable instance of the implementation.

一抹微笑 2024-12-05 00:31:07

使用枚举来列出当前的实现有许多非常好的属性。例如,找到所有当前的实现非常容易,因为根据定义它们必须在枚举声明中列出,并且您有标准的enum接口来访问这些实现。

但它也使得不可能以可插拔的方式扩展当前的集合 - 因此第 3 方无法添加新的实现。正是出于这个原因,我通常喜欢使用一个单例管理器来保存所有实现。这种模式也可以很好地与 OSGi 等许多组件框架配合使用。

Using an enumeration to list the current implementations have a number of very nice properties. E.g. it is pretty easy to find all current implementations as they by definition must be listed in the enumeration declaration and you have the standard enum interface to get access to the implementations.

But it also makes it impossible to extend the current set in a pluggable way - so a 3rd party cannot add a new implementation. For this very reason I usually like to use a singleton manager that will hold all implementations. This pattern also plays very nicely with many component frameworks like OSGi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文