开发者

How to refactor a class that contains many listener inner classes?

I am writing an android application that receives location updates and sends requests to a web service. The application has several activities and uses a local Service as a controller. The problem is that this 'controller' service is rapidly turning into a Blob/God anti-pattern. I'm trying to figure out the best way to refractor the code.

Nearly all of the functionality is asynchronous as the http client needs to run in a separate thread. Most of the method calls pass listeners to receive the response from the server. The location updates are also received via a listener. All of these listeners are currently implemented as inner classes in my Local Service. I now have about 10 inner classes inside my Local Service. I don't see how i can move these listeners into separate classes as they need access to the state stored in the Service. I have tried implementing a singleton/observer hybrid class that handles all the state, receiving updates from each of the listeners then passing it on to any registered observers but this is not a good solution either as it has all the problems associated with global state and it is di开发者_JS百科fficult to follow the flow of the program from one place to another. After re-writing, refactoring and re-writing again i am now totally confused and don't know what to do. Any advice?


Two thoughts. 1) This apparent event handling code bloat is hidden in MS Net IDE, and optionally in the Mac IDE, by collapsing the event handlers and providing a icon to expand the event code section. So I do not see the event handling code bloat and it does not bother me. Out of sight out of mind.

2) for Android 1.6+ you could refactor to XML attributes

<Button android:text="@string/get_plain_text_button"
android:id="@+id/ButtonPlainText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick= "plainTextOnClickHandler">

that corresponds to the method:

public voidplainTextOnClickHandler(View v){
    editTextPlainText.setText("Plain Text Button Clicked");
}

JAL

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜