欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java小白设计模式之观察者模式

程序员文章站 2022-04-28 09:16:11
观察者模式: 对象之间多对一依赖的一种设计方案,被依赖对象为Subject(一),依赖对象为Observer(多),Subject通知Observer变化直接代码: package com.wz.twoObserver;public interface Subject { public void a ......
观察者模式:
对象之间多对一依赖的一种设计方案,被依赖对象为Subject(一),依赖对象为Observer(多),Subject通知Observer变化
直接代码:
package com.wz.twoObserver;


public interface Subject {

public void add(Observer student);

public void del(Observer student);

public void mNotify();
}

package com.wz.twoObserver;

public interface Observer {

public void update(String name, String sex, String subject);

}

package com.wz.twoObserver;

import java.util.ArrayList;

/**
* 教师类
*
* @author Administrator
* @create 2018-04-18 11:50
*/
public class Teacher implements Subject{

private String name;

private String sex;
// 学科
private String subject;

private ArrayList<Observer> studentList;

// 记住要将集合初始化
public Teacher() {
this.studentList = new ArrayList<>();
}

public String getName() {
return name;
}

public String getSex() {
return sex;
}

public String getSubject() {
return subject;
}

public void teacherUpdate(){
mNotify();
}

public void setTeacher(String name, String sex, String subject){
this.name = name;
this.sex = sex;
this.subject = subject;
teacherUpdate();
}

@Override
public void add(Observer student) {
studentList.add(student);
}

@Override
public void del(Observer student) {
studentList.remove(student);
}

@Override
public void mNotify() {
for (int i = 0; i < studentList.size() ; i++) {
studentList.get(i).update(name,sex,subject);
}
}
}

package com.wz.twoObserver;

/**
* 学生类
*
* @author Administrator
* @create 2018-04-18 11:53
*/
public class Student implements Observer{

private String name;

private String sex;
// 学科
private String subject;

@Override
public void update(String name, String sex, String subject){
this.name = name;
this.sex = sex;
this.subject = subject;
display();
}

public void display(){
System.out.println("教师姓名:" + name);
System.out.println("教师的性别:" + sex);
System.out.println("教师的学科:" + subject);
System.out.println("=================================");
}

}

package com.wz.twoObserver;

/**
* 学生类
*
* @author Administrator
* @create 2018-04-18 11:53
*/
public class Student1 implements Observer{

private String name;

private String sex;
// 学科
private String subject;

@Override
public void update(String name, String sex, String subject){
this.name = name;
this.sex = sex;
this.subject = subject;
display();
}

public void display(){
System.out.println("教师姓名:" + name);
System.out.println("教师的性别:" + sex);
System.out.println("教师的学科:" + subject);
System.out.println("=================================");
}

}


package com.wz.twoObserver;

/**
* 学生类
*
* @author Administrator
* @create 2018-04-18 11:53
*/
public class Student2 implements Observer{

private String name;

private String sex;
// 学科
private String subject;

@Override
public void update(String name, String sex, String subject){
this.name = name;
this.sex = sex;
this.subject = subject;
display();
}

public void display(){
System.out.println("教师姓名:" + name);
System.out.println("教师的性别:" + sex);
System.out.println("教师的学科:" + subject);
System.out.println("=================================");
}

}

package com.wz.twoObserver;

/**
* 测试类
*
* @author Administrator
* @create 2018-04-18 12:12
*/
public class TestMain {

public static void main(String[] args) {
Teacher t1 = new Teacher();
Student s = new Student();;
Student1 s1 = new Student1();;
Student2 s2 = new Student2();

t1.add(s);
t1.add(s1);
t1.add(s2);
t1.setTeacher("郑爽","女","舞蹈");
System.out.println("======================================");
t1.del(s);
t1.setTeacher("杨洋","男","演技");



}

}


自我感觉这个模式不是特别的理解,看的视频是以天气预报为例,我这以教师和学生为例,开始郑爽老师有3个学生,后来换成杨洋老师了,学生s就退学了,就剩下学生s1和s2的