Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facade&mediator #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ServantDesignPattern;

public class Cleaner{
//仆人动物类
//提供的具体服务内容:打扫操场的跑道和草坪
public void clean(IsCleanable cleanable){
cleanable.cleaned();
}
}
11 changes: 11 additions & 0 deletions Client.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

10 changes: 10 additions & 0 deletions Colleague.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Mediator;

public abstract class Colleague {//AthleteAndOthers

protected Mediator mediator;

public Colleague(Mediator mediator) {
this.mediator = mediator;
}
}
18 changes: 18 additions & 0 deletions ConcreteAthlete.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

25 changes: 25 additions & 0 deletions ConcreteMediator.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
17 changes: 17 additions & 0 deletions ConcreteOthers.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
82 changes: 82 additions & 0 deletions Facade.java
Original file line number Diff line number Diff line change
@@ -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();

}

}
12 changes: 12 additions & 0 deletions FacadeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Test;

import Facade.Facade;

public class FacadeTest {
public static void main(String[] args) {
Facade facade = new Facade();

facade.method();

}
}
7 changes: 7 additions & 0 deletions IsCleanable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ServantDesignPattern;

public interface IsCleanable{
//被服务者
// 定义“一组类”所具有的通用功能:打扫操场
void cleaned();
}
11 changes: 11 additions & 0 deletions Lawn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ServantDesignPattern;

public class Lawn implements IsCleanable {
//实现 IServiced,并完成具体逻辑
//具体被服务的对象
//仆人动物打扫草坪
public void cleaned(){
System.out.println("Lawn:clean:草坪被清理干净");
}
}

45 changes: 45 additions & 0 deletions Mediator.java
Original file line number Diff line number Diff line change
@@ -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("运动员已经参会");
}

}






23 changes: 23 additions & 0 deletions MediatorTest.java
Original file line number Diff line number Diff line change
@@ -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("运动员已经参会");
}

}
11 changes: 11 additions & 0 deletions Runway.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ServantDesignPattern;


public class Runway implements IsCleanable {
//实现 IServiced,并完成具体逻辑
//具体被服务的对象
//仆人动物打扫跑道
public void cleaned(){
System.out.println("Runway:clean:跑道被清理干净");
}
}
14 changes: 14 additions & 0 deletions ServantTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}