通过使用片段视图和活动的控制器MVC设计模式为Android应用程序开发视图、控制器、程序开发、片段

2023-09-05 05:51:19 作者:凉初透

我最近读片段(没有用在我的应用程序,虽然),得知它可以在手机应用程序的开发中使用。

I was recently reading Fragments( haven't used this in my app though) and learnt that it can be used in phones app development.

我想利用片段来实现MVC(模型 - 视图 - 控制器)设计模式。许多人认为,Android开发符合MVC默认。但我确实看到活动很像的意见和有一定的欠缺,当另一个活动是一个控制器/由于推出的。所以我想使用的片段作为查看有一个活动作为控制器和交换/添加/如需要时,取下的碎片。

I am thinking about using Fragments to implement MVC(model-view-controller) design pattern. Many argue that android development complies with MVC by default . But i do see Activities much like Views and there's definite lack of a controller when another activity is being/due to be launched. So i am thinking of using "Fragments" as "Views" with a single "Activity" as "Controller" and swap/add/remove the fragments as and when needed.

所以我的基本做法是这样的。

So my basic approach is like this.

1)的用户交互的用户界面(片段)。

1) The user interacts with the user interface (Fragments).

2)控制器(活动)处理来自片段的事件并将其传递到一个模型(Backendthread /服务)。

2) The controller (Activity) handles the event from the Fragments and passes it to a model(Backendthread / Service).

3)模型(Backendthread /服务)通知模型状态变化控制器。

3) Model(Backendthread / Service) notifies the controller of models state change.

4),控制器(活动)notifes所述的UserInterface(片段),其inturn通知用户。

4) The controller (Activity) notifes the UserInterface(Fragments) which inturn notifies the User.

做我的做法是仪式或不必要的开销还是我的感悟片段是错误的?

does my approach is rite or an unnecessary overhead or my perception about fragments is wrong?

请澄清我。

推荐答案

恕我直言,碎片控制器。该片段的篮下小部件重新presents的看法。活动是业务流程层,确定哪些片段(及其部件)都需要在特定的环境(例如,一个在手机上,两人在片),但以其他方式有限的业务逻辑。

IMHO, fragments are the controller. The fragment's basket of widgets represents the view. Activities are an orchestration layer, determining what fragments (and their widgets) are needed in a given circumstance (e.g., one on a phone, two on a tablet) but otherwise having limited business logic.

话虽这么说,Android和类似的MVC模式并不一定要在一起。我不认为谷歌的意图是建立一个纯粹的MVC框架。

That being said, Android and patterns like MVC don't necessarily go together. I don't think that Google's intention was to create a pure MVC framework.