java - Clean Architecture: How to reflect the data layer's changes in the UI -


i'm trying make design based on uncle bob's clean architecture in android.

the problem:

i'd solve how make changes generated in 1 repository reflected in other parts of app, other repositories or views.

the example

i've designed simplified example example. please notice boundary interfaces has been removed keep diagrams small.

imagine app shows list of videos (with title, thumnail , count), clicking video can see detail (there can like/dislike video).

additionally app has statistics system counts number of videos user liked or disliked.

the main classes app be:

for videos part/module: enter image description here

for stats part/module: enter image description here

the target

now imagine check stats, navigate list of videos, open detail of one, , click button.

the after sent server, there several elements of apps should aware of change:

  • of course detail view, should updated changes (this can made though callbacks no problem)
  • the list of videos should update "likes" count given video
  • the `statsrepository may want update/invalidate caches after voting new video
  • if list of stats visible (imagine split screen) should show updated stats (or @ least receive event re-query data)

the question

what common patterns solve kind of communication? please make answer complete can, specifying events generated, how propagated though app, etc.

note: bounties given complete answers

publish / subscribe

typically, n:m communication (n senders may send message m receivers, while senders , receivers not know each other) you'll use publish/subscribe pattern. there lots of libraries implementing such communication style, java there example eventbus implementation in guava library. in-app communication these libraries typically called eventbus or eventmanager , send/receive events.

domain events

suppose created event videoratedevent, signals user has either liked or disliked video. these type of events referred domain events. event class simple pojo , might this:

class videoratedevent {     /** video rated */     public video video;     /** user triggered event */     public user user;     /** true if user liked video, false if user disliked video */     public boolean liked; } 

dispatch events

now each time users or dislike video, you'll need dispatch videoratedevent. guava, you'll pass instantiated event object object eventbus.post(myvideoratedevent). ideally events generated in domain objects , dispatched within persisting transaction (see this blog post details). means domain model state persisted, events dispatched.

event listeners

in application, components affected event can listen domain events. in particular example, videodetailview or statsrepository might event listeners videoratedevent. of course, need register guava eventbus eventbus.register(object).


Comments