diff --git a/Cleaner.java b/Cleaner.java new file mode 100644 index 0000000..08eb2ee --- /dev/null +++ b/Cleaner.java @@ -0,0 +1,9 @@ +package ServantDesignPattern; + +public class Cleaner{ + //仆人动物类 + //提供的具体服务内容:打扫操场的跑道和草坪 + public void clean(IsCleanable cleanable){ + cleanable.cleaned(); + } +} diff --git a/Client.java b/Client.java new file mode 100644 index 0000000..a8be879 --- /dev/null +++ b/Client.java @@ -0,0 +1,11 @@ +package ServantDesignPattern; + +//Client客户端场景 + public class Client{ + public static void main(String[] args){ + Cleaner cleaner = new Cleaner(); + cleaner.clean(new Runway()); + cleaner.clean(new Lawn()); + } + } + diff --git a/Colleague.java b/Colleague.java new file mode 100644 index 0000000..ac71028 --- /dev/null +++ b/Colleague.java @@ -0,0 +1,10 @@ +package Mediator; + + public abstract class Colleague {//AthleteAndOthers + + protected Mediator mediator; + + public Colleague(Mediator mediator) { + this.mediator = mediator; + } +} diff --git a/ConcreteAthlete.java b/ConcreteAthlete.java new file mode 100644 index 0000000..7bc5571 --- /dev/null +++ b/ConcreteAthlete.java @@ -0,0 +1,18 @@ +package Mediator; + +public class ConcreteAthlete extends Colleague { + + public ConcreteAthlete(Mediator mediator) { + super(mediator); + } + + public void send(String message) { + mediator.send(message, this); + } + + public void notify(String message) { + System.out.println("ConcreteAthlete:send:运动员得到消息:" + message); + } + +} + diff --git a/ConcreteMediator.java b/ConcreteMediator.java new file mode 100644 index 0000000..8e4b40a --- /dev/null +++ b/ConcreteMediator.java @@ -0,0 +1,25 @@ +package Mediator; + +public class ConcreteMediator extends Mediator { + + private ConcreteOthers Others;//具体同事1,其他普通动物 + private ConcreteAthlete Athlete;//具体同事2,运动员动物 + + public void setOthers(ConcreteOthers Others) { + this.Others = Others; + } + + public void setAthlete(ConcreteAthlete Athlete) { + this.Athlete = Athlete; + } + + @Override + //普通动物和运动员发送信息功能 + public void send(String message, Colleague colleague) { + if(colleague == Others) { + Athlete.notify(message); + } else { + Others.notify(message); + } + } +} diff --git a/ConcreteOthers.java b/ConcreteOthers.java new file mode 100644 index 0000000..27ea288 --- /dev/null +++ b/ConcreteOthers.java @@ -0,0 +1,17 @@ +package Mediator; + +public class ConcreteOthers extends Colleague { + + public ConcreteOthers(Mediator mediator) { + super(mediator); + } + + public void send(String message) { + mediator.send(message, this); + } + + public void notify(String message) { + System.out.println("ConcreteOthers:send:普通动物得到消息:" + message); + } + +} \ No newline at end of file diff --git a/Facade.java b/Facade.java new file mode 100644 index 0000000..c77f3bd --- /dev/null +++ b/Facade.java @@ -0,0 +1,82 @@ +package Facade; + +//外观类 + public class Facade { + + //被委托的对象,报名,排队,体检,检录 + baoming a; + paidui b; + tijian c; + jianlu d; +//外观设计模式创建新的报名,排队等过程 + public Facade() { + a = new baoming(); + b = new paidui(); + c = new tijian(); + d = new jianlu(); + } + + //提供给外部访问的方法,做这四个工作 + public void method_baoming() { this.a.dosomething_baoming();} + + public void method_paidui() { + this.b.dosomething_paidui(); + } + + public void method_tijian() { + this.c.dosomething_tijian(); + } + + public void method_jianlu() { + this.d.dosomething_jianlu(); + } + + +//外观设计的四个method +public void method(){ + method_baoming(); + method_paidui(); + method_tijian(); + method_jianlu(); +} + } +//facade中四个子系统角色,及其输出 +class baoming {//报名类 + + public void dosomething_baoming() { + System.out.println("baoming:method_baoming:运动员报名"); + } + +} + class paidui {//排队类 + + public void dosomething_paidui() { + System.out.println("paidui:method_paidui:运动员参与排队"); + } + +} + class tijian {//体检类 + + public void dosomething_tijian() { + System.out.println("tijian:method_tijian:运动员体检"); + } + +} +class jianlu {//检录类 + + public void dosomething_jianlu() { + System.out.println("jianlu:method_jianlu:运动员入场检录"); + } + +} +//Client客户端与主函数 + class Client { + + public static void main(String[] args) { + Facade facade = new Facade(); + + facade.method(); + + } + +} \ No newline at end of file diff --git a/FacadeTest.java b/FacadeTest.java new file mode 100644 index 0000000..e974b5c --- /dev/null +++ b/FacadeTest.java @@ -0,0 +1,12 @@ +package Test; + +import Facade.Facade; + +public class FacadeTest { + public static void main(String[] args) { + Facade facade = new Facade(); + + facade.method(); + + } +} diff --git a/IsCleanable.java b/IsCleanable.java new file mode 100644 index 0000000..0c1ab37 --- /dev/null +++ b/IsCleanable.java @@ -0,0 +1,7 @@ +package ServantDesignPattern; + +public interface IsCleanable{ + //被服务者 + // 定义“一组类”所具有的通用功能:打扫操场 + void cleaned(); +} diff --git a/Lawn.java b/Lawn.java new file mode 100644 index 0000000..8699de5 --- /dev/null +++ b/Lawn.java @@ -0,0 +1,11 @@ +package ServantDesignPattern; + +public class Lawn implements IsCleanable { + //实现 IServiced,并完成具体逻辑 + //具体被服务的对象 + //仆人动物打扫草坪 + public void cleaned(){ + System.out.println("Lawn:clean:草坪被清理干净"); + } +} + diff --git a/Mediator.java b/Mediator.java new file mode 100644 index 0000000..1304a78 --- /dev/null +++ b/Mediator.java @@ -0,0 +1,45 @@ +package Mediator; +//抽象中介者类 + + public abstract class Mediator { + +//抽象的发送消息方法 + public abstract void send(String message, Colleague colleague); + + } + + //抽象同事类,也就是运动员和普通动物的抽象类 + + + //具体中介者类 + + + + //具体同事类,普通动物 + + + //具体同事类,运动员 + + //Client客户端 + class Client { + + public static void main(String[] args) { + ConcreteMediator mediator = new ConcreteMediator(); + + ConcreteOthers Others = new ConcreteOthers(mediator); + ConcreteAthlete Athlete = new ConcreteAthlete(mediator); + + mediator.setOthers(Others); + mediator.setAthlete(Athlete); + + Others.send("普通动物要求运动员参会"); + Athlete.send("运动员已经参会"); + } + + } + + + + + + diff --git a/MediatorTest.java b/MediatorTest.java new file mode 100644 index 0000000..34a3ad6 --- /dev/null +++ b/MediatorTest.java @@ -0,0 +1,23 @@ +package Test; +import org.junit.Test; + +import Mediator.ConcreteAthlete; +import Mediator.ConcreteMediator; +import Mediator.ConcreteOthers; + +public class MediatorTest { + @Test + public void test() { + ConcreteMediator mediator = new ConcreteMediator(); + + ConcreteOthers Others = new ConcreteOthers(mediator); + ConcreteAthlete Athlete = new ConcreteAthlete(mediator); + + mediator.setOthers(Others); + mediator.setAthlete(Athlete); + + Others.send("普通动物要求运动员参会"); + Athlete.send("运动员已经参会"); + } + +} diff --git a/Runway.java b/Runway.java new file mode 100644 index 0000000..56a5290 --- /dev/null +++ b/Runway.java @@ -0,0 +1,11 @@ +package ServantDesignPattern; + + +public class Runway implements IsCleanable { + //实现 IServiced,并完成具体逻辑 + //具体被服务的对象 + //仆人动物打扫跑道 + public void cleaned(){ + System.out.println("Runway:clean:跑道被清理干净"); + } +} diff --git a/ServantTest.java b/ServantTest.java new file mode 100644 index 0000000..2aa5053 --- /dev/null +++ b/ServantTest.java @@ -0,0 +1,14 @@ +package Test; + +import ServantDesignPattern.Cleaner; +import ServantDesignPattern.Lawn; +import ServantDesignPattern.Runway; + +public class ServantTest { + public static void main(String[] args){ + Cleaner cleaner = new Cleaner(); + cleaner.clean(new Runway()); + cleaner.clean(new Lawn()); + } + +}