java - A design pattern for constructors -
i have been challenged design issue try describe below.
suppose class, call a, has constructor bunch of parameters. since tiring , dirty write parameters in each instantiation, have written class, call stylesheeta, encapsulates parameters , parameter constructor of a. in way, can prepare default stylesheeta templates used later, , if needed, can modify them.
and @ point, need extend a. suppose b extends a. b have own stylesheet, namely stylesheetb. think appropriate stylesheetb extends stylesheeta, 1 stylesheet parameter, constructor of b can construct super class a. afraid of possibility design may have flaws. example if decide add getter/setter stylesheet? there novel way handle these situations? in wrong way? confused, attach code here:
class { stylesheeta ss; a(stylesheeta ss) { this.ss = ss; // stuff ingredients of stylesheet } } class stylesheeta { int n1; int n2; // : // : int n100; } class b extends { b(stylesheetb ss) { super(ss); // stuff ingredients of stylesheet } } class stylesheetb extends stylesheeta { int n101; int n102; // : // : int n200; }
thank or suggestions, of critics appreciated.
edit: developing in java me there no generics support.
it seems me moving problem of having many parameters class a
class stylesheeta
.
to illustrate point, think of question: how instantiate stylesheeta
? using constructor accepts these parameters, anyway. benefit design may give if have same set of parameter values encapsulated object of stylesheeta
reuse among multiple instances of a
. if so, bear in mind although you'd have different instances of a
share same parameters, isn't choice.
what recommend try refactor class a
itself. try break smaller classes. if nesseccary, try create subclasses avoid conditional branches, etc.
now, don't know how class a
looks like, maybe if you'll have several classes, each own set of parameters. , if of parameters discriminator (meaning determines class "type") able rid of it, using subclasses, , relying on built in type system instead.
Comments
Post a Comment