Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create different Objects with same base type. Factory pattern?

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

like image 605
maruchinu Avatar asked Nov 27 '11 18:11

maruchinu


2 Answers

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.

like image 197
Brian Agnew Avatar answered Sep 30 '22 10:09

Brian Agnew


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.

like image 29
Vilas Avatar answered Sep 30 '22 11:09

Vilas