观察者模式。简单demo

来源:互联网 发布:如何获得淘宝优惠券 编辑:程序博客网 时间:2024/06/06 21:06

观察者模式大致流程:

1.被观察者接口:Watched

2.观察者接口:Watcher

3.被观察者实现类:AchreveWatched,使用了单例模式

4.观察者实现类:MainActivity

Deme地址:https://github.com/zxp19920626/Obsever


具体的代码如下:

1.Watched被观察者接口

/** * 被观察者接口 * Created by zxp on 2016/4/14. */public interface Watched {   public void add(Watcher watcher);   public void remove(Watcher watcher);   public void notifyWatcher(int code, Object o);}

2.Watcher观察者接口

/** * 观察者接口 * Created by zxp on 2016/4/14. */public interface Watcher {   public void updataNotify(int code, Object o);}
3.AchieveWatched被观察者实现类(使用了单例模式)

/** * 被观察者的具体实现类 * Created by zxp on 2016/4/14. */public class AchieveWatched implements Watched {   private static AchieveWatched instance = new AchieveWatched();   private List<Watcher> list = new ArrayList<>();   public static AchieveWatched getInstance() {      return instance;   }   @Override   public void add(Watcher watcher) {      list.add(watcher);   }   @Override   public void remove(Watcher watcher) {      list.remove(watcher);   }   @Override   public void notifyWatcher(int code, Object o) {      for (Watcher watcher : list) {         watcher.updataNotify(code, o);      }   }}
4.MainActivity观察者实现类

public class MainActivity extends AppCompatActivity implements Watcher {   private TextView tv;   private Button bt;   private int number = 0;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      AchieveWatched.getInstance().add(this);      initView();      initEvent();   }   private void initView() {      tv = (TextView) findViewById(R.id.tv);      bt = (Button) findViewById(R.id.bt);   }   private void initEvent() {      bt.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {            number++;            Person person = new Person("Zengxiaoping" + number);            AchieveWatched.getInstance().notifyWatcher(CodeType.PERSON_CODE, person);         }      });   }   @Override   public void updataNotify(int code, Object o) {      switch (code) {         case CodeType.PERSON_CODE:            Person person = (Person) o;            tv.setText(person.getName());            break;      }   }   @Override   protected void onDestroy() {      super.onDestroy();      AchieveWatched.getInstance().remove(this);   }}
5.Person类

public class Person {   private String name;   public Person(String name) {      this.name = name;   }   public String getName() {      return name;   }   public void setName(String name) {      this.name = name;   }}
6.CodeType类

public class CodeType {   public static final int PERSON_CODE = 0;//person的code码}
7.xml布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context="com.observer.cn.observer.MainActivity">   <TextView      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:background="#349824"      android:padding="5dp"      android:text="观察者模式通知数据"/>   <TextView      android:id="@+id/tv"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:background="#349878"      android:padding="5dp"      android:text="这是数据"/>   <Button      android:id="@+id/bt"      android:clickable="true"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="刷新数据"/></LinearLayout>

 效果图:


1 0
原创粉丝点击