Observers vs. Callbacks

前端 未结 3 1324
星月不相逢
星月不相逢 2020-12-13 04:09

i thought about using observers or callbacks. What and when you should use an observer?

F.e. you could do following:

# User-model
class User <<         


        
3条回答
  •  臣服心动
    2020-12-13 04:54

    You can use observers as a means of decoupling or distribution of responsibility. In the basic sense - if your model code gets too messy start to think about using observers for some unessential behavior. The real power (at least as I see it) of observers lies in their ability to serve as a connection point between your models and some other subsystem whose functionality is used by all (or some) of the other classes. Let's say you decide to add an IM notification to your application - say you want to be notified about some (or all) of the CRUD actions of some (or all) of the models in your system. In this case using observers would be ideal - your notification subsystem will stay perfectly separated from your business logic and your models won't be cluttered with behavior which is not of their business. Another good use case for observers would be an auditing subsystem.

提交回复
热议问题