吉大观察者模式,陈老师在超星学习通上新发布的作业,明天中午就交了,狂肝(补)一波~
package com.guangluo.code;
import java.util.ArrayList;
import java.util.Iterator;
interface Observer{
public void update(Subject theChangedSubject);
}
class Subject{
public ArrayList<Observer> observers = new ArrayList<Observer>();
public void atach(Observer obs){
observers.add(obs);
}
public void dtach(Observer obs){
observers.remove(obs);
}
public void info() { // 现在领会到了这种广播机制,我把息传给每个人,具体是不是大伙们的任务工作自己判断去~
Iterator<Observer> it = observers.iterator();
while(it.hasNext()) {
Observer obs = (Observer)it.next();
obs.update(this);
}
}
}
class IFunc{
public int f(int a, int b) {
return 0;
}
}
class Add extends IFunc{
public int f(int a, int b) {
return a + b;
}
}
class Multi extends IFunc{
public int f(int a, int b) {
return a * b;
}
}
class Cell extends Subject implements Observer{
private Cell mcell1, mcell2; // 每个观察者具体观察两个对象
private IFunc mIFunc; // 两个观察对象的计算模式也存储下来
private int mvalue = 0, mRow, mCol;
Cell(int r, int c){
this.mRow = r;
this.mCol = c;
}
public void setVal(int value) {
this.mvalue = value;
this.mIFunc = null; // 人为赋值,去掉自身原有的计算公式
this.info(); // 作为对象来说,自身值一旦发生改变,应及时通知观察者
}
public int val() {
return this.mvalue;
}
public void setFormula(IFunc fnObj, Cell cell1, Cell cell2) {
this.mvalue = fnObj.f(cell1.val(), cell2.val());
cell1.atach(this); // c=a+b,则c是观察者,a和b是对象,即给对象a b增加观察者c(c就是当前对象)
cell2.atach(this);
this.mcell1 = cell1; // 同时给观察者(当前对象)赋上所观察的两个对象
this.mcell2 = cell2;
this.mIFunc = fnObj; // 同时把计算规则给传进来
}
public void reset() {
this.mvalue = 0; // value值清零
this.mIFunc = null; // 计算规则清除
this.info(); // 作为对象来说,自身值一旦发生改变,应及时通知观察者
}
@Override
public void update(Subject theChangedSubject) {
// TODO Auto-generated method stub
if(mcell1!=null && mcell2!=null && (theChangedSubject == mcell1 || theChangedSubject == mcell2)) {
this.mvalue = this.mIFunc.f(this.mcell1.val(), this.mcell2.val());
// setFormula(this.mIFunc, this.mcell1, this.mcell2); //???你setFormula个什么,人家的表达式又没变,只是根据自己所观察的那两个对象值的更新而改变
this.info(); // 注意:不单单是把自己作为观察者进行相应值的修改,同时还要把自身作为对象的情况进行考虑,即:继续通知把自己作为观察对象的观察者!!!
}
}
}
public class Grid2{
private Cell[][] item;
private int m, n;
public Grid2(int m, int n) {
this.m = m;
this.n = n;
this.item = new Cell[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
Cell cell = new Cell(i, j);
this.item[i][j] = cell;
}
}
}
public Cell item(int x, int y){
return this.item[x][y];
}
public void show() {
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
System.out.print(this.item[i][j].val() + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Add addFuncObject = new Add();
Multi multiFuncObject = new Multi();
Grid2 grid2 = new Grid2(2, 4);
grid2.item(0, 0).setVal(1);
grid2.item(0, 1).setVal(2);
grid2.item(0, 3).setVal(4);
grid2.show( );
System.out.println();
grid2.item(0, 2).setFormula(addFuncObject, grid2.item(0, 0), grid2.item(0, 1));
grid2.item(0, 3).setFormula(multiFuncObject, grid2.item(0, 1), grid2.item(0, 2));
grid2.show( );
System.out.println();
grid2.item(1, 0).setFormula(addFuncObject, grid2.item(0, 0), grid2.item(0, 3));
grid2.item(1, 1).setFormula(multiFuncObject, grid2.item(0, 1), grid2.item(0, 3));
grid2.show( );
System.out.println();
grid2.item(0, 2).setVal(5);
grid2.show();
System.out.println();
grid2.item(1, 2).setFormula(addFuncObject, grid2.item(0, 2), grid2.item(1, 1));
grid2.show( );
System.out.println();
grid2.item(1, 1).reset( );
grid2.show( );
}
}