如何重构一个包含很多监听器内部类的类?

发布于 2024-11-14 07:41:30 字数 433 浏览 0 评论 0原文

我正在编写一个 Android 应用程序,用于接收位置更新并向网络服务发送请求。该应用程序有多个活动并使用本地服务作为控制器。问题是这个“控制器”服务正在迅速转变为 Blob/God 反模式。我正在尝试找出折射代码的最佳方法。

几乎所有功能都是异步的,因为 http 客户端需要在单独的线程中运行。大多数方法调用都会通过侦听器来接收来自服务器的响应。位置更新也通过侦听器接收。所有这些侦听器当前都作为我的本地服务中的内部类实现。现在我的本地服务中有大约 10 个内部类。我不知道如何将这些侦听器移动到单独的类中,因为它们需要访问存储在服务中的状态。我尝试实现一个处理所有状态的单例/观察者混合类,从每个侦听器接收更新,然后将其传递给任何注册的观察者,但这也不是一个好的解决方案,因为它具有与全局状态相关的所有问题,并且很难跟踪程序从一个地方到另一个地方的流程。在重写、重构和再次重写之后,我现在完全困惑了,不知道该怎么办。有什么建议吗?

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 difficult 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-11-21 07:41:30

两个想法。 1) 通过折叠事件处理程序并提供一个图标来展开事件代码部分,这种明显的事件处理代码膨胀隐藏在 MS Net IDE 中,也可以选择在 Mac IDE 中隐藏。所以我没有看到事件处理代码臃肿,这并不困扰我。眼不见心不烦。

XML 属性

<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">

2) 对于 Android 1.6+,您可以重构对应于方法

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

: JAL

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文