java - Code refactoring: Outsourcing substeps to helper classes -
when class performs complicated , lengthy task, refactor step-by-step based on situation, shown below.
version 0
public class complicatedtaskdoer{ public void docomplicatedtask(){ // lots of complicated code } } version 1:
break downwards multiple smaller sub-tasks
public class complicatedtaskdoer{ public void docomplicatedtask(){ init(); dosubstepa(); dob(); doc(); wrapup(); } } version 2:
if complicated enough, outsource sub-tasks helper classes. don't code interfaces in case.
public class complicatedtaskdoer{ public void docomplicatedtask(){ init(); subsetpadoerclass.doa(); classb.dob(); classc.doc(); wrapup(); } } version 3:
if see self in need of adding more components in future , if there's valid pattern in terms of input , output objects, following.
public class complicatedtaskcontroller{ //injected list<somethinghelpercomponent> components; public void docomplicatedtask(){ init(); for(somethinghelpercomponent component : components){ component.process(commoninput); } wrapup(); } } i'm more curious version 3. ended doing quite few times.
q1) there existing pattern that's similar , more effective? it's not 'chain of responsibilities' i'm looking prefer components independent (open discuss). looks more configurable variation of template method pattern.
q2) i've named main class 'somethingcontroller' , helper classes 'somethinghelper' or 'somethingcomponent'. realized 'controller' misleading , 'helper' non-informative.
it'd helpful ideas on correctly naming classes. how'd name them?
q3) did think refactoring reasonable?
q4) subjective: ok maintain steps in helper methods , outsource steps helper classes? restrain myself unit-testing non-public methods.
q5) consider helper classes, i.e. no code-to-interfaces, code smell? may can declare them inner classes?
configuration of classes
this legit solution. seen spring partial tasks implement interface , through spring magic can have list of implementations with
@autowired list<myinterface> myparts; design pattern name as naming think think as special case of chain of responsibility. might not accurate, shows intention pretty well.
naming of classesi'd go suffix .*algorithm
name main interface dosomethingcomplicatedalgorithm or dosomethingcomplicatedalgorithmstep. classes implement interface called whatpartofalgorithmisusedhere
if they'll allow avoid code duplication , have no improve domain model can utilize them. on other hand leave lastly resort option. might little code smell, application won't burn.
java oop design-patterns refactoring code-cleanup
No comments:
Post a Comment