具有 UI 和非 UI 模式的后台操作的 Android 设计模式 [英] Android design pattern for background operation with UI and non-UI modes

查看:27
本文介绍了具有 UI 和非 UI 模式的后台操作的 Android 设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台操作,我希望能够以两种模式运行:

I have a background operation that I'd like to be able to run in two modes:

  • UI 模式,由用户启动,为用户提供关于整个生命周期操作状态的精确反馈.

  • UI mode, which is launched by the user and provides the user precise feedback on the state of the operation throughout its life cycle.

非 UI 模式,由 AlarmManager 启动并在其生命周期结束时为用户提供操作摘要.

non-UI mode, which is launched by AlarmManager and provides the user with a summary of the operation at the end of its life cycle.

仅实现UI 模式的自然设计选择将是AsyncTask,而仅实现非UI 模式的自然设计选择将是IntentService.

The natural design choice to achieve only UI mode would be AsyncTask and the natural design choice to achieve only non-UI mode would be IntentService.

同时实现两种模式的自然设计选择是什么?即,将这两种模式合并到一个对象中的自然设计选择是什么?

What is the natural design choice to achieve both modes simultaneously? I.e., what is the natural design choice to incorporate these two modes into a single object?

推荐答案

与其让一个对象通过不同的方式(UI/非 UI)做同样的事情,我会把业务逻辑移到一个单独的类中,然后有在您需要时激活两个不同的对象(AsyncTaskIntentService),并在其中使用该对象.另外,您打算在 AsyncTask 中运行什么样的任务?

Instead of having an object doing the same stuff through different ways (UI / non-UI), I would move the business logic into a separate class, then have two different objects (AsyncTask and IntentService) being activated at the time you need, and use that object within. Also, what kind of task are you planning on running in your AsyncTask?

这篇关于具有 UI 和非 UI 模式的后台操作的 Android 设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆