简体   繁体   English

Android应用程序架构 - 基于事件或分层

[英]Android application architecture - event based or layered

I'm building an SDK to be used in a hosting app. 我正在构建一个用于托管应用程序的SDK。 This SDK is going to be integrated in applications with a lot of users. 该SDK将集成在具有大量用户的应用程序中。

We started to build it with a layered architecture - UI, managers interacting with models and networking layer. 我们开始使用分层架构构建它 - UI,管理器与模型和网络层交互。 Three layers total. 总共三层。 The UI is getting updates by callbacks. UI正在通过回调获取更新。


Side note: I was a member in teams that builded some Android applications with a lot of users, in all of them we used the same layered architecture. 旁注:我是团队中的一员,用很多用户构建了一些Android应用程序,所有这些都使用了相同的分层架构。 These applications have active users and there is a good feedbacks until now. 这些应用程序拥有活跃的用户,直到现在还有很好的反馈。


We have our beta version and it seems that everything is working as expected. 我们有测试版,似乎一切都按预期工作。

A week ago one of our team members come and say that he think we need to change our architecture to event driven based. 一周前,我们的一位团队成员前来表示他认为我们需要将我们的架构更改为基于事件驱动的架构。 We will have static event handles in the SDK that handle all the listeners and the dispatch will be triggered from some other component. 我们将在SDK中具有处理所有侦听器的静态事件句柄,并且将从其他组件触发调度。

I think it's a bit risky, I think it will be hard to handle a large SDK with a lot of dependencies and services, in a multithreaded environment in that way. 我认为这有点冒险,我认为在多线程环境中以这种方式处理具有大量依赖性和服务的大型SDK会很困难。 In addition, because we are building an SDK, we want to isolate our main classes form our host application, and I think it will be almost impossible. 另外,因为我们正在构建SDK,所以我们希望将我们的主类从主机应用程序中隔离出来,我认为这几乎是不可能的。

My questions : 我的问题

  1. What do you think is the right way to go, is the layered architecture good and we need to stick with it, or the event based is the preferred one? 您认为正确的方法是什么,分层架构是好的,我们需要坚持下去,或者基于事件是首选的?

  2. Do you think it's a suitable solution for an SDK that's needs to be as efficient as possible? 您是否认为适合需要尽可能高效的SDK的解决方案?

  3. Do you think that this is a good solution for a multithreaded environment? 你认为这对多线程环境来说是一个很好的解决方案吗?

Appreciate any feedback, thanks. 感谢任何反馈,谢谢。

some articles I have read: 我读过的一些文章:

architecting-android-the-clean-way 架构,Android的最干净的方式

architectural-patterns-for-mobile-application-development 建筑图案-移动应用开发

event-driven-programming-for-android 事件驱动编程换机器人

Android itself is based on an event-based model. Android本身基于基于事件的模型。 An Android application uses a special thread, named the looper thread, to select events from an event queue one at a time, and processes them sequentially by executing their handlers. Android应用程序使用一个名为looper线程的特殊线程,一次从一个事件队列中选择事件,并通过执行其处理程序顺序处理它们。 There may be additional regular threads that communicate between each other and the looper thread using conventional synchronization operations. 可能存在使用传统同步操作在彼此和looper线程之间通信的附加常规线程。 The main role of a looper thread is to continuously check its event queue , select and process one event at a time. looper线程的主要作用是不断检查其事件队列 ,一次选择并处理一个事件。

To the best of my knowledge, the main motivation for Android to use event based model is that it has to handle alot of events (touch screen, click, sensors, network, internal and external events etc) and choice of event based model make sense. 据我所知,Android使用基于事件的模型的主要动机是它必须处理很多事件 (触摸屏,点击,传感器,网络,内部和外部事件等)和基于事件的模型选择有意义。

If your main concern is concurrency and multitasking then Event driven and reactive models happens to be the better choice for multi-threading (or concurrency in general). 如果您主要关注的是并发性和多任务处理,那么事件驱动反应模型恰好是多线程(或一般的并发)的更好选择。 But layered structure is not bad either, in-fact I think it is better. 但分层结构也不错,实际上我觉得它更好。

At the end of the day, it depends on the nature of job. 在一天结束时,这取决于工作的性质。 If you think what your SDK is proving, is similar to Android's job (handling massive events) then go for event based model otherwise layered structure is better. 如果您认为您的SDK正在证明什么,类似于Android的工作(处理大型事件),那么去基于事件的模型,否则分层结构更好。

If concurrency (Multi-threading) is your main issue and you want to exploit multi-core architecture and your jobs are CPU intensive then there are other architectures you should see. 如果并发 (多线程)是您的主要问题,并且您想要利用多核架构并且您的作业是CPU密集型的,那么您应该看到其他架构。

while going through performance tips for android some time ago I came across following advice: 虽然前段时间我经历了Android的性能提示,但我遇到了以下建议:

Always Measure 始终测量

Before you start optimizing, make sure you have a problem that you need to solve . 在开始优化之前,请确保您遇到需要解决的问题 Make sure you can accurately measure your existing performance, or you won't be able to measure the benefit of the alternatives you try. 确保您可以准确衡量现有的绩效,否则您将无法衡量您尝试的替代方案的好处。

in short 简而言之

If it ain't broke, don't fix it 如果没有损坏,请不要修理它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM