Re: AW: How to avoid code duplication on forms?
Factory pattern is about Dependency Inversion Principle. You may use this pattern to avoid dependency to concrete classes. So the best way in your case is having Factory that constructs concrete editors via subclassing. They work best together :) Christian Helmbold-2 wrote: I think the difference between sub classing and static factory methods is a matter of taste in this case. If I have many fields, I'd need many classes for them. So I'd group them in a sub package. In the case of factory methods I'd group the methods to create fields in a class instead of a package. Subclass: 1class UserEmailField extends TextField { 2 public UserEmailField(String id, IModel model) { 3super(id, model); 4setRequired(true); 5add(EmailAddressValidator.getInstance()) 6add(UniqueValidator.unique(SystemUser.class, email)); 7 } 8} Factory method: 1public static TextField emailTextField(String id){ 2return (TextField) new TextField(id) 3.setRequired(true) 4.add(EmailAddressValidator.getInstance()) 5.add(UniqueValidator.unique(SystemUser.class, email)); 6} Your answer shows me, that there is no dramatically simpler way to do this. If you ignore the two lines for factory class declaration factory methods are even a bit shorter. Or is there an advantage of sub classing that I have missed? Regards, Christian - Ursprüngliche Mail Von: Igor Vaynberg igor.vaynb...@gmail.com An: users@wicket.apache.org Gesendet: Freitag, den 8. Mai 2009, 09:08:34 Uhr Betreff: Re: How to avoid code duplication on forms? much simpler class UserEmailField extends TextField { public UserEmailField(String id, IModel model) { super(id, model); add(EmailAddressValidator.getInstance()) add(UniqueValidator.unique(SystemUser.class, email)); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- View this message in context: http://www.nabble.com/How-to-avoid-code-duplication-on-forms--tp23440920p23496309.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
AW: How to avoid code duplication on forms?
I think the difference between sub classing and static factory methods is a matter of taste in this case. If I have many fields, I'd need many classes for them. So I'd group them in a sub package. In the case of factory methods I'd group the methods to create fields in a class instead of a package. Subclass: 1class UserEmailField extends TextField { 2 public UserEmailField(String id, IModel model) { 3super(id, model); 4setRequired(true); 5add(EmailAddressValidator.getInstance()) 6add(UniqueValidator.unique(SystemUser.class, email)); 7 } 8} Factory method: 1public static TextField emailTextField(String id){ 2return (TextField) new TextField(id) 3.setRequired(true) 4.add(EmailAddressValidator.getInstance()) 5.add(UniqueValidator.unique(SystemUser.class, email)); 6} Your answer shows me, that there is no dramatically simpler way to do this. If you ignore the two lines for factory class declaration factory methods are even a bit shorter. Or is there an advantage of sub classing that I have missed? Regards, Christian - Ursprüngliche Mail Von: Igor Vaynberg igor.vaynb...@gmail.com An: users@wicket.apache.org Gesendet: Freitag, den 8. Mai 2009, 09:08:34 Uhr Betreff: Re: How to avoid code duplication on forms? much simpler class UserEmailField extends TextField { public UserEmailField(String id, IModel model) { super(id, model); add(EmailAddressValidator.getInstance()) add(UniqueValidator.unique(SystemUser.class, email)); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: AW: How to avoid code duplication on forms?
the advantage to subclassing is that you're working better with the type system whereas static pulls you out of the object world and should be used with great caution. for example, subclassing enables more subclassing and therefore more reusability. static factory methods cannot be specialized. i also feel it's more intuitive to users for something like this. if you want to see all the textfields on your project and you subclass textfield, you can bring up textfield and hit F4 in eclipse and see the class hierarchy. static factory methods are a bit more hidden to quick inspection. i like static factory methods for things like units (Distance.meters(...) versus Distance.feet(...)) or if you want to vary the subclass returned. one improvement i'd make: public class EmailField extends TextField and write an EmailConverter. another reason subclassing is best. Christian Helmbold-2 wrote: I think the difference between sub classing and static factory methods is a matter of taste in this case. If I have many fields, I'd need many classes for them. So I'd group them in a sub package. In the case of factory methods I'd group the methods to create fields in a class instead of a package. Subclass: 1class UserEmailField extends TextField { 2 public UserEmailField(String id, IModel model) { 3super(id, model); 4setRequired(true); 5add(EmailAddressValidator.getInstance()) 6add(UniqueValidator.unique(SystemUser.class, email)); 7 } 8} Factory method: 1public static TextField emailTextField(String id){ 2return (TextField) new TextField(id) 3.setRequired(true) 4.add(EmailAddressValidator.getInstance()) 5.add(UniqueValidator.unique(SystemUser.class, email)); 6} Your answer shows me, that there is no dramatically simpler way to do this. If you ignore the two lines for factory class declaration factory methods are even a bit shorter. Or is there an advantage of sub classing that I have missed? Regards, Christian - Ursprüngliche Mail Von: Igor Vaynberg igor.vaynb...@gmail.com An: users@wicket.apache.org Gesendet: Freitag, den 8. Mai 2009, 09:08:34 Uhr Betreff: Re: How to avoid code duplication on forms? much simpler class UserEmailField extends TextField { public UserEmailField(String id, IModel model) { super(id, model); add(EmailAddressValidator.getInstance()) add(UniqueValidator.unique(SystemUser.class, email)); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- View this message in context: http://www.nabble.com/How-to-avoid-code-duplication-on-forms--tp23440920p23445544.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: AW: How to avoid code duplication on forms?
the advantage to subclassing is that you're working better with the type system whereas static pulls you out of the object world and should be used with great caution. for example, subclassing enables more subclassing and therefore more reusability. static factory methods cannot be specialized. i also feel it's more intuitive to users for something like this. if you want to see all the textfields on your project and you subclass textfield, you can bring up textfield and hit F4 in eclipse and see the class hierarchy. static factory methods are a bit more hidden to quick inspection. i like static factory methods for things like units (Distance.meters(...) versus Distance.feet(...)) or if you want to vary the subclass returned. one improvement i'd make: public class EmailField extends TextField and write an EmailConverter. another reason subclassing is best. Christian Helmbold-2 wrote: I think the difference between sub classing and static factory methods is a matter of taste in this case. If I have many fields, I'd need many classes for them. So I'd group them in a sub package. In the case of factory methods I'd group the methods to create fields in a class instead of a package. Subclass: 1class UserEmailField extends TextField { 2 public UserEmailField(String id, IModel model) { 3super(id, model); 4setRequired(true); 5add(EmailAddressValidator.getInstance()) 6add(UniqueValidator.unique(SystemUser.class, email)); 7 } 8} Factory method: 1public static TextField emailTextField(String id){ 2return (TextField) new TextField(id) 3.setRequired(true) 4.add(EmailAddressValidator.getInstance()) 5.add(UniqueValidator.unique(SystemUser.class, email)); 6} Your answer shows me, that there is no dramatically simpler way to do this. If you ignore the two lines for factory class declaration factory methods are even a bit shorter. Or is there an advantage of sub classing that I have missed? Regards, Christian - Ursprüngliche Mail Von: Igor Vaynberg igor.vaynb...@gmail.com An: users@wicket.apache.org Gesendet: Freitag, den 8. Mai 2009, 09:08:34 Uhr Betreff: Re: How to avoid code duplication on forms? much simpler class UserEmailField extends TextField { public UserEmailField(String id, IModel model) { super(id, model); add(EmailAddressValidator.getInstance()) add(UniqueValidator.unique(SystemUser.class, email)); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- View this message in context: http://www.nabble.com/How-to-avoid-code-duplication-on-forms--tp23440920p23445543.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: AW: How to avoid code duplication on forms?
uh that was: class EmailField extends TextField[Email] where the square brackets are angle brackets :) Christian Helmbold-2 wrote: I think the difference between sub classing and static factory methods is a matter of taste in this case. If I have many fields, I'd need many classes for them. So I'd group them in a sub package. In the case of factory methods I'd group the methods to create fields in a class instead of a package. Subclass: 1class UserEmailField extends TextField { 2 public UserEmailField(String id, IModel model) { 3super(id, model); 4setRequired(true); 5add(EmailAddressValidator.getInstance()) 6add(UniqueValidator.unique(SystemUser.class, email)); 7 } 8} Factory method: 1public static TextField emailTextField(String id){ 2return (TextField) new TextField(id) 3.setRequired(true) 4.add(EmailAddressValidator.getInstance()) 5.add(UniqueValidator.unique(SystemUser.class, email)); 6} Your answer shows me, that there is no dramatically simpler way to do this. If you ignore the two lines for factory class declaration factory methods are even a bit shorter. Or is there an advantage of sub classing that I have missed? Regards, Christian - Ursprüngliche Mail Von: Igor Vaynberg igor.vaynb...@gmail.com An: users@wicket.apache.org Gesendet: Freitag, den 8. Mai 2009, 09:08:34 Uhr Betreff: Re: How to avoid code duplication on forms? much simpler class UserEmailField extends TextField { public UserEmailField(String id, IModel model) { super(id, model); add(EmailAddressValidator.getInstance()) add(UniqueValidator.unique(SystemUser.class, email)); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- View this message in context: http://www.nabble.com/How-to-avoid-code-duplication-on-forms--tp23440920p23445578.html Sent from the Wicket - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org