Android 接口回调
天蓬老师
天蓬老师 2017-04-17 16:48:24
0
6
287
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(6)
刘奇

It’s not an observer mode, but it can be implemented

左手右手慢动作

It cannot be said that interface callback is the observer pattern, but the observer pattern uses interface callback

大家讲道理

Interface callbacks are not considered observer patterns

Example of callback: In Java, objects can be passed as parameters of methods. When passing a class that implements a certain interface in the parameters, you can call the parameter object in this method. Generally, you are calling method A. When passing an anonymous object b that implements the B interface (it is said to be anonymous because it is not copied to a named reference, such as B b = new B(), it is not an anonymous object), then inside the A method, you Then you can call the method of object b. At this time, if you call the method of b, you will enter the method of object b that overrides the B interface. This is how the callback is implemented.

Observer pattern: refers to when there is an observed object, the observed object usually holds a reference to the observer object (various Listeners are observer objects, usually there is a List inside the observed object to hold these references), a The observed object can register monitoring for multiple observation objects (observe changes in this object). When the content of the observed object changes, the internal method will be called to traverse all observer objects (various Listeners, such as OnClickListener in View). etc.), call the corresponding content change methods of these listener objects, notify the observer of event changes, and allow the observer to make corresponding changes. Therefore, the observer pattern is only implemented with the help of callbacks. It cannot be said that the callback is the observer. model.

巴扎黑

In fact, according to what the original poster really wants to ask, it can be said that your understanding is correct. The most common example of the observer pattern is the callback function

刘奇

No, but the observer pattern is implemented through callbacks

阿神

It is the observer mode, but it is a special observer mode. The interface callback is one-to-one, and the observer pattern is one-to-many. The observer pattern is just a nice name for the interface callback, but the principle is still the same.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!