Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:51:16+00:00 2026-05-27T02:51:16+00:00

I have to implement a multiple producers / multiple consumers example application for a

  • 0

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 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:51:17+00:00Added an answer on May 27, 2026 at 2:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have heard there is a way to cheat single inheritance and implement multiple
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have to implement the VinPower application. They offer a Java version, a C
I have the following in mind: A product can have multiple categories A category
Guys, please help me with the following problem. I have encountered the famous cannot
I want to implement multiple auth providers (like stackoverflow). I have downloaded the dotnetauth
I have an interesting problem and would appreciate your thoughts for the best solution.
We have an application with multiple windows on different screens using 3 graphic cards.
I have a question for how to implement multiple criteria for a repository pattern
I need to implement a producer/consumer bounded queue, multiple consumers against a single producer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.