diff --git a/iterator/pom.xml b/iterator/pom.xml new file mode 100644 index 0000000..89fcfde --- /dev/null +++ b/iterator/pom.xml @@ -0,0 +1,51 @@ + + + + + java_design_patterns + com.github.JamesZBL + 1.11.9-SNAPSHOT + + 4.0.0 + + iterator + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + \ No newline at end of file diff --git a/iterator/src/main/java/Application.java b/iterator/src/main/java/Application.java new file mode 100644 index 0000000..fb2a681 --- /dev/null +++ b/iterator/src/main/java/Application.java @@ -0,0 +1,60 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Iterator + */ +public class Application { + + private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + // 查找小说 + ItemIterator iterator = new BookShelfIterator(ItemType.FICTION, new BookShelf()); + LOGGER.info("正在查找小说类图书"); + while (iterator.hasNext()) { + Item nextItem = iterator.next(); + LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); + } + + // 查找IT + ItemIterator iterator2 = new BookShelfIterator(ItemType.IT, new BookShelf()); + LOGGER.info("正在查找IT类图书"); + while (iterator2.hasNext()) { + Item nextItem = iterator2.next(); + LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); + } + + // 查找漫画 + ItemIterator iterator3 = new BookShelfIterator(ItemType.CARTOON, new BookShelf()); + LOGGER.info("正在查找漫画类图书"); + while (iterator3.hasNext()) { + Item nextItem = iterator3.next(); + LOGGER.info("找到了符合条件的图书,书名为:{}", nextItem.toString()); + } + } +} diff --git a/iterator/src/main/java/BookShelf.java b/iterator/src/main/java/BookShelf.java new file mode 100644 index 0000000..239ef48 --- /dev/null +++ b/iterator/src/main/java/BookShelf.java @@ -0,0 +1,55 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +import java.util.ArrayList; +import java.util.List; + +/** + * 书架 + */ +public class BookShelf { + + private List items; + + public BookShelf() { + items = new ArrayList<>(); + items.add(new Item(ItemType.FICTION, "西游记")); + items.add(new Item(ItemType.FICTION, "水浒传")); + items.add(new Item(ItemType.FICTION, "三国演义")); + items.add(new Item(ItemType.FICTION, "红楼梦")); + items.add(new Item(ItemType.CARTOON, "阿衰")); + items.add(new Item(ItemType.CARTOON, "七龙珠")); + items.add(new Item(ItemType.CARTOON, "火影忍者")); + items.add(new Item(ItemType.IT, "设计模式-可复用面向对象软件的基础")); + items.add(new Item(ItemType.IT, "重构-改善既有代码的设计")); + items.add(new Item(ItemType.IT, "Effective Java")); + items.add(new Item(ItemType.IT, "Java编程思想")); + } + + public List getItemList() { + List list = new ArrayList<>(); + list.addAll(items); + return list; + } +} diff --git a/iterator/src/main/java/BookShelfIterator.java b/iterator/src/main/java/BookShelfIterator.java new file mode 100644 index 0000000..418ec16 --- /dev/null +++ b/iterator/src/main/java/BookShelfIterator.java @@ -0,0 +1,72 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +import java.util.List; + +/** + * 图书的迭代器 + */ +public class BookShelfIterator implements ItemIterator { + + private ItemType type; + private BookShelf shelf; + private int idx; + + public BookShelfIterator(ItemType type, BookShelf shelf) { + this.type = type; + this.shelf = shelf; + this.idx = -1; + } + + @Override + public boolean hasNext() { + return -1 != getNexIdx(); + } + + @Override + public Item next() { + idx = getNexIdx(); + if (-1 != idx) { + return shelf.getItemList().get(idx); + } + return null; + } + + private int getNexIdx() { + List list = shelf.getItemList(); + int tempIdx = idx; + boolean found = false; + while (!found) { + tempIdx++; + if (tempIdx >= list.size()) { + tempIdx = -1; + break; + } + if (list.get(tempIdx).getType().equals(type)) { + break; + } + } + return tempIdx; + } +} diff --git a/iterator/src/main/java/Item.java b/iterator/src/main/java/Item.java new file mode 100644 index 0000000..7c5fa39 --- /dev/null +++ b/iterator/src/main/java/Item.java @@ -0,0 +1,50 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +/** + * 元素 + */ +public class Item { + + private ItemType type; + private String name; + + public Item(ItemType type, String name) { + this.type = type; + this.name = name; + } + + public final void setType(ItemType type) { + this.type = type; + } + + public ItemType getType() { + return type; + } + + @Override + public String toString() { + return name; + } +} diff --git a/iterator/src/main/java/ItemIterator.java b/iterator/src/main/java/ItemIterator.java new file mode 100644 index 0000000..c308452 --- /dev/null +++ b/iterator/src/main/java/ItemIterator.java @@ -0,0 +1,33 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +/** + * 迭代器接口 + */ +public interface ItemIterator { + + boolean hasNext(); + + Item next(); +} diff --git a/iterator/src/main/java/ItemType.java b/iterator/src/main/java/ItemType.java new file mode 100644 index 0000000..2b7a9c3 --- /dev/null +++ b/iterator/src/main/java/ItemType.java @@ -0,0 +1,31 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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. + */ + +/** + * 元素类型 + */ +public enum ItemType { + + IT, FICTION, CARTOON +} diff --git a/mediator/pom.xml b/mediator/pom.xml new file mode 100644 index 0000000..d94879f --- /dev/null +++ b/mediator/pom.xml @@ -0,0 +1,51 @@ + + + + + java_design_patterns + com.github.JamesZBL + 1.11.9-SNAPSHOT + + 4.0.0 + + mediator + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + \ No newline at end of file diff --git a/mediator/src/main/java/me/zbl/mediator/AbstractPartyMember.java b/mediator/src/main/java/me/zbl/mediator/AbstractPartyMember.java new file mode 100644 index 0000000..c7080a3 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/AbstractPartyMember.java @@ -0,0 +1,59 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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(); +} diff --git a/mediator/src/main/java/me/zbl/mediator/Activity.java b/mediator/src/main/java/me/zbl/mediator/Activity.java new file mode 100644 index 0000000..9a1d8b7 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Activity.java @@ -0,0 +1,48 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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; + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/Application.java b/mediator/src/main/java/me/zbl/mediator/Application.java new file mode 100644 index 0000000..9448607 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Application.java @@ -0,0 +1,53 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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); + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/Businessman.java b/mediator/src/main/java/me/zbl/mediator/Businessman.java new file mode 100644 index 0000000..9ac7bdf --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Businessman.java @@ -0,0 +1,35 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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 "商人"; + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/Officer.java b/mediator/src/main/java/me/zbl/mediator/Officer.java new file mode 100644 index 0000000..9d8b913 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Officer.java @@ -0,0 +1,35 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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 "官员"; + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/Oldman.java b/mediator/src/main/java/me/zbl/mediator/Oldman.java new file mode 100644 index 0000000..13f1c3c --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Oldman.java @@ -0,0 +1,35 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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 "老人"; + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/Party.java b/mediator/src/main/java/me/zbl/mediator/Party.java new file mode 100644 index 0000000..4d45c95 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Party.java @@ -0,0 +1,34 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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); +} diff --git a/mediator/src/main/java/me/zbl/mediator/PartyImpl.java b/mediator/src/main/java/me/zbl/mediator/PartyImpl.java new file mode 100644 index 0000000..dd0d976 --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/PartyImpl.java @@ -0,0 +1,54 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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 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); + } + } + } +} diff --git a/mediator/src/main/java/me/zbl/mediator/PartyMember.java b/mediator/src/main/java/me/zbl/mediator/PartyMember.java new file mode 100644 index 0000000..ace610d --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/PartyMember.java @@ -0,0 +1,36 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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); +} diff --git a/mediator/src/main/java/me/zbl/mediator/Student.java b/mediator/src/main/java/me/zbl/mediator/Student.java new file mode 100644 index 0000000..a2160ac --- /dev/null +++ b/mediator/src/main/java/me/zbl/mediator/Student.java @@ -0,0 +1,35 @@ +/** + * MIT License + *

+ * Copyright (c) 2017 James + *

+ * 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: + *

+ * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + *

+ * 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 Student extends AbstractPartyMember { + + @Override + public String toString() { + return "学生"; + } +} diff --git a/pom.xml b/pom.xml index 75f561a..3bfc3cd 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,8 @@ chain command interpreter + iterator + mediator