开发者

How can I separate Logic/UI in Android

I want as my application to be struct开发者_StackOverflow社区ured in 2 parts: the logic code and the UI.

I've tried to implement that using a controller class(here I keep the logic code) inside of each activity. The activity send messages to controller and receive the answer in two ways:

  1. the answer is returned immediately (if the action is not complex and it can be done in a verry short time)
  2. the activity set some listeners and the controller fire this listener when the action is complete.

    The problems appears when the controller have a lot of objects(each object should handle a set of actions and for each action I have to set & trigger a listener): it is hard to keep the code syncronized.

    I'm asking if you know a better way to implement this mechanism.

    Thank you.


Personally, I consider the Activity to be the controller. Widgets are the view. Others may disagree, and I am far from an MVC purist.


Android methodology is pretty MVC.

Start by getting good at Views, then look at extending things as you see fit.

The views connect to the controller via the UI thread, which is the main thread of the application. You can define callbacks and such in the XML and handle all button clicks and such in this thread by just using the android xml methodology.

You should consider XML to be the View, UI Thread the Controller, and Background Threads/Services/Broadcast Receivers etc to all be part of the model.


You can follow "Model View Presenter(MVP)" pattern which pretty much organises the code and keep the code segments clear.

I like the dummy view approach where all views events (like onClick, onLongClick.. ) are delegated to presenter and presenter takes care of the business logic and updates the View back.

Consider a scenario where user is adding some text to his note and In view, we have a textbox, texfield to indicate the status and add button.


User types text and clicks add button.

AddButton: "Hey Presenter! User wants to add something".

Presenter: "Give me your text, TextBox".

TextBox: gives the text typed.

Presenter: Validates the text with buisness logic. adds item to database using Model classes and sends the status text to StatusTextView to show.

StatusTextView: updates its text which is sent by the presenter.


As you can see, Views do what Presenter says. You can have different flavours of Presenters to act differently to user interactions.

Some points to consider:

  • Keep presenter out of android classes so that you can unit test it separately later.
  • Connect view and presenter with interfaces (contracts) so that you can plug different views and presenters later with changing app requirements
  • Use Dependency Injection frameworks like Dagger2 to even more decouple the hard dependencies.

Google about MVP and choose your preferred way of implementation. Only con I felt in MVP is your code will become huge yet clear.

Few links:

https://medium.com/cr8resume/make-you-hand-dirty-with-mvp-model-view-presenter-eab5b5c16e42

https://www.journaldev.com/14886/android-mvp

Happy coding :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜