diff --git a/src/main/java/com/msb/observer/v7/Child.java b/src/main/java/com/msb/observer/v7/Child.java new file mode 100644 index 0000000..4ce2a19 --- /dev/null +++ b/src/main/java/com/msb/observer/v7/Child.java @@ -0,0 +1,40 @@ +package com.msb.observer.v7;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v2 + * @Version: 1.0 + */ + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +/** + *@ClassName Child + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Child { + + private boolean cry = false; + private List observers = new ArrayList<>(); + + { + observers.add(new Dad()); + observers.add(new Mum()); + } + + public boolean isCry() { + return cry; + } + + public void wakeUp() { + this.cry = true; + WakeUpEvent event = new WakeUpEvent(LocalDateTime.now(), "bed", this); + for (Observer observer : observers) { + observer.actionOnWakeUp(event); + } + } +} diff --git a/src/main/java/com/msb/observer/v7/Dad.java b/src/main/java/com/msb/observer/v7/Dad.java new file mode 100644 index 0000000..977e0de --- /dev/null +++ b/src/main/java/com/msb/observer/v7/Dad.java @@ -0,0 +1,25 @@ +package com.msb.observer.v7;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v3 + * @Version: 1.0 + */ + +/** + *@ClassName Dad + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Dad implements Observer { + public void feed() { + System.out.println("孩子醒了,爸爸给孩子喂奶……"); + } + @Override + public void actionOnWakeUp(WakeUpEvent event) { + if(event.getSource().isCry() && "hungry".equals(event.getLocation())) { + feed(); + } + } +} diff --git a/src/main/java/com/msb/observer/v7/Mum.java b/src/main/java/com/msb/observer/v7/Mum.java new file mode 100644 index 0000000..a7f760d --- /dev/null +++ b/src/main/java/com/msb/observer/v7/Mum.java @@ -0,0 +1,25 @@ +package com.msb.observer.v7;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v3 + * @Version: 1.0 + */ + +/** + *@ClassName Dad + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Mum implements Observer { + public void hug() { + System.out.println("孩子醒了,妈妈抱抱孩子……"); + } + @Override + public void actionOnWakeUp(WakeUpEvent event) { + if(event.getSource().isCry() && "bed".equals(event.getLocation())) { + hug(); + } + } +} diff --git a/src/main/java/com/msb/observer/v7/Observer.java b/src/main/java/com/msb/observer/v7/Observer.java new file mode 100644 index 0000000..f85924f --- /dev/null +++ b/src/main/java/com/msb/observer/v7/Observer.java @@ -0,0 +1,11 @@ +package com.msb.observer.v7; + +/** + * @Author bingor + * @Date 2022/10/13 19:33 + * @Description: com.msb.observer.v5 + * @Version: 1.0 + */ +public interface Observer { + public void actionOnWakeUp(WakeUpEvent event); +} diff --git a/src/main/java/com/msb/observer/v7/Observer07.java b/src/main/java/com/msb/observer/v7/Observer07.java new file mode 100644 index 0000000..cd6ed10 --- /dev/null +++ b/src/main/java/com/msb/observer/v7/Observer07.java @@ -0,0 +1,20 @@ +package com.msb.observer.v7;/** + * @Author bingor + * @Date 2022/10/13 19:28 + * @Description: com.msb.observer.v4 + * @Version: 1.0 + */ + +/** + *@ClassName Observer04 + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:28 + *@Version 3.0 + */ +public class Observer07 { + public static void main(String[] args) { + Child child = new Child(); + child.wakeUp(); + } +} diff --git a/src/main/java/com/msb/observer/v7/WakeUpEvent.java b/src/main/java/com/msb/observer/v7/WakeUpEvent.java new file mode 100644 index 0000000..0aa467b --- /dev/null +++ b/src/main/java/com/msb/observer/v7/WakeUpEvent.java @@ -0,0 +1,39 @@ +package com.msb.observer.v7;/** + * @Author bingor + * @Date 2022/10/13 19:56 + * @Description: com.msb.observer.v6 + * @Version: 1.0 + */ + +import java.time.LocalDateTime; + +/** + *@ClassName WakeUpEvent + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:56 + *@Version 3.0 + */ +public class WakeUpEvent { + private LocalDateTime localDateTime; + private String location; + private Child source; + + public WakeUpEvent(LocalDateTime localDateTime, String location, Child source) { + this.localDateTime = localDateTime; + this.location = location; + this.source = source; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public String getLocation() { + return location; + } + + public Child getSource() { + return source; + } +} diff --git a/src/main/java/com/msb/observer/v8/Child.java b/src/main/java/com/msb/observer/v8/Child.java new file mode 100644 index 0000000..188d9c4 --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Child.java @@ -0,0 +1,40 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v2 + * @Version: 1.0 + */ + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +/** + *@ClassName Child + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Child { + + private boolean cry = false; + private List observers = new ArrayList<>(); + + { + observers.add(new Dad()); + observers.add(new Mum()); + } + + public boolean isCry() { + return cry; + } + + public void wakeUp() { + this.cry = true; + WakeUpEvent event = new WakeUpEvent(LocalDateTime.now(), "bed", this); + for (Observer observer : observers) { + observer.actionOnWakeUp(event); + } + } +} diff --git a/src/main/java/com/msb/observer/v8/Dad.java b/src/main/java/com/msb/observer/v8/Dad.java new file mode 100644 index 0000000..495cec2 --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Dad.java @@ -0,0 +1,25 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v3 + * @Version: 1.0 + */ + +/** + *@ClassName Dad + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Dad implements Observer { + public void feed() { + System.out.println("孩子醒了,爸爸给孩子喂奶……"); + } + @Override + public void actionOnWakeUp(WakeUpEvent event) { + if(event.getSource().isCry() && "hungry".equals(event.getLocation())) { + feed(); + } + } +} diff --git a/src/main/java/com/msb/observer/v8/Event.java b/src/main/java/com/msb/observer/v8/Event.java new file mode 100644 index 0000000..639c333 --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Event.java @@ -0,0 +1,17 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/14 11:36 + * @Description: com.msb.observer.v8 + * @Version: 1.0 + */ + +/** + *@ClassName Event + *@Description TODO + *@Author bingor + *@Date 2022/10/14 11:36 + *@Version 3.0 + */ +public abstract class Event { + public abstract T getSource(); +} diff --git a/src/main/java/com/msb/observer/v8/Mum.java b/src/main/java/com/msb/observer/v8/Mum.java new file mode 100644 index 0000000..7651b9a --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Mum.java @@ -0,0 +1,25 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/13 19:18 + * @Description: com.msb.observer.v3 + * @Version: 1.0 + */ + +/** + *@ClassName Dad + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:18 + *@Version 3.0 + */ +public class Mum implements Observer { + public void hug() { + System.out.println("孩子醒了,妈妈抱抱孩子……"); + } + @Override + public void actionOnWakeUp(WakeUpEvent event) { + if(event.getSource().isCry() && "bed".equals(event.getLocation())) { + hug(); + } + } +} diff --git a/src/main/java/com/msb/observer/v8/Observer.java b/src/main/java/com/msb/observer/v8/Observer.java new file mode 100644 index 0000000..f39361d --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Observer.java @@ -0,0 +1,11 @@ +package com.msb.observer.v8; + +/** + * @Author bingor + * @Date 2022/10/13 19:33 + * @Description: com.msb.observer.v5 + * @Version: 1.0 + */ +public interface Observer { + public void actionOnWakeUp(WakeUpEvent event); +} diff --git a/src/main/java/com/msb/observer/v8/Observer08.java b/src/main/java/com/msb/observer/v8/Observer08.java new file mode 100644 index 0000000..8c84c5e --- /dev/null +++ b/src/main/java/com/msb/observer/v8/Observer08.java @@ -0,0 +1,20 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/13 19:28 + * @Description: com.msb.observer.v4 + * @Version: 1.0 + */ + +/** + *@ClassName Observer04 + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:28 + *@Version 3.0 + */ +public class Observer08 { + public static void main(String[] args) { + Child child = new Child(); + child.wakeUp(); + } +} diff --git a/src/main/java/com/msb/observer/v8/WakeUpEvent.java b/src/main/java/com/msb/observer/v8/WakeUpEvent.java new file mode 100644 index 0000000..475e408 --- /dev/null +++ b/src/main/java/com/msb/observer/v8/WakeUpEvent.java @@ -0,0 +1,40 @@ +package com.msb.observer.v8;/** + * @Author bingor + * @Date 2022/10/13 19:56 + * @Description: com.msb.observer.v6 + * @Version: 1.0 + */ + +import java.time.LocalDateTime; + +/** + *@ClassName WakeUpEvent + *@Description TODO + *@Author bingor + *@Date 2022/10/13 19:56 + *@Version 3.0 + */ +public class WakeUpEvent extends Event { + private LocalDateTime localDateTime; + private String location; + private Child source; + + public WakeUpEvent(LocalDateTime localDateTime, String location, Child source) { + this.localDateTime = localDateTime; + this.location = location; + this.source = source; + } + + public LocalDateTime getLocalDateTime() { + return localDateTime; + } + + public String getLocation() { + return location; + } + + @Override + public Child getSource() { + return source; + } +}