我想我只是在制作“Spaghetti-Code”。也许一些专家可以帮助我为我的解决方案编写更好的代码。以下伪代码将显示我的问题:
我有一些运行时间很长的程序。此过程将显示一个进度条。我使用 Asynctasks 来实现这一点。这一切都很好,但我的代码不再可维护:
我希望留下简单的代码而不嵌套代码:
public void onCreate(Bundle savedInstanceState) {
LongrunningTask1();
dosomething1();
LongrunningTask2();
dosomething2();
LongrunningTask3();
dosomething3();
LongrunningTask4();
dosomething4();
}
我的 Asynctasks 代码:
public void onCreate(Bundle savedInstanceState) {
new LongrunningTask1().execute("Task1");
}
class LongrunningTask1 extends AsyncTask<String, Void, boolean> {
private Exception exception;
protected RSSFeed doInBackground(String... urls) {
try {
Do;
return true;
} catch (Exception e) {
return null;
}
}
protected void onProgressUpdate(Void... values) {
updateprogressbar();
}
protected void onPostExecute(RSSFeed feed) {
dosomething1();
new LongrunningTask2().execute("Task2");
}
}
class LongrunningTask2 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask1
protected void onPostExecute(RSSFeed feed) {
dosomething2();
new LongrunningTask3().execute("Task3");
}
}
class LongrunningTask3 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask2
protected void onPostExecute(RSSFeed feed) {
dosomething3();
new LongrunningTask4().execute("Task4");
}
}
class LongrunningTask4 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask3
protected void onPostExecute(RSSFeed feed) {
dosomething4();
}
}
我想避免任何嵌套调用。谁能帮我 ?
问候