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

2018-06-18 01:59:24来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

观察者模式:
对象之间多对一依赖的一种设计方案,被依赖对象为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的



标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:mybatis要指定参数再数据库中的类型,类型要全大写

下一篇:创建SpringBoot项目