package idv.jk.study.designpattern.observer;
import java.util.ArrayList;
import java.util.List;
public abstract class Subject
{
private String mMessage;
List mObserverList = new ArrayList();
public void add(Observer observer)
{
mObserverList.add(observer);
}
public void remove(Observer observer)
{
mObserverList.remove(observer);
}
public void broadcast()
{
for(Observer o : mObserverList)
{
o.update();
}
}
public String getMessage()
{
return mMessage;
}
public void setMessage(String message)
{
this.mMessage = message;
}
}
Observer.java
package idv.jk.study.designpattern.observer;
public abstract class Observer
{
protected String mName;
protected Subject mSubject;
public abstract void update();
}
Sheep.java
package idv.jk.study.designpattern.observer;
public class Sheep extends Observer
{
public Sheep(String name, Subject subject)
{
this.mName = name;
this.mSubject = subject;
subject.add(this);
}
@Override
public void update()
{
System.out.printf("通知 %s:%s%n", mName, mSubject.getMessage());
}
}
SheepDog.java
package idv.jk.study.designpattern.observer;
public class SheepDog extends Subject
{
}
Main.java
package idv.jk.study.designpattern.observer;
public class Main
{
public static void main(String[] args)
{
//Shaun the sheep中的那隻牧羊犬
SheepDog bitzer = new SheepDog();
//chief actor
Sheep shaun = new Sheep("Shaun", bitzer);
//Shaun's cousin, is the flock's only lamb,
Sheep timmy = new Sheep("Timmy", bitzer);
//Shirley is the largest member of the flock
Sheep shirley = new Sheep("Shirley", bitzer);
//Nuts, is quite an eccentric, but useful sheep and usually like the rest of the flock, accompanies and helps Shaun.
Sheep nuts = new Sheep("Nuts", bitzer);
bitzer.setMessage("主人回來了,快點恢復正常羊的樣子");
bitzer.broadcast();
System.out.println("晚上了...");
//晚上了,Timmy比較早睡,Bitzer就不通知他
bitzer.remove(timmy);
bitzer.setMessage("星星出來了,大家快來看啊");
bitzer.broadcast();
}
}
package idv.jk.study.designpattern.memoto;
/**
* Created by bioyang on 2015/10/12.
*/
public class Main
{
public static void main(String[] args)
{
//有一個叫小明的工程師
Programmer smallMing = Programmer.beforeWork();
//他在上班前的身心狀態
System.out.printf("開始工作前的HP為 %d, 憤怒條為 %d\r\n",
smallMing.getHitPoint(), smallMing.getAngryPoint());
//為了確認明天是全新的一天,他的大腦要把上班前的狀態給記下來
ProgrammerBrain brainOfSmallMing = new ProgrammerBrain();
brainOfSmallMing.setBodyStateMemoto(smallMing.getBodyStateMemoto());
//小明修了一個Bug
smallMing.fixBug();
//這對他的身心狀態沒有太大的影響
System.out.printf("修了一個Bug的HP為 %d, 憤怒條為 %d\r\n",
smallMing.getHitPoint(), smallMing.getAngryPoint());
//客戶改了需求
smallMing.requirementChanged();
//要殺死一個工程師,只要改一個需求
System.out.printf("知道客戶改了需求後的HP為 %d, 憤怒條為 %d\r\n",
smallMing.getHitPoint(), smallMing.getAngryPoint());
//但一天過去了,又是美好、新的一天
smallMing.recoverBodyStateMemoto(brainOfSmallMing.getBodyStateMemoto());
System.out.printf("隔天起床工作前的HP為 %d, 憤怒條為 %d\r\n",
smallMing.getHitPoint(), smallMing.getAngryPoint());
}
}
Programmer.java
package idv.jk.study.designpattern.memoto;
/**
* Created by bioyang on 2015/10/12.
*/
public class Programmer
{
/**
* 生命值
*/
private int hitPoint;
/**
* 憤怒條
*/
private int angryPoint;
private BodyStateMemoto mBodyStateMemoto;
public BodyStateMemoto saveState()
{
return new BodyStateMemoto(this.hitPoint, this.angryPoint);
}
public static Programmer beforeWork()
{
Programmer programmer = new Programmer();
programmer.setHitPoint(100);
programmer.setAngryPoint(0);
return programmer;
}
public void fixBug()
{
this.hitPoint -= 5;
this.angryPoint += 10;
}
public void requirementChanged()
{
this.hitPoint = 0;
this.angryPoint = 200;
}
public BodyStateMemoto getBodyStateMemoto()
{
return new BodyStateMemoto(getHitPoint(), getAngryPoint());
}
public void recoverBodyStateMemoto(BodyStateMemoto bodyStateMemoto)
{
setHitPoint(bodyStateMemoto.getHitPoint());
setAngryPoint(bodyStateMemoto.getAngryLevel());
}
public int getHitPoint()
{
return hitPoint;
}
public void setHitPoint(int hitPoint)
{
this.hitPoint = hitPoint;
}
public int getAngryPoint()
{
return angryPoint;
}
public void setAngryPoint(int angryPoint)
{
this.angryPoint = angryPoint;
}
}
BodyStateMemoto.java
package idv.jk.study.designpattern.memoto;
/**
* Created by bioyang on 2015/10/12.
*/
public class BodyStateMemoto
{
/**
* 生命力
*/
private int hitPoint;
/**
* 憤怒值
*/
private int angryLevel;
public BodyStateMemoto(int hitPoint, int angryLevel)
{
this.hitPoint = hitPoint;
this.angryLevel = angryLevel;
}
public int getHitPoint()
{
return hitPoint;
}
public void setHitPoint(int hitPoint)
{
this.hitPoint = hitPoint;
}
public int getAngryLevel()
{
return angryLevel;
}
public void setAngryLevel(int angryLevel)
{
this.angryLevel = angryLevel;
}
}
ProgrammerBrain.java
package idv.jk.study.designpattern.memoto;
/**
* Created by bioyang on 2015/10/12.
*/
public class ProgrammerBrain
{
private BodyStateMemoto mBodyStateMemoto;
public void setBodyStateMemoto(BodyStateMemoto bodyStateMemoto)
{
this.mBodyStateMemoto = bodyStateMemoto;
}
public BodyStateMemoto getBodyStateMemoto()
{
return mBodyStateMemoto;
}
}