用于灵活类创建的 Java 最佳实践库创建(工厂模式、抽象和接口)

发布于 2024-10-22 17:47:15 字数 787 浏览 5 评论 0 原文

假设我是一家汽车制造商的 Java 软件开发人员。我的任务是创建一个可供众多内部应用程序使用的库。对于制造的每种类型的汽车模型,我创建一个代表该模型的 java 对象。我不仅必须能够跟踪当前模型,还必须能够跟踪原型模型。原型模型将有一个名称,一旦投入生产,该名称很可能会发生变化。我需要能够使用该库来解释原型和 Flex,并在将其投入生产时更改名称。

我的问题是,最好的方法是什么?

这是我的想法......

我一直在阅读几本书,寻找最好的处理这种情况的想法。我立即想到使用工厂模式。我将有一个 CarModelFactory 类,它将为每个模型返回一个具体对象。例如:

public class CarModelFactory() {

    public CarModel createCivicModel() {}
    public CarModel createAccordModel() {}
    public CarModel createPrototype1() {
        return new ModelX();
    }
    public CarModel createPrototype1() {
        return new ModelY();
    }

这是最好的方法吗?我觉得应该有另一层抽象。我看到的问题是:

1) 如果 ModelX 投入生产,我为它创建一个方法并在 createPrototype1 方法中放入其他内容,现在调用该方法的程序得到错误的对象

怎么办 2) 如何处理 ModelX 更改其名称?

我感谢您抽出时间!

Imagine I am a Java software developer for a car manufacturer. I have been tasked with creating a library that will be used by numerous in-house applications. For each type of car model manufactured, I create a java object representing that model. I must be able to track not only current models, but prototype models. The prototype models will have one name that is very likely to change once it goes into production. I need to be able to use the library to account for the prototypes and flex with the name change when they are switched into production.

My question is, what is the best approach for this?

Here are my thoughts...

I have been reading several books for ideas as to best handle this situation. Immediately my mind jumps to using a factory pattern. I would have a CarModelFactory class which would return a concrete object for each model. For example:

public class CarModelFactory() {

    public CarModel createCivicModel() {}
    public CarModel createAccordModel() {}
    public CarModel createPrototype1() {
        return new ModelX();
    }
    public CarModel createPrototype1() {
        return new ModelY();
    }

Would this be the best approach? I feel like there should be another layer of abstraction. Problems I see are:

1) What if ModelX goes into production, I create a method for it and put something else in createPrototype1 method, now programs that call that method get the wrong object

2) How do I handle ModelX changing its name?

I thank you for your time!

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

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

发布评论

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

评论(1

假扮的天使 2024-10-29 17:47:15

工厂模型听起来不错,但我建议使用 createCarModel(String model) 方法,该方法在映射中查找适当的对象。然后重命名汽车模型只需在该地图中添加/删除即可。当然,通过适当的同步,可以防止重命名和获取发生冲突。

地图可能是 Map>,并且 createCar 方法将使用无参数构造函数实例化该类,所有 Car 都需要该构造函数。

这样,每次添加或重命名模型时都不需要重新编译,因为工厂类不会更改其方法签名集。

此外,如果您重写 ClassLoader,您可以卸载旧模型并加载新模型,从而使包含 .class 文件的实际目录保持干净(此后不再有旧原型类)制作成真实模型)。

The factory model sounds good, but I would suggest a createCarModel(String model) method, which looks up in a map the appropriate object. Then renaming a car model is a simple add/remove in that map. With appropriate synchronization, of course, to prevent a rename and a get from colliding.

The map would likely be Map<String, Class<? extends CarModel>>, and the createCar method would instantiate the class using a no-argument constructor, which you would require of all Cars.

This way, there is no recompile necessary any time you add or rename a model, as the factory class does not change its set of method signatures.

Additionally, if you override the ClassLoader, you can unload an old model and load up a new model, allowing the actual directory containing your .class files to be kept clean (no old prototype classes that have since been made into real models).

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