创建具有相同基本类型的不同对象。工厂模式?

发布于 2024-12-18 09:51:45 字数 1425 浏览 1 评论 0原文

我必须为大学课程实现多个生产者/多个消费者示例应用程序,并且很难找到以下问题的解决方案,但这并不让我觉得我做错了什么;)

我必须实现一个Producer 生成给定类型的 ComponentCPUComponentMainboardComponent。公共 Component 的所有子类 类)。 Producer 的每个实例只会生成给定数量的一种类型的组件(例如,仅主板),然后退出。

Component 或多或少都是不可变的对象(只有 final 字段),所有逻辑都在公共基类 Component 中实现(简化如下) )

public abstract class Component implements Serializable
{    
    private final long id;

    public Component(int id) { ... }

    public long getId()
    {
        return id;
    }
}

Component 的子类仅仅是原始的,就像

public class CPUComponent extends Component
{
    public CPUComponent(long id) { ... }
}

Java 语言一样,我无法使用 Generics 轻松解决这个对象生成问题(正如我在 C# 中可以做到的那样,因为我无法实例化泛型类型参数的新对象爪哇)。所以我开始实现一个工厂

public interface ComponentFactory {
    Component createComponent(Producer producer, boolean defective);
}

并为每个Component类型提供具体的工厂实现。

我现在遇到的问题是,当我想将生成的组件存储在我的 Storage 类中(只为消费者管理所有生成的组件)时,我需要弄清楚对象的确切类型(每个 CPUComponent 等都在它自己的架子上),但我只从工厂获得一个 Component (基本类型)。

所以现在唯一有帮助的是 instanceof,但我认为必须有一个更好的解决方案来解决我的问题。

我能想到的另一个解决方案是为每种类型的组件实现一个生产者,但我想避免这种方式。

也许我想得太复杂了,并且已经完全过度设计了整个事情。只要给我指出正确的方向即可;)

I have to implement a multiple producers / multiple consumers example application for a university course and have a hard time to find a solution for the following problem, that doesn't make me feel, that I do something wrong ;)

I have to implement a Producer which produces a given kind of Component (CPUComponent, MainboardComponent. All subclasses of a common Component class). Each instance of a Producer will only produce a given amount of one type of component (e.g. only Mainboards) and then quits.

The Components are all more or less immutable objects (only final fields) and all logic is implemented in the common base class Component (simplified below)

public abstract class Component implements Serializable
{    
    private final long id;

    public Component(int id) { ... }

    public long getId()
    {
        return id;
    }
}

The subclasses of Component are merely primitive, like

public class CPUComponent extends Component
{
    public CPUComponent(long id) { ... }
}

With the language being Java, I cannot solve this object generation easily with Generics (as I would be able to in C#, because I cannot instantiate new objects of generic type parameters in Java). So I started to implement a Factory

public interface ComponentFactory {
    Component createComponent(Producer producer, boolean defective);
}

And provide concrete factory implementations for each Component type.

The problem I have now is that, when I want to store the produced components in my Storage class (just manages all produced components for the consumers), I need to figure out the exact type of the objects (every CPUComponent, etc. in it's own shelf), but I only get a Component (base type) from the factory.

So the only thing that would help now, would be instanceof, but I think there has to be a better solution for my problem.

The other solution I can think of would be to implement a Producers for each type of Component, but I wanted to avoid that way.

Maybe I'm thinking way to complex and have already completely over-engineered the whole thing. Just point me in the right direction ;)

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

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

发布评论

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

评论(4

弥繁 2024-12-25 09:51:45

基于 OO 是关于告诉对象为你做事,我会在每个组件上调用一个方法 store() (将其定义为 abstract 在你的基类中),传入 Storage 对象。您的子类将以自己的特定方式实现此方法,并与 Storage 对象进行协调以存储自身。如果您这样做,那么您的 Storage 类不需要了解不同的组件,并且添加新的 Component 类型只需要定义该类,而无需在其他地方进行额外的工作。

我注意到,在您的问题中,您给人的印象是您的子类除了基类之外没有其他功能(如果我没有正确阅读它)。正是因为这样的场景,子类应该具有特定于其类型的功能。你是绝对正确的。 实例。如果您必须使用它,那么它通常表明您没有充分利用面向对象的灵活性和/或您的对象分析不正确。

On the basis that OO is about telling objects to do things for you, I would call a method store() on each of your components (define this as abstract in your base class), passing in the Storage object. Your subclasses will implement this method in their own particular way, and mediate with the Storage object in order to store themselves. If you do this then your Storage class doesn't need to know about different components, and adding a new Component type only requires the definition of that class and no extra work elsewhere.

I note that in your question you give the impression that your subclasses have no further functionality beyond the base class (if I've read it correctly). It;'s precisely because of scenarios like this that the subclasses should have functionality specific to their type. You're absolutely right re. instanceof. If you have to use it then it's often a pointer that you're not using the full flexibility of OO and/or that your object analysis is not right.

—━☆沉默づ 2024-12-25 09:51:45

1) Java确实支持泛型。您是说由于某种原因,Java 中的通用支持在这种情况下还不够吗?从您的描述来看,您似乎可以使用泛型类型参数化 Producer 类。

2)根据您的描述,组件似乎可能是 enum

1) Java does support generics. Are you saying that for some reason the generic support in Java is not sufficient in this case? From your description it looks like you could just parameterize the Producer class using a generic type.

2) From your description, it seems like Component could be an enum.

半山落雨半山空 2024-12-25 09:51:45

工厂设计模式是一种利用多态性的方法。多态性意味着您的基类具有特定的接口,即它具有一组特定的方法,外部对象可以通过这些方法进行通信。派生类可以以自己的方式实现此接口。

最重要的是,如果您的类设计得当,您应该能够通过基类完成您需要的一切。 Storage类将存储Components,Storage类的用户对Components的实际类一无所知;只是它们可以通过 Component 接口使用。

The factory design pattern is a way of leveraging polymorphism. Polymorphism means that your base class has a particular interface, i.e. it has a particular set of methods through which external objects will communicate. Derived classes may implement this interface in their own way.

The bottom line is that if your classes are designed properly, you should be able to do everything you need through the base class. The Storage class will store Components, and users of the Storage class will know nothing about the actual class of the Components; only that they can be used through the Component interface.

无畏 2024-12-25 09:51:45

您可以将 Component 对象存储在单独的列表中,例如: motherboards = ArrayList

您是对的,您确实需要在这里实现工厂模式。

public class ComponentFactory() {
    public Component getComponent(Integer id, String componentType) {
        if (componentType.equals("motherboard"))
            return new MotherboardComponent(id);
        else if(componentType.equals("cpu"))
            return new CpuComponent(id);
        else
            return null;
    }
}

正如您所说,您将需要为所有组件子类型实现具体的类,所有这些子类型都继承自 Component 基类。

You can store your Component objects in separate lists like: motherboards = ArrayList<Component>

You are right in that you do need to implement a Factory pattern here.

public class ComponentFactory() {
    public Component getComponent(Integer id, String componentType) {
        if (componentType.equals("motherboard"))
            return new MotherboardComponent(id);
        else if(componentType.equals("cpu"))
            return new CpuComponent(id);
        else
            return null;
    }
}

You will need to implement concrete classes for all component sub-types, that all inherit from the Component base class, as you say.

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