parent
0690729d42
commit
54179304da
@ -0,0 +1,32 @@
|
||||
package observer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class Child {
|
||||
private boolean cry = false;
|
||||
private static List<Observer> observers = new ArrayList<>();
|
||||
static {
|
||||
observers.add(new Dad());
|
||||
observers.add(new Mom());
|
||||
observers.add(new Dog());
|
||||
}
|
||||
|
||||
public void weakUp(){
|
||||
System.out.println("child weak up...");
|
||||
WeakUpEvent event = new WeakUpEvent(new Date(), "bed", this);
|
||||
for (Observer observer : observers){
|
||||
observer.actionOnWeakUp(event);
|
||||
}
|
||||
cry = true;
|
||||
}
|
||||
|
||||
public boolean isCry() {
|
||||
return cry;
|
||||
}
|
||||
|
||||
public void setCry(boolean cry) {
|
||||
this.cry = cry;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package observer;
|
||||
|
||||
public class Dad implements Observer{
|
||||
public void feed(){
|
||||
System.out.println("Dad feed child...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionOnWeakUp(WeakUpEvent event) {
|
||||
feed();
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package observer;
|
||||
|
||||
public class Dog implements Observer{
|
||||
public void bark(){
|
||||
System.out.println("Dog barking...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionOnWeakUp(WeakUpEvent event) {
|
||||
bark();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package observer;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Child child = new Child();
|
||||
child.weakUp();
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package observer;
|
||||
|
||||
public class Mom implements Observer{
|
||||
public void hug(){
|
||||
System.out.println("Mom hug child...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionOnWeakUp(WeakUpEvent event) {
|
||||
hug();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package observer;
|
||||
|
||||
public interface Observer {
|
||||
void actionOnWeakUp(WeakUpEvent event);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package observer;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class WeakUpEvent {
|
||||
private Date time;
|
||||
private String location;
|
||||
private Child source;
|
||||
|
||||
public WeakUpEvent(Date time, String location, Child source) {
|
||||
this.time = time;
|
||||
this.location = location;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public Child getSource() {
|
||||
return source;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue