Skip to content

Commit

Permalink
完成 中介者模式 Sample
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesZBL committed Nov 27, 2017
1 parent f55a617 commit 6cf8f8c
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 0 deletions.
59 changes: 59 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/AbstractPartyMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 派对成员抽象类
*/
public abstract class AbstractPartyMember implements PartyMember {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPartyMember.class);

private Party party;

@Override
public void joinParty(Party party) {
LOGGER.info("{}加入了派对", this);
this.party = party;
}

@Override
public void act(Activity activity) {
if (null != activity) {
LOGGER.info("{}提议进行{}活动", this, activity);
party.letAct(this, activity);
}
}

@Override
public void partyActivity(Activity activity) {
LOGGER.info("进行派对活动,名称:{},介绍:{}", activity, activity.getDescription());
}

@Override
public abstract String toString();
}
48 changes: 48 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 活动
*/
public enum Activity {
SHOOT("射击", "Shooting"), GUESS("猜灯谜", "Guess"), DESKTOP_GAME("桌游", "Desktop games"), SING("唱歌", "singing");

private String name;
private String description;

Activity(String name, String description) {
this.name = name;
this.description = description;
}

public String getDescription() {
return description;
}

@Override
public String toString() {
return name;
}
}
53 changes: 53 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Mediator
*/
public class Application {

private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
Party party = new PartyImpl();
Student student = new Student();
Officer officer = new Officer();
Businessman businessman = new Businessman();
Oldman oldman = new Oldman();

party.addMember(student);
party.addMember(officer);
party.addMember(businessman);
party.addMember(oldman);

student.act(Activity.DESKTOP_GAME);
officer.act(Activity.GUESS);
businessman.act(Activity.SHOOT);
oldman.act(Activity.SING);
}
}
35 changes: 35 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Businessman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 商人
*/
public class Businessman extends AbstractPartyMember {

@Override
public String toString() {
return "商人";
}
}
35 changes: 35 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Officer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 官员
*/
public class Officer extends AbstractPartyMember {

@Override
public String toString() {
return "官员";
}
}
35 changes: 35 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Oldman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 老人
*/
public class Oldman extends AbstractPartyMember {

@Override
public String toString() {
return "老人";
}
}
34 changes: 34 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/Party.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 派对
*/
public interface Party {

void addMember(PartyMember member);

void letAct(PartyMember member, Activity activity);
}
54 changes: 54 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/PartyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

import java.util.ArrayList;
import java.util.List;

/**
* 派对的实现类
*/
public class PartyImpl implements Party {

private List<PartyMember> members;

public PartyImpl() {
members = new ArrayList<>();
}

@Override
public void addMember(PartyMember member) {
members.add(member);
member.joinParty(this);
}

@Override
public void letAct(PartyMember member, Activity activity) {
for (PartyMember m : members) {
if (!member.equals(m)) {
m.partyActivity(activity);
}
}
}
}
36 changes: 36 additions & 0 deletions mediator/src/main/java/me/zbl/mediator/PartyMember.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* MIT License
* <p>
* Copyright (c) 2017 James
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.zbl.mediator;

/**
* 派对成员
*/
public interface PartyMember {

void joinParty(Party party);

void partyActivity(Activity activity);

void act(Activity activity);
}
Loading

0 comments on commit 6cf8f8c

Please sign in to comment.