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

DesignPatterns
bingor 2 years ago
parent 32b0fa6c52
commit 98ae4fe17a

@ -0,0 +1,22 @@
package com.msb.observer.v1;/**
* @Author bingor
* @Date 2022/10/13 19:05
* @Description: com.msb.observer
* @Version: 1.0
*/
/**
*@ClassName Observer01
*@Description
*@Author bingor
*@Date 2022/10/13 19:05
*@Version 3.0
*/
public class Observer01 {
public static void main(String[] args) {
boolean cry = false;
while ( ! cry) {
System.out.println("继续观察……");
}
}
}

@ -0,0 +1,26 @@
package com.msb.observer.v2;/**
* @Author bingor
* @Date 2022/10/13 19:18
* @Description: com.msb.observer.v2
* @Version: 1.0
*/
/**
*@ClassName Child
*@Description TODO
*@Author bingor
*@Date 2022/10/13 19:18
*@Version 3.0
*/
public class Child {
private boolean cry = false;
public boolean isCry() {
return cry;
}
public void wakeUp() {
this.cry = true;
System.out.println("孩子醒了……");
}
}

@ -0,0 +1,29 @@
package com.msb.observer.v2;/**
* @Author bingor
* @Date 2022/10/13 19:09
* @Description: com.msb.observer
* @Version: 1.0
*/
/**
*@ClassName Observer02
*@Description
*@Author bingor
*@Date 2022/10/13 19:09
*@Version 3.0
*/
public class Observer02 {
public static void main(String[] args) {
Child child = new Child();
while ( ! child.isCry()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("孩子还没有醒,继续观察……");
}
}
}

@ -0,0 +1,28 @@
package com.msb.observer.v3;/**
* @Author bingor
* @Date 2022/10/13 19:18
* @Description: com.msb.observer.v2
* @Version: 1.0
*/
/**
*@ClassName Child
*@Description TODO
*@Author bingor
*@Date 2022/10/13 19:18
*@Version 3.0
*/
public class Child {
private boolean cry = false;
private Dad dad = new Dad();
public boolean isCry() {
return cry;
}
public void wakeUp() {
this.cry = true;
dad.feed();
}
}

@ -0,0 +1,19 @@
package com.msb.observer.v3;/**
* @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 {
public void feed() {
System.out.println("孩子醒了,爸爸给孩子喂奶……");
}
}

@ -0,0 +1,21 @@
package com.msb.observer.v3;/**
* @Author bingor
* @Date 2022/10/13 19:15
* @Description: com.msb.observer
* @Version: 1.0
*/
/**
*@ClassName Observer03
*@Description
*@Author bingor
*@Date 2022/10/13 19:15
*@Version 3.0
*/
public class Observer03 {
public static void main(String[] args) {
Child child = new Child();
child.wakeUp();
}
}

@ -0,0 +1,34 @@
package com.msb.observer.v4;/**
* @Author bingor
* @Date 2022/10/13 19:18
* @Description: com.msb.observer.v2
* @Version: 1.0
*/
import com.msb.observer.v3.Dad;
/**
*@ClassName Child
*@Description TODO
*@Author bingor
*@Date 2022/10/13 19:18
*@Version 3.0
*/
public class Child {
private boolean cry = false;
private com.msb.observer.v3.Dad dad = new Dad();
private Mum mum = new Mum();
private Dog dog = new Dog();
public boolean isCry() {
return cry;
}
public void wakeUp() {
this.cry = true;
dad.feed();
mum.hug();
dog.wang();
}
}

@ -0,0 +1,19 @@
package com.msb.observer.v4;/**
* @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 {
public void feed() {
System.out.println("孩子醒了,爸爸给孩子喂奶……");
}
}

@ -0,0 +1,19 @@
package com.msb.observer.v4;/**
* @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 Dog {
public void wang() {
System.out.println("孩子醒了,狗叫……");
}
}

@ -0,0 +1,19 @@
package com.msb.observer.v4;/**
* @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 {
public void hug() {
System.out.println("孩子醒了,妈妈宝宝孩子……");
}
}

@ -0,0 +1,20 @@
package com.msb.observer.v4;/**
* @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 Observer04 {
public static void main(String[] args) {
Child child = new Child();
child.wakeUp();
}
}

@ -0,0 +1,41 @@
package com.msb.observer.v5;/**
* @Author bingor
* @Date 2022/10/13 19:18
* @Description: com.msb.observer.v2
* @Version: 1.0
*/
import com.msb.chain.ColliderChain;
import com.msb.observer.v4.Dog;
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;
for (Observer observer : observers) {
observer.actionOnWakeUp();
}
}
}

@ -0,0 +1,23 @@
package com.msb.observer.v5;/**
* @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() {
feed();
}
}

@ -0,0 +1,23 @@
package com.msb.observer.v5;/**
* @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() {
hug();
}
}

@ -0,0 +1,11 @@
package com.msb.observer.v5;
/**
* @Author bingor
* @Date 2022/10/13 19:33
* @Description: com.msb.observer.v5
* @Version: 1.0
*/
public interface Observer {
public void actionOnWakeUp();
}

@ -0,0 +1,22 @@
package com.msb.observer.v5;/**
* @Author bingor
* @Date 2022/10/13 19:28
* @Description: com.msb.observer.v4
* @Version: 1.0
*/
import com.msb.observer.v5.Child;
/**
*@ClassName Observer04
*@Description TODO
*@Author bingor
*@Date 2022/10/13 19:28
*@Version 3.0
*/
public class Observer05 {
public static void main(String[] args) {
Child child = new Child();
child.wakeUp();
}
}

@ -0,0 +1,40 @@
package com.msb.observer.v6;/**
* @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");
for (Observer observer : observers) {
observer.actionOnWakeUp(event);
}
}
}

@ -0,0 +1,25 @@
package com.msb.observer.v6;/**
* @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("hungry".equals(event.getLocation())) {
feed();
}
}
}

@ -0,0 +1,25 @@
package com.msb.observer.v6;/**
* @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("bed".equals(event.getLocation())) {
hug();
}
}
}

@ -0,0 +1,11 @@
package com.msb.observer.v6;
/**
* @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.v6;/**
* @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 Observer06 {
public static void main(String[] args) {
Child child = new Child();
child.wakeUp();
}
}

@ -0,0 +1,33 @@
package com.msb.observer.v6;/**
* @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;
public WakeUpEvent(LocalDateTime localDateTime, String location) {
this.localDateTime = localDateTime;
this.location = location;
}
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
public String getLocation() {
return location;
}
}
Loading…
Cancel
Save