Let's assume I have the following class:
public abstract class Client { private String clientID; private String clientFirstName; private String clientLastName; public Client(String ID, String firstname, String lastname) { this.clientID = ID; this.clientFirstName = firstname; this.clientLastName = lastname; } }
and the following Subclasses:
public class Domestic extends Client { public Domestic(String ID, String firstname, String lastname) { super(ID, firstname, lastname); } }
public class International extends Client { private List<String> documents; public International(String ID, String firstname, String lastname, List<String> documents) { super(ID, firstname, lastname); this.documents = documents; } }
Given that both subclasses have a different set of parameters, the International subclass requires a List of documents(passport, drivers license), while domestic doesn't. To me anyway, I don't believe the factory pattern is adequate because of the different constructor parameters, and the builder patter isn't necessary as all arguments are necessary, an there isn't that many of them. So is doing this okay?
Client international = new International(id, firstname, lastname, documents);
or if there is a specific method that I need to access in the International class:
International internationalObj = new International(id, firstname, lastname, documents);
Questions:
1. Is it mandatory to use the factory or builder pattern given this situation?
2. Is there a downside to doing to doing it without the factory or builder?
1. No it is not mandatory but would help.
Patterns in general are not mandatory and would be better if you alternate them to your own solution rather than using them as is.
Because most of the time, the don't fit 100% as-is.
2. Yes. A big deal of a good software is where your instances are coming from.
In your own example, a factory would be an over-design because you are dealing with a simple model and not a service nor a proxy.
Generally a factory is needed when:
A) You have too many concrete implementatino so you want to give something easier to deal with.
B) You want to control the instantiation of an object. (How it is created).
C) Complex objects which you need special cases to deal with.
So for your solution I'd just create a function at the most part. I don't know your other part of the software, so generally look for what I mentioned above.