java - Design Pattern to deal with a class with many members (not Builder) -


i have class many members (~20). initialize nicely constructor not option (due many parameters) because of bad code readability.

so tried use builder dp. problem it's nasty when comes extending such class.

any suggestion on design patter should use deal nicely situation (class many members)?

thanks lot!

as say, 20 fields lot constructor. regarding readability, pattern comes mind static factory methods (but in sense of joshua bloch's effective java, not factory method of gof book).

as says in book (in item 1), static factory methods have names (unlike constructors) static method well-chosen name easier use , resulting client code easier read.

class manyfields {     t a;     u b;     (...)     v t; } 

that way, things like

manyfields.withatofzeroed(g, h, i, ..., t) manyfields.onlygandj(g, j) manyfields.consonantszeroed(a, e, i, o, u) (...) 

that is, adapt/coin new static factory methods addressing different usage patterns in code easier , more readable.

of course, having set of default values on that, since number of parameters needed in methods reduced.

besides, static factory methods not have create new object each time they’re invoked, allowint predefined instances, caching, etc. result, if equivalent objects requested often, can improve performance technique, apart highlighting typical objects used class.

nevertheless, efectiveness (in terms of readability) of bunch of static factory methods depends on how can group parameters in terms of common values, default values, etc. arises need of splitting such big class smaller classes , using composition (as @dkaztel suggests).

anyway, when dealing many constructor parameters, best approach builder.

given want avoid it, can improve readability using method chaining setters.

manyfields obj = new manyfields(); obj.a(a).b(b).d(d).t(t).v(v).f(f); 

this answer builders , fluent interfaces can useful.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -