java - DRY: a case with AsyncTasks -
i'm developing android app has lot of different requests web services. every request done in subclass of asynctask
in manner:
(new asynctask<string, void, object1>() { @override protected object1 doinbackground(string... params) { // network request , parsing object1 object1 obj = new object1(); obj1 = parser.parseobject1(httpclient.execute(...)); return obj1; } @override protected object1 onpostexecute(object1... ret) { return ret[0]; } }).execute();
object1
placeholder different objects (car
, bicycle
, truck
...), each 1 in different asynctask
.
what alternatives other returning output of httpclient in string
, parsing in main thread (ui thread)? avoid parsing in ui thread sounds reasonable if it's going parse lot of data, right?
-= update =-
let me rephrase question: i'm asking more intelligent way develop application avoiding being repetitive (asynctask
has lot of boilerplate code). way did creating 20+ subclasses of asynctask
, not dry (do not repeat yourself).
in ios have lambda expressions callbacks done in web requests easy , succinct.
you can create classes contain of boilerplate code. e.g.
public class specialasynctask<t> extends asynctask<string, void, t> { public interface resultprovider<t> { t generateresultinbackground(string... params); } public interface resultconsumer<t> { void handleresultinforeground(t result); } private final resultprovider<t> mprovider; private final resultconsumer<t> mconsumer; private specialasynctask(resultprovider<t> provider, resultconsumer<t> consumer) { mprovider = provider; mconsumer = consumer; } @override protected t doinbackground(string... params) { return mprovider.generateresultinbackground(params); } @override protected void onpostexecute(t result) { mconsumer.handleresultinforeground(result); } public static <t> void execute(resultprovider<t> provider, resultconsumer<t> consumer, string... params) { new specialasynctask<t>(provider, consumer).execute(params); } }
is example how keep object1
generic parameter while being able specify object needs implement interface handle code otherwise have inside new asynctask
instance.
with schema example define common code static content:
class providers { public static final resultprovider<string> http_getter = new resultprovider<string>() { @override public string generateresultinbackground(string... params) { return magichttplibrary.getcontentasstring(params[0]); } }; }
and can use providers.http_getter
parameter instead of implementing doinbackground
. or create new class hierarchy of implement 1 of interfaces different methods access them (like factories example)
use of above example example below
class user extends activity implements resultconsumer<string> { @override protected void oncreate(bundle savedinstancestate) { specialasynctask.execute(providers.http_getter, , "http://google.com"); specialasynctask.execute(providers.http_getter, , "http://yahoo.com"); } @override public void handleresultinforeground(string result) { toast.maketext(this, result, toast.length_long).show(); } }
and there more or less no repeated code besides different method calls. depends on how want use class , changes in code know how design that. identify parts need parametrized , move code repeats re-used place (inheritance / composition).
Comments
Post a Comment