I was doing some reading on the following links:
I notice that when using the factory pattern, all the examples I've seen use the no argument constructor in the factory. In the last link it's suggested that if the parameters grow, to consider using the builder pattern. I understand when and how to use the builder pattern, but the factory pattern is confusing to me. What happens if you have sub classes that have parameters, some of which are unique to the subclass? What do you do in this case?
Take this example:
Let's say you have the following abstract 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 sub-classes:
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;
}
Now, the arguments aren't enough to use the builder pattern are they? Is there anything wrong with simply doing this:
Client international = new International(id, firstname, lastname, documents);
or
International internationalObj = new International(id, firstname, lastname, documents);
Aucun commentaire:
Enregistrer un commentaire