Android 关于观察者模式使用代码总结

public class Person extends Observable {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
        setChanged();
        notifyObservers();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        setChanged();
        notifyObservers();
    }
}
package com.example.yqs.myapplication;

import android.util.Log;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by yqs on 2017/7/5.
 */

public class People implements Observer {
    private int id;
    private Person person;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("观察者---->" + id + "收到消息");
        this.person = (Person) o;
        Log.i("PEOPLE",person.getName());
    }
}
package com.example.yqs.myapplication;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List peoples = new ArrayList<>();
    private Person person;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        // toolbar.setNavigationIcon(R.mipmap.ic_launcher);//设置导航栏图标
        setSupportActionBar(toolbar);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        person = new Person();
        person.setId(110);
        person.setName("我是警察");

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(MainActivity.this, SecondActivity.class));
            People people = new People();
                people.setId(i+1);

                person.addObserver(people);
                peoples.add(people);

            }
        });

        Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //startActivity(new Intent(MainActivity.this, SecondActivity.class));
               person.setName("我是警察。。。。");

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

运行结果:
07-05 18:09:00.875 26534-26534/com.example.yqs.myapplication I/System.out: 观察者—->1收到消息
07-05 18:09:00.875 26534-26534/com.example.yqs.myapplication I/PEOPLE: 我是警察。。。。
07-05 18:09:03.007 26534-26534/com.example.yqs.myapplication I/System.out: 观察者—->1收到消息
07-05 18:09:03.007 26534-26534/com.example.yqs.myapplication I/PEOPLE: 我是警察。。。。
07-05 18:09:03.007 26534-26534/com.example.yqs.myapplication I/System.out: 观察者—->1收到消息
07-05 18:09:03.007 26534-26534/com.example.yqs.myapplication I/PEOPLE: 我是警察。。。。

你可能感兴趣的:(代码草稿)