dimanche 29 décembre 2019

Can factory pattern be used for only one user defined class and no subclasses

I am trying to implement a basic factory pattern for a class which is user defined. For example a class of currencies which allows the user to enter the currency name, exchange rate, etc. Reason being that I would like to allow the possibility for future sub classes to be added.

I do not want to create a subclass for each possible currency as the details are entered by the user and these currencies may also be ficititous. I also do not have any other classes.

For example this is my code in which the user is expected to fill in the parameters of the class for a currency to be created. This is all done in a seperate class and they are placed in a list:

public String code;
public String name;
public boolean major;

public Currency(String code, String name, boolean major) {
    this.code = code;
    this.name = name;
    this.major = major;
}

I think I must be missing something but I cannot figure it out. It is possible that the factory pattern is not supposed to be used in this manner?

This is my first time using this design pattern as well in conjunction with dependency injection. Thank you for any help.

Aucun commentaire:

Enregistrer un commentaire