It's used to improve performance of a ListView while scrolling it.
The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent calls of findViewById() during ListView scrolling, and that will make it smooth.
All these patterns, MVC, MVVM, MVP, and Presentation Model, can be applied to Android apps, but without a third-party framework, it is not easy to get well-organized structure and clean code.
MVVM is originated from PresentationModel. When we apply MVC, MVVM, and Presentation Model to an Android app, what we really want is to have a clear structured project and more importantly easier for unit tests.
At the moment, without an third-party framework, you usually have lots of code (like addXXListener(), findViewById(), etc.), which does not add any business value. What's more, you have to run Android unit tests instead of normal JUnit tests, which take ages to run and make unit tests somewhat impractical.
For these reasons, some years ago we started an open source project, RoboBinding - A data-binding Presentation Model framework for the Android platform. RoboBinding helps you write UI code that is easier to read, test, and maintain. RoboBinding removes the need of unnecessary code like addXXListener or so, and shifts UI logic to the Presentation Model, which is a POJO and can be tested via normal JUnit tests. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality.
I would like to add a design pattern that has been applied in Android Framework. This is Half Sync Half Async pattern used in the Asynctask implementation. See my discussion at
In Android the "work queue processor" pattern is commonly used to offload tasks from an application's main thread.
Example: The design of the IntentService class.
The IntentService receives the Intents, launch a worker thread, and stops the service as appropriate.All requests are handled on a single worker thread.
I tried using both the model–view–controller (MVC) and model–view–presenter architectural patterns for doing android development. My findings are model–view–controller works fine, but there are a couple of "issues". It all comes down to how you perceive the Android Activity class. Is it a controller, or is it a view?
The actual Activity class doesn't extend Android's View class, but it does, however, handle displaying a window to the user and also handle the events of that window (onCreate, onPause, etc.).
This means, that when you are using an MVC pattern, your controller will actually be a pseudo view–controller. Since it is handling displaying a window to the user, with the additional view components you have added to it with setContentView, and also handling events for at least the various activity life cycle events.
In MVC, the controller is supposed to be the main entry point. Which is a bit debatable if this is the case when applying it to Android development, since the activity is the natural entry point of most applications.
Because of this, I personally find that the model–view–presenter pattern is a perfect fit for Android development. Since the view's role in this pattern is:
Serving as a entry point
Rendering components
Routing user events to the presenter
This allows you to implement your model like so:
View - this contains your UI components, and handles events for them.
Presenter - this will handle communication between your model and your view, look at it as a gateway to your model. Meaning, if you have a complex domain model representing, God knows what, and your view only needs a very small subset of this model, the presenters job is to query the model and then update the view. For example, if you have a model containing a paragraph of text, a headline and a word-count. But in a given view, you only need to display the headline in the view. Then the presenter will read the data needed from the model, and update the view accordingly.
Model - this should basically be your full domain model. Hopefully it will help making your domain model more "tight" as well, since you won't need special methods to deal with cases as mentioned above.
By decoupling the model from the view all together (through use of the presenter), it also becomes much more intuitive to test your model. You can have unit tests for your domain model, and unit tests for your presenters.
Try it out. I personally find it a great fit for Android development.
After working and blogging about MVC and MVP in Android for several years (see the body of the answer below), I decided to capture my knowledge and understanding in a more comprehensive and easily digestible form.
So, I released a full blown video course about Android applications architecture. So, if you're interested in mastering the most advanced architectural patterns in Android development, check out this comprehensive course here.
This answer was updated in order to remain relevant as of November 2016
Design patterns aim at describing a general "trick" that programmer might implement for handling a particular set of recurring software tasks. For example: In OOP, when there is a need for an object to notify a set of other objects about some events, the observer design pattern can be employed.
Since Android applications (and most of AOSP) are written in Java, which is object-oriented, I think you'll have a hard time looking for a single OOP design pattern which is NOT used on Android.
Architectural patterns, on the other hand, do not address particular software tasks - they aim to provide templates for software organization based on the use cases of the software component in question.
It sounds a bit complicated, but I hope an example will clarify: If some application will be used to fetch data from a remote server and present it to the user in a structured manner, then MVC might be a good candidate for consideration. Note that I said nothing about software tasks and program flow of the application - I just described it from user's point of view, and a candidate for an architectural pattern emerged.
Since you mentioned MVC in your question, I'd guess that architectural patterns is what you're looking for.
Historically, there were no official guidelines by Google about applications' architectures, which (among other reasons) led to a total mess in the source code of Android apps. In fact, even today most applications that I see still do not follow OOP best practices and do not show a clear logical organization of code.
Two years ago it was very hard to find information about MVC or MVP on Android. Today, MVC, MVP and MVVM has become "buzz-words" in the Android community, and we are surrounded by countless experts which constantly try to convince us that MVx is better than MVy. In my opinion, discussing whether MVx is better than MVy is totally pointless because the terms themselves are very ambiguous - just look at the answers to this question, and you'll realize that different people can associate these abbreviations with completely different constructs.
Due to the fact that a search for a best architectural pattern for Android has officially been started, I think we are about to see several more ideas come to light. At this point, it is really impossible to predict which pattern (or patterns) will become industry standards in the future - we will need to wait and see (I guess it is matter of a year or two).
However, there is one prediction I can make with a high degree of confidence: Usage of the Data Binding library will not become an industry standard. I'm confident to say that because the Data Binding library (in its current implementation) provides short-term productivity gains and some kind of architectural guideline, but it will make the code non-maintainable in the long run. Once long-term effects of this library will surface - it will be abandoned.
Now, although we do have some sort of official guidelines and tools today, I, personally, don't think that these guidelines and tools are the best options available (and they are definitely not the only ones). In my applications I use my own implementation of an MVC architecture. It is simple, clean, readable and testable, and does not require any additional libraries.
This MVC is not just cosmetically different from others - it is based on a theory that Activities in Android are not UI Elements, which has tremendous implications on code organization.
So, if you're looking for a good architectural pattern for Android applications that follows SOLID principles, you can find a description of one in my post about MVC and MVP architectural patterns in Android.
When i reach this post it really help me to understand patterns with example so i have make below table to clearly see the Design patterns & their example in Android Framework
发布评论
评论(12)
Android 还使用 ViewHolder 设计模式。
它用于提高 ListView 滚动时的性能。
ViewHolder 设计模式使您无需查找即可访问每个列表项视图,从而节省宝贵的处理器周期。具体来说,它避免了 ListView 滚动期间频繁调用 findViewById(),这将使滚动变得流畅。
Android also uses the ViewHolder design pattern.
It's used to improve performance of a ListView while scrolling it.
The ViewHolder design pattern enables you to access each list item view without the need for the look up, saving valuable processor cycles. Specifically, it avoids frequent calls of findViewById() during ListView scrolling, and that will make it smooth.
所有这些模式,MVC、MVVM、MVP 和 演示模型,可以应用于Android应用程序,但如果没有第三方框架,不容易获得组织良好的结构和干净的代码。
MVVM起源于PresentationModel。当我们应用MVC、MVVM和演示模型到Android应用程序,我们真正想要的是有一个结构清晰的项目,更重要的是更容易进行单元测试。
目前,如果没有第三方框架,您通常会有大量代码(如 addXXListener()、findViewById() 等),这些代码不会增加任何业务价值。此外,您必须运行 Android 单元测试,而不是普通的 JUnit 测试,这需要很长时间才能运行,并且使单元测试有些不切实际。
出于这些原因,几年前我们启动了一个开源项目,RoboBinding - Android 的数据绑定演示模型框架平台。 RoboBinding 可帮助您编写更易于阅读、测试和维护的 UI 代码。 RoboBinding 消除了不必要的代码(例如 addXXListener 等),并将 UI 逻辑转移到表示模型,这是一个POJO 并可以通过普通 JUnit 测试进行测试。 RoboBinding 本身附带了 300 多项 JUnit 测试,以确保其质量。
All these patterns, MVC, MVVM, MVP, and Presentation Model, can be applied to Android apps, but without a third-party framework, it is not easy to get well-organized structure and clean code.
MVVM is originated from PresentationModel. When we apply MVC, MVVM, and Presentation Model to an Android app, what we really want is to have a clear structured project and more importantly easier for unit tests.
At the moment, without an third-party framework, you usually have lots of code (like addXXListener(), findViewById(), etc.), which does not add any business value. What's more, you have to run Android unit tests instead of normal JUnit tests, which take ages to run and make unit tests somewhat impractical.
For these reasons, some years ago we started an open source project, RoboBinding - A data-binding Presentation Model framework for the Android platform. RoboBinding helps you write UI code that is easier to read, test, and maintain. RoboBinding removes the need of unnecessary code like addXXListener or so, and shifts UI logic to the Presentation Model, which is a POJO and can be tested via normal JUnit tests. RoboBinding itself comes with more than 300 JUnit tests to ensure its quality.
我想添加一个已在Android Framework中应用的设计模式。这是 Asynctask 实现中使用的半同步半异步模式。请参阅我的讨论:
https://docs.google.com/document /d/1_zihWXAwgTAdJc013-bOLUHPMrjeUBZnDuPkzMxEEj0/edit?usp=sharing
I would like to add a design pattern that has been applied in Android Framework. This is Half Sync Half Async pattern used in the Asynctask implementation. See my discussion at
https://docs.google.com/document/d/1_zihWXAwgTAdJc013-bOLUHPMrjeUBZnDuPkzMxEEj0/edit?usp=sharing
在 Android 中,“工作队列处理器”模式通常用于从应用程序的主线程卸载任务。
示例:IntentService类的设计。
IntentService 接收 Intent,启动工作线程,并根据需要停止服务。所有请求都在单个工作线程上处理。
In Android the "work queue processor" pattern is commonly used to offload tasks from an application's main thread.
Example: The design of the IntentService class.
The IntentService receives the Intents, launch a worker thread, and stops the service as appropriate.All requests are handled on a single worker thread.
Binder 使用“观察者模式”来发送死亡接收者通知。
Binder uses "Observer Pattern" for Death Recipient notifications.
我尝试使用 模型–视图–控制器 (MVC) 和模型-视图-演示者 用于进行 Android 开发的架构模式。我的发现是模型-视图-控制器工作正常,但存在一些“问题”。这一切都取决于您如何看待 Android
Activity
类。它是一个控制器,还是一个视图?实际的
Activity
类并不扩展 Android 的View
类,但它确实处理向用户显示窗口并处理该窗口的事件(onCreate、 onPause 等)。这意味着,当您使用 MVC 模式时,您的控制器实际上是一个伪视图控制器。由于它正在处理向用户显示窗口,以及您使用 setContentView 添加到其中的附加视图组件,并且还处理至少各种活动生命周期事件的事件。
在 MVC 中,控制器应该是主要入口点。如果将其应用到 Android 开发中,则存在一些争议,因为 Activity 是大多数应用程序的自然入口点。
正因为如此,我个人认为模型-视图-演示者模式非常适合Android开发。由于视图在此模式中的角色是:
这允许您像这样实现模型:
View - 这包含您的 UI 组件并处理事件对于他们来说。
演示者 - 这将处理模型和视图之间的通信,将其视为模型的网关。这意味着,如果您有一个复杂的领域模型,上帝知道什么,并且您的视图只需要该模型的一个非常小的子集,那么演示者的工作就是查询模型,然后更新视图。例如,如果您有一个包含文本段落、标题和字数的模型。但在给定的视图中,您只需要在视图中显示标题。然后演示者将从模型中读取所需的数据,并相应地更新视图。
模型 - 这基本上应该是您的完整域模型。希望它也有助于使您的领域模型更加“紧密”,因为您不需要特殊的方法来处理上述情况。
通过将模型与视图完全解耦(通过使用演示器),测试模型也变得更加直观。您可以为域模型进行单元测试,也可以为演示者进行单元测试。
尝试一下。我个人认为它非常适合 Android 开发。
I tried using both the model–view–controller (MVC) and model–view–presenter architectural patterns for doing android development. My findings are model–view–controller works fine, but there are a couple of "issues". It all comes down to how you perceive the Android
Activity
class. Is it a controller, or is it a view?The actual
Activity
class doesn't extend Android'sView
class, but it does, however, handle displaying a window to the user and also handle the events of that window (onCreate, onPause, etc.).This means, that when you are using an MVC pattern, your controller will actually be a pseudo view–controller. Since it is handling displaying a window to the user, with the additional view components you have added to it with setContentView, and also handling events for at least the various activity life cycle events.
In MVC, the controller is supposed to be the main entry point. Which is a bit debatable if this is the case when applying it to Android development, since the activity is the natural entry point of most applications.
Because of this, I personally find that the model–view–presenter pattern is a perfect fit for Android development. Since the view's role in this pattern is:
This allows you to implement your model like so:
View - this contains your UI components, and handles events for them.
Presenter - this will handle communication between your model and your view, look at it as a gateway to your model. Meaning, if you have a complex domain model representing, God knows what, and your view only needs a very small subset of this model, the presenters job is to query the model and then update the view. For example, if you have a model containing a paragraph of text, a headline and a word-count. But in a given view, you only need to display the headline in the view. Then the presenter will read the data needed from the model, and update the view accordingly.
Model - this should basically be your full domain model. Hopefully it will help making your domain model more "tight" as well, since you won't need special methods to deal with cases as mentioned above.
By decoupling the model from the view all together (through use of the presenter), it also becomes much more intuitive to test your model. You can have unit tests for your domain model, and unit tests for your presenters.
Try it out. I personally find it a great fit for Android development.
2018 年 11 月更新
在 Android 中的 MVC 和 MVP 工作并撰写博客数年之后(请参阅下面的答案正文),我决定以更全面且易于理解的形式捕捉我的知识和理解。
因此,我发布了一个有关 Android 应用程序架构的完整视频课程。因此,如果您有兴趣掌握 Android 开发中最先进的架构模式,请查看这个综合课程在这里。
此答案已更新,以便截至 2016 年 11 月保持相关性
您似乎正在寻找 架构模式而不是设计模式。
设计模式旨在描述程序员为处理一组特定的重复软件任务而可能实现的一般“技巧”。例如:在 OOP 中,当需要一个对象向一组其他对象通知某些事件时,
由于 Android 应用程序(以及大多数 AOSP)是用面向对象的 Java 编写的,因此我认为您将很难找到 Android 上未使用的单一 OOP 设计模式。
另一方面,架构模式并不解决特定的软件任务 - 它们旨在根据相关软件组件的用例为软件组织提供模板。
听起来有点复杂,但我希望一个例子能够澄清:如果某个应用程序将用于从远程服务器获取数据并以结构化方式将其呈现给用户,那么 MVC 可能是一个值得考虑的不错选择。请注意,我没有提及应用程序的软件任务和程序流程 - 我只是从用户的角度描述了它,并且出现了架构模式的候选者。
既然您在问题中提到了 MVC,我猜您正在寻找架构模式。
从历史上看,Google 没有关于应用程序架构的官方指南,这(除其他原因外)导致 Android 应用程序的源代码完全混乱。事实上,即使在今天,我看到的大多数应用程序仍然没有遵循 OOP 最佳实践,并且没有显示清晰的代码逻辑组织。
但今天情况有所不同 - Google 最近发布了数据绑定库 与 Android Studio 完全集成,甚至还推出了一套 Android 架构蓝图应用程序。
两年前,很难在 Android 上找到有关 MVC 或 MVP 的信息。如今,MVC、MVP 和 MVVM 已成为 Android 社区中的“流行语”,我们周围有无数的专家,他们不断地试图让我们相信 MVx 比 MVy 更好。在我看来,讨论 MVx 是否比 MVy 更好是完全没有意义的,因为这些术语本身非常模糊 - 只要看看 这个问题,你会意识到不同的人可以将这些缩写与完全不同的结构联系起来。
由于对 Android 最佳架构模式的搜索已经正式开始,我想我们将会看到更多的想法浮出水面。在这一点上,确实无法预测未来哪种模式(或多种模式)将成为行业标准——我们需要拭目以待(我猜这只是一两年的事情)。
然而,我可以非常有信心地做出一个预测:数据绑定库的使用不会成为行业标准。我可以自信地说,因为数据绑定库(在当前的实现中)提供了短期生产力提升和某种架构指南,但从长远来看,它将使代码变得不可维护。一旦这个库的长期影响显现出来——它将被放弃。
现在,尽管我们今天确实有某种官方指南和工具,但我个人并不认为这些指南和工具是可用的最佳选择(而且它们绝对不是唯一的选择)。在我的应用程序中,我使用自己的 MVC 架构实现。它简单、干净、可读且可测试,并且不需要任何额外的库。
这个 MVC 不仅仅在外观上与其他 MVC 有所不同 - 它基于这样一种理论:Android 中的 Activity 不是 UI元素,这对代码组织有巨大的影响。
因此,如果您正在为 Android 应用程序寻找一个良好的架构模式,并且遵循 SOLID< /a> 原则,您可以在我关于 MVC 和 MVP 的文章中找到其中一个描述Android 中的架构模式。
Update November 2018
After working and blogging about MVC and MVP in Android for several years (see the body of the answer below), I decided to capture my knowledge and understanding in a more comprehensive and easily digestible form.
So, I released a full blown video course about Android applications architecture. So, if you're interested in mastering the most advanced architectural patterns in Android development, check out this comprehensive course here.
This answer was updated in order to remain relevant as of November 2016
It looks like you are seeking for architectural patterns rather than design patterns.
Design patterns aim at describing a general "trick" that programmer might implement for handling a particular set of recurring software tasks. For example: In OOP, when there is a need for an object to notify a set of other objects about some events, the observer design pattern can be employed.
Since Android applications (and most of AOSP) are written in Java, which is object-oriented, I think you'll have a hard time looking for a single OOP design pattern which is NOT used on Android.
Architectural patterns, on the other hand, do not address particular software tasks - they aim to provide templates for software organization based on the use cases of the software component in question.
It sounds a bit complicated, but I hope an example will clarify: If some application will be used to fetch data from a remote server and present it to the user in a structured manner, then MVC might be a good candidate for consideration. Note that I said nothing about software tasks and program flow of the application - I just described it from user's point of view, and a candidate for an architectural pattern emerged.
Since you mentioned MVC in your question, I'd guess that architectural patterns is what you're looking for.
Historically, there were no official guidelines by Google about applications' architectures, which (among other reasons) led to a total mess in the source code of Android apps. In fact, even today most applications that I see still do not follow OOP best practices and do not show a clear logical organization of code.
But today the situation is different - Google recently released the Data Binding library, which is fully integrated with Android Studio, and, even, rolled out a set of architecture blueprints for Android applications.
Two years ago it was very hard to find information about MVC or MVP on Android. Today, MVC, MVP and MVVM has become "buzz-words" in the Android community, and we are surrounded by countless experts which constantly try to convince us that MVx is better than MVy. In my opinion, discussing whether MVx is better than MVy is totally pointless because the terms themselves are very ambiguous - just look at the answers to this question, and you'll realize that different people can associate these abbreviations with completely different constructs.
Due to the fact that a search for a best architectural pattern for Android has officially been started, I think we are about to see several more ideas come to light. At this point, it is really impossible to predict which pattern (or patterns) will become industry standards in the future - we will need to wait and see (I guess it is matter of a year or two).
However, there is one prediction I can make with a high degree of confidence: Usage of the Data Binding library will not become an industry standard. I'm confident to say that because the Data Binding library (in its current implementation) provides short-term productivity gains and some kind of architectural guideline, but it will make the code non-maintainable in the long run. Once long-term effects of this library will surface - it will be abandoned.
Now, although we do have some sort of official guidelines and tools today, I, personally, don't think that these guidelines and tools are the best options available (and they are definitely not the only ones). In my applications I use my own implementation of an MVC architecture. It is simple, clean, readable and testable, and does not require any additional libraries.
This MVC is not just cosmetically different from others - it is based on a theory that Activities in Android are not UI Elements, which has tremendous implications on code organization.
So, if you're looking for a good architectural pattern for Android applications that follows SOLID principles, you can find a description of one in my post about MVC and MVP architectural patterns in Android.
当我到达这篇文章时,它确实帮助我通过示例理解模式,因此我制作了下表以清楚地看到设计模式和设计模式。他们在 Android 框架中的示例
我希望你会发现它有帮助。
一些有用的参考链接:
Android 设计模式简介< /a>
设计模式
When i reach this post it really help me to understand patterns with example so i have make below table to clearly see the Design patterns & their example in Android Framework
I hope you will find it helpful.
Some useful links for reference:
Introduction to Android Design Patterns
Design Patterns
Android 框架中使用了多种模式,例如:
There are various patterns used in Android framework like:
这是一篇关于Android 常见设计模式的精彩文章:
创建模式:
结构模式:
行为模式:
Here is a great article on Common Design Patterns for Android:
Creational patterns:
Structural patterns:
Behavioral patterns:
以下 Android 类使用设计模式
1) View Holder 使用 Singleton 设计模式
2) Intent 使用 Factory 设计模式
3) Adapter 使用 Adapter 设计模式
4) Broadcast Receiver 使用 Observer 设计模式
5) View 使用 Composite 设计模式
6) Media FrameWork 使用 Façade设计模式
The Following Android Classes uses Design Patterns
1) View Holder uses Singleton Design Pattern
2) Intent uses Factory Design Pattern
3) Adapter uses Adapter Design Pattern
4) Broadcast Receiver uses Observer Design Pattern
5) View uses Composite Design Pattern
6) Media FrameWork uses Façade Design Pattern
在 通知 情况下,
NotificationCompat.Builder< /code> 使用构建器模式
,例如
In the Notifications case, the
NotificationCompat.Builder
uses Builder Patternlike,