事件总线OTTO(助Android深层次解耦——跟回调说拜拜)

 <! -- http://blog.csdn.net/yuanyang5917/article/details/45482457 -- >

事件总线框架

针对事件提供统一订阅,发布以达到组件间通信的解决方案。


官方定义:

       Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.

原理

观察者模式 + 注解 + 反射


Otto实现篇

这里要注意几点点:
(1)Otto使用注解定义订阅/发布者的角色,@Subscribe为订阅者,@Produce为发布者,方法名称就可以自定义了。
(2)Otto为了性能,代码意图清晰,@Subscribe,@Produce方法必须定义在直接的作用类上,而不能定义在基类而被继承。
(3)订阅者/发布者均需要register和unregister,而EventBus的发布者是不需要的。
(4)事件参数数量只可以有一个,不可更多

  1. 定义事件-LocationChangedEvent ,作为组件间通信传递数据的载体

    /*
     * Copyright (C) 2012 Square, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.squareup.otto.sample;
    
    public class LocationChangedEvent {
      public final float lat;
      public final float lon;
    
      public LocationChangedEvent(float lat, float lon) {
        this.lat = lat;
        this.lon = lon;
      }
    
      @Override public String toString() {
        return new StringBuilder("(") //
            .append(lat) //
            .append(", ") //
            .append(lon) //
            .append(")") //
            .toString();
      }
    }
    
    
  2. 避免浪费,相对于BusProvider.getInstance(), Otto需要自己实现单例。

    /*
     * Copyright (C) 2012 Square, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.squareup.otto.sample;
    
    import com.squareup.otto.Bus;
    
    /**
     * Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means
     * such as through injection directly into interested classes.
     */
    public final class BusProvider {
      private static final Bus BUS = new Bus();
    
      public static Bus getInstance() {
        return BUS;
      }
    
      private BusProvider() {
        // No instances.
      }
    }
    
    
  3. 定义订阅者,在接受事件的方法加上修饰符@Subscribe

    /*
     * Copyright (C) 2012 Square, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.squareup.otto.sample;
    
    import android.os.Bundle;
    import android.support.v4.app.ListFragment;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Toast;
    
    import com.squareup.otto.Subscribe;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /** Maintain a scrollable history of location events. */
    public class LocationHistoryFragment extends ListFragment {
        private final List<String> locationEvents = new ArrayList<String>();
        private ArrayAdapter<String> adapter;
    
        @Override
        public void onResume() {
            super.onResume();
            BusProvider.getInstance().register(this);
        }
    
        @Override
        public void onPause() {
            super.onPause();
            BusProvider.getInstance().unregister(this);
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, locationEvents);
            setListAdapter(adapter);
        }
    
        @Subscribe
        public void onLocationChanged(LocationChangedEvent event) {
            locationEvents.add(0, event.toString());
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }
    
        /**
         * 方法随便取,参数只能唯一
         * 很方便
         * @param event
         */
        @Subscribe
        public void turboLocationChanged(LocationChangedEvent event) {
            Toast.makeText(getActivity(), event.toString(), Toast.LENGTH_LONG)
                    .show();
        }
    
        @Subscribe
        public void onLocationCleared(LocationClearEvent event) {
            locationEvents.clear();
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }
        }
    }
    
    
    
  4. 定义发布者,通过post()方法在任何地方发布消息了

    /*
     * Copyright (C) 2012 Square, Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.squareup.otto.sample;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import com.squareup.otto.Produce;
    
    import java.util.Random;
    
    import static android.view.View.OnClickListener;
    
    public class LocationActivity extends FragmentActivity {
      public static final float DEFAULT_LAT = 40.440866f;
      public static final float DEFAULT_LON = -79.994085f;
      private static final float OFFSET = 0.1f;
      private static final Random RANDOM = new Random();
    
      private static float lastLatitude = DEFAULT_LAT;
      private static float lastLongitude = DEFAULT_LON;
    
      @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_history);
    
        findViewById(R.id.clear_location).setOnClickListener(new OnClickListener() {
          @Override public void onClick(View v) {
            // Tell everyone to clear their location history.
            BusProvider.getInstance().post(new LocationClearEvent());
    
            // Post new location event for the default location.
            lastLatitude = DEFAULT_LAT;
            lastLongitude = DEFAULT_LON;
            BusProvider.getInstance().post(produceLocationEvent());
          }
        });
    
        findViewById(R.id.move_location).setOnClickListener(new OnClickListener() {
          @Override public void onClick(View v) {
            lastLatitude += (RANDOM.nextFloat() * OFFSET * 2) - OFFSET;
            lastLongitude += (RANDOM.nextFloat() * OFFSET * 2) - OFFSET;
            BusProvider.getInstance().post(produceLocationEvent());
          }
        });
      }
    
      @Override protected void onResume() {
        super.onResume();
    
        // Register ourselves so that we can provide the initial value.
        BusProvider.getInstance().register(this);
      }
    
      @Override protected void onPause() {
        super.onPause();
    
        // Always unregister when an object no longer should be on the bus.
        BusProvider.getInstance().unregister(this);
      }
    
      @Produce public LocationChangedEvent produceLocationEvent() {
        // Provide an initial value for location based on the last known position.
        return new LocationChangedEvent(lastLatitude, lastLongitude);
      }
    }
    
    

DOWNLOAD ADDRESS(DEMO下载地址): http://download.csdn.net/download/yuanyang5917/8660175




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。