Create different objects with the same base type. Factory pattern?

I need to implement an application from several manufacturers / several consumers for a university course and have difficulty finding a solution to the next problem, which does not make me feel like I'm something wrong;)

I need to implement a Producer that creates the given Component type ( CPUComponent , MainboardComponent ). All subclasses of the general class Component ). Each instance of Producer will produce only a certain amount of one type of component (for example, only motherboards), and then shuts down.

Component - all more or less immutable objects (only final fields), and all the 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; } } 

Component subclasses are simply primitive, like

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

With a language that is Java, I cannot easily solve this problem using Generics (since I could in C #, because I cannot create new type parameter objects in Java). So I started to implement Factory

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

And specify specific factory implementations for each type of Component .

Now the problem is that when I want to save the created components in the Storage class (just controls all the produced components for consumers), I need to find out the exact type of objects (each CPUComponent , etc. in its own shelf), but I only get Component (base type) from factory.

So the only thing that will help now is instanceof , but I think there should be a better solution for my problem.

Another solution I can imagine would be to implement Producer for each type of Component , but I wanted to avoid it.

Perhaps Iā€™m thinking about how complicated and already completely redid it all. Just point me in the right direction;)

+4
source share
4 answers

Due to the fact that OO says that objects are doing something for you, I would call the store() method for each of your components (define this as abstract in the base class), passing the object to Storage . Your subclasses will implement this method in their own way and mediate with the Storage object for storage. If you do this, then your Storage class should not know about different components, and adding a new type of Component requires only defining this class and additional work elsewhere.

I note that in your question, it seems that your subclasses do not have any additional functionality outside the base class (if I read it correctly). It; it is precisely because of such scenarios that subclasses must have functionality specific to their type. You are absolutely right. instanceof . If you need to use it, this often indicates that you are not using the full flexibility of OO and / or that your object analysis is wrong.

+4
source

1) Java supports generics . Are you saying that for some reason, general Java support is insufficient in this case? From your description, you can see that you can simply parameterize the Producer class using a generic type.

2) From your description, it seems that Component may be enum .

+1
source

Factory pattern design is a way to use polymorphism. Polymorphism means that your base class has a specific interface, that is, it has a certain set of methods through which external objects will interact. Derived classes can implement this interface in their own way.

The bottom line is that if your classes are designed correctly, you will have to do everything you need through the base class. The Storage class will store components, and users of the Storage class will not know anything about the actual class of components; only that they can be used through the Component interface.

+1
source

You can save your Component objects in separate lists, for example: motherboards = ArrayList<Component>

You are correct that you need to implement the 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 specific classes for all component subtypes, which, as you say, all inherit from the Component base class.

+1
source

Source: https://habr.com/ru/post/1383332/


All Articles