5

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 ;)

3
  • What is the difference which doesn't allow using generics for you?
    – Vlad
    Nov 27, 2011 at 19:02
  • 3
    Why must STorage know the type of the component? What does it do with them? Could it call a polymorphic method of Component?
    – JB Nizet
    Nov 27, 2011 at 19:06
  • Consumers of the Components request a specific type of component (e.g. 1 mainboard), so I want to store them separately and not in a single Collection<Component>
    – maruchinu
    Nov 27, 2011 at 19:36

4 Answers 4

4

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.

1

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.

1
  • 1
    Ad 1) In Java you cannot instantiate a new object of a generic type parameter as the type information is not available at runtime. Ad 2) The Component class has more fields than shown above. I removed them for the sake of simplicity, so I don't think an enum i sufficient in this case.
    – maruchinu
    Nov 27, 2011 at 19:29
1

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.