design patterns - Java: separate main logic & exception-handling logic -



design patterns - Java: separate main logic & exception-handling logic -

question: how move logic strategy (another class) leave exception-handling logic in class-owner?

i've got: method map gets data array input argument, , perform lot of logic on that:

class owner ... public void map(final longwritable key, final text value, final context context) { // 1st-fragment of code string[] info = null; seek { // not owner class logic, should moved strategy stringreader reader = new stringreader(line); customparser cparser = new customparser(reader,_strategy); info = cparser.getline(); } grab (final ioexception e) { outputbaddata(line); // owner class logic return; } // 2nd-fragment of code: logic based on `data` array ................ ................ }

i want: logic in 1st fragment (except outputbaddata) doesn't belong class. want move strategies. it'll like:

public void map(final longwritable key, final text value, final context context) { // 1st-fragment of code string[] info = strategy.getdata(value); // 2nd-fragment of code: logic based on `data` array ................ ................ }

problem: outputbaddata logic of owner class, not strategy.

if i've understood right, must create strategy class , define within method required logic. in method's signature define 'throws ioexception'

for example:

public [return type] readline(arguments...) throws ioexception { ... }

also must have reference object of new class(strategy) invoke 'readline' method. can store object field of owner class passing constructor argument (for using setter method) when create instance of class. , then, in 'map' method of owner class, surround 'readline' method invocation 'try-catch' block handle exception:

try{ strategyreference.readline(arguments...); }catch (ioexception e) { outputbaddata(line); return;

java design-patterns refactoring

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -