坦克大战(一期)-设计模式-观察者模式联系

DesignPatterns
bingor 2 years ago
parent 98ae4fe17a
commit a31873c566

@ -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<Observer> 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);
}
}
}

@ -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();
}
}
}

@ -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();
}
}
}

@ -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);
}

@ -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();
}
}

@ -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;
}
}

@ -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<Observer> 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);
}
}
}

@ -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();
}
}
}

@ -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<T> {
public abstract T getSource();
}

@ -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();
}
}
}

@ -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);
}

@ -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();
}
}

@ -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<Child> {
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;
}
}
Loading…
Cancel
Save