├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── abstract-factory ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── abstractFactory │ │ ├── Application.java │ │ ├── Captain.java │ │ ├── Member.java │ │ ├── NewShip.java │ │ ├── OldCaptain.java │ │ ├── OldSailor.java │ │ ├── OldShip.java │ │ ├── PermanentTeamFactory.java │ │ ├── Sailor.java │ │ ├── Ship.java │ │ ├── TeamFactory.java │ │ ├── YoungCaptain.java │ │ ├── YoungSailor.java │ │ └── YoungTeamFactory.java │ └── test │ └── java │ └── me │ └── zbl │ └── abstractFactory │ └── AbstractFactoryTest.java ├── adapter ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── adapter │ │ ├── Application.java │ │ ├── Bus.java │ │ ├── BusAdapter.java │ │ ├── Car.java │ │ └── Driver.java │ └── test │ └── java │ └── me │ └── zbl │ └── adapter │ └── TestAdapter.java ├── bridge ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── bridge │ │ ├── AncientWar.java │ │ ├── Application.java │ │ ├── Enemy.java │ │ ├── IntrepidEnemy.java │ │ ├── MordernWar.java │ │ ├── TenderEnemy.java │ │ └── War.java │ └── test │ └── java │ └── me │ └── zbl │ └── bridge │ ├── AncientWarTest.java │ ├── MordernWarTest.java │ └── WarTest.java ├── builder ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── builder │ │ ├── Application.java │ │ ├── Nationality.java │ │ ├── Person.java │ │ └── SkinColor.java │ └── test │ └── java │ └── me │ └── zbl │ └── builder │ └── TestPerson.java ├── chain ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── chain │ ├── Application.java │ ├── Captain.java │ ├── Commander.java │ ├── Gunner.java │ ├── Gunny.java │ ├── Request.java │ └── RequestHandler.java ├── command ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── command │ ├── AbstractFont.java │ ├── Application.java │ ├── Color.java │ ├── Command.java │ ├── Enlarge.java │ ├── RegularScript.java │ ├── Rubify.java │ ├── Size.java │ └── Typist.java ├── composite ├── README.md ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── me │ │ │ └── zbl │ │ │ └── composite │ │ │ ├── Application.java │ │ │ ├── Character.java │ │ │ ├── CharacterComposite.java │ │ │ ├── ChineseSentence.java │ │ │ ├── ChineseWord.java │ │ │ ├── EnglishSentence.java │ │ │ ├── EnglishWord.java │ │ │ └── Writer.java │ └── test │ │ └── java │ │ └── me │ │ └── zbl │ │ └── composite │ │ └── WriterTest.java └── uml │ ├── Character.png │ └── Composite.png ├── decorator ├── README.md ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── me │ │ │ └── zbl │ │ │ └── decorator │ │ │ ├── Application.java │ │ │ ├── CarpenterOperation.java │ │ │ ├── HammerSmithOperation.java │ │ │ └── Operation.java │ └── test │ │ └── java │ │ └── me │ │ └── zbl │ │ └── decorator │ │ ├── CarpenterOperationsTest.java │ │ └── HammerSmithOperationsTest.java └── uml │ ├── Decorator.png │ └── Decorator_impl.png ├── facade ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── facade │ │ ├── Application.java │ │ ├── CourseFacade.java │ │ ├── CourseParticipator.java │ │ ├── CourseStudent.java │ │ └── CourseTeacher.java │ └── test │ └── java │ └── me │ └── zbl │ └── facade │ └── CourseFacadeTest.java ├── factory-method ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── factory │ │ └── method │ │ ├── Application.java │ │ ├── ChineseCook.java │ │ ├── ChineseFood.java │ │ ├── Cook.java │ │ ├── Food.java │ │ ├── FoodType.java │ │ ├── WesternCook.java │ │ └── WesternFood.java │ └── test │ └── java │ └── me │ └── zbl │ └── factory │ └── method │ └── FactoryMethodTest.java ├── flyweight ├── README.md ├── pom.xml ├── src │ └── main │ │ └── java │ │ └── me │ │ └── zbl │ │ └── flyweight │ │ ├── Application.java │ │ ├── GunFactory.java │ │ ├── GunType.java │ │ ├── HandGun.java │ │ ├── Musket.java │ │ ├── Shooting.java │ │ ├── Sniper.java │ │ ├── Submachine.java │ │ └── WeaponShop.java └── uml │ ├── Flyweight.png │ └── Weapon.png ├── interpreter ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── interpreter │ ├── Application.java │ ├── DivisionExpression.java │ ├── Expression.java │ ├── MinusExpression.java │ ├── MultipleExpression.java │ ├── NumberExpression.java │ └── PlusExpression.java ├── iterator ├── pom.xml └── src │ └── main │ └── java │ ├── Application.java │ ├── BookShelf.java │ ├── BookShelfIterator.java │ ├── Item.java │ ├── ItemIterator.java │ └── ItemType.java ├── mediator ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── mediator │ ├── AbstractPartyMember.java │ ├── Activity.java │ ├── Application.java │ ├── Businessman.java │ ├── Officer.java │ ├── Oldman.java │ ├── Party.java │ ├── PartyImpl.java │ ├── PartyMember.java │ └── Student.java ├── memento ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── memento │ ├── Application.java │ ├── Flower.java │ ├── FlowerType.java │ └── Plant.java ├── observer ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── observer │ ├── Application.java │ ├── Northern.java │ ├── Southern.java │ ├── Time.java │ ├── TimeObserver.java │ └── TimePoint.java ├── pom.xml ├── prototype ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── prototype │ │ ├── Application.java │ │ ├── Driver.java │ │ ├── EstateDriver.java │ │ ├── EstatePassanger.java │ │ ├── EstateVehicle.java │ │ ├── OffRoadDriver.java │ │ ├── OffRoadPassanger.java │ │ ├── OffRoadVehicle.java │ │ ├── Passenger.java │ │ ├── Prototype.java │ │ ├── TeamFactory.java │ │ ├── TeamFactoryImpl.java │ │ └── Vehicle.java │ └── test │ └── java │ └── me │ └── zbl │ └── prototype │ ├── FactoryTest.java │ └── PrototypeTest.java ├── proxy ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── proxy │ ├── Application.java │ ├── Customer.java │ ├── DiningRoom.java │ ├── DiningRoomProxy.java │ └── Room.java ├── readme_template └── README.md ├── singleton ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── me │ │ └── zbl │ │ └── singleton │ │ ├── Application.java │ │ ├── Director.java │ │ ├── EnumDirector.java │ │ ├── LazyInitializationDirector.java │ │ ├── ThreadSafeDoubleCheckLocking.java │ │ └── ThreadSafeLazyLoadDirector.java │ └── test │ └── java │ └── me │ └── zbl │ └── singleton │ ├── DirectorTest.java │ ├── EnumDirectorTest.java │ ├── LazyInitializationDirectorTest.java │ ├── SingletonTest.java │ ├── ThreadSafeDoubleCheckLockingTest.java │ └── ThreadSafeLazyLoadDirectorTest.java ├── state ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── state │ ├── Application.java │ ├── Coder.java │ ├── IdleState.java │ ├── ImpatientState.java │ └── State.java ├── strategy ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── strategy │ ├── Application.java │ ├── BusinessMan.java │ ├── TransportationAirplane.java │ ├── TransportationStrategy.java │ ├── TransportationTrain.java │ └── TransportationVehicle.java ├── template-method ├── pom.xml └── src │ └── main │ └── java │ └── me │ └── zbl │ └── template │ └── method │ ├── Application.java │ ├── LearningMethod.java │ ├── NegativeLearinngMethod.java │ ├── PositiveLearningMethod.java │ └── Student.java └── visitor ├── pom.xml └── src └── main └── java └── me └── zbl └── visitor ├── Application.java ├── Boss.java ├── BossVisitor.java ├── Engineer.java ├── EngineerVisitor.java ├── Manager.java ├── ManagerVisitor.java ├── Unit.java └── UnitVisitor.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | 24 | ####################################################################################### 25 | 26 | *.iml 27 | .idea/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | notifications: 5 | email: true -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 James 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java 实现的面向对象软件设计模式 2 | 3 | 4 | [](https://github.com/JamesZBL/java_design_patterns/blob/master/LICENSE) 5 | [](https://travis-ci.org/JamesZBL/java_design_patterns) 6 | [](https://gitter.im/java_design_patterns/) 7 | 8 | 设计模式(Design pattern)是用于面向对象程序设计的、有效提高代码重用效率、有着明确使用场景分类的程序设计规范的总结。使用设计模式的 9 | 目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦 10 | 的结构一样。在程序设计中 11 | 引入设计模式可以提高代码的可读性和程序运行时的可靠性,使程序设计得到规范和统一。 12 | 13 | 设计模式代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临 14 | 的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。 15 | 16 | 设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了重用代码、让代码更容易被他人理解、 17 | 保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的 18 | 一块块砖石一样。项目中合理地运用设计模式可以完美地解决很多问题,每种模式在现实中都有相应的原理来与之对应,每种模式都描述了一个在我们 19 | 周围不断重复发生的问题,以及该问题的核心解决方案,这也是设计模式能被广泛应用的原因。 20 | 21 | ## 目录 22 | 23 | ### 构建模式 24 | 25 | * Builder (创建者) 26 | 27 | * Abstract Factory (抽象工厂) 28 | 29 | * Factory Method (工厂方法) 30 | 31 | * Prototype (原型模式) 32 | 33 | * Singleton (单例模式) 34 | 35 | 36 | ### 结构模式 37 | 38 | * Adapter (适配器) 39 | 40 | * Bridge (桥接模式) 41 | 42 | * Composite (组合模式) 43 | 44 | * Decorator (装饰模式) 45 | 46 | * Facade (外观模式) 47 | 48 | * Flyweight (享元模式) 49 | 50 | * Proxy (代理模式) 51 | 52 | ### 行为模式 53 | 54 | * Chain of Responsibility (职责链模式) 55 | 56 | * Command (命令模式) 57 | 58 | * Interpreter (解释器模式) 59 | 60 | * Iterator(迭代器模式) 61 | 62 | * Mediator(中介者模式) 63 | 64 | * Memento(备忘录模式) 65 | 66 | * Observer(观察者模式) 67 | 68 | * State(状态模式) 69 | 70 | * Strategy (策略模式) 71 | 72 | * Template Method (模板方法) 73 | 74 | * Visitor(访问者模式) 75 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/Captain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 船长 28 | */ 29 | public interface Captain extends Member { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/Member.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 船上成员以及船 28 | */ 29 | public interface Member { 30 | 31 | String getDescription(); 32 | } 33 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/NewShip.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 新船 28 | */ 29 | public class NewShip implements Ship { 30 | 31 | static final String DESCRIPTION = "我是一艘崭新的船"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/OldCaptain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 老船长 28 | */ 29 | public class OldCaptain implements Captain { 30 | 31 | static final String DESCRIPTION = "我是一名老船长"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/OldSailor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 老水手 28 | */ 29 | public class OldSailor implements Sailor { 30 | 31 | static final String DESCRIPTION = "我是一名老水手"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/OldShip.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 旧船 28 | */ 29 | public class OldShip implements Ship { 30 | 31 | static final String DESCRIPTION = "我是一艘破旧的船"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/PermanentTeamFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 久经考验的团队 28 | */ 29 | public class PermanentTeamFactory implements TeamFactory { 30 | 31 | public Ship createShip() { 32 | return new OldShip(); 33 | } 34 | 35 | public Captain createCaptain() { 36 | return new OldCaptain(); 37 | } 38 | 39 | public Sailor createSailor() { 40 | return new OldSailor(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/Sailor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 水手 28 | */ 29 | public interface Sailor extends Member { 30 | 31 | } 32 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/Ship.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | 27 | /** 28 | * 船 29 | */ 30 | public interface Ship extends Member { 31 | 32 | } 33 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/TeamFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 团队工厂要实现的功能 28 | */ 29 | public interface TeamFactory { 30 | 31 | Ship createShip(); 32 | 33 | Captain createCaptain(); 34 | 35 | Sailor createSailor(); 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/YoungCaptain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 年轻船长 28 | */ 29 | public class YoungCaptain implements Captain { 30 | 31 | static final String DESCRIPTION = "我是一名年轻的船长"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/YoungSailor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 年轻水手 28 | */ 29 | public class YoungSailor implements Sailor { 30 | 31 | static final String DESCRIPTION = "我是年轻的水手"; 32 | 33 | public String getDescription() { 34 | return DESCRIPTION; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/me/zbl/abstractFactory/YoungTeamFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.abstractFactory; 25 | 26 | /** 27 | * 年轻团队工厂 28 | */ 29 | public class YoungTeamFactory implements TeamFactory { 30 | 31 | public Ship createShip() { 32 | return new NewShip(); 33 | } 34 | 35 | public Captain createCaptain() { 36 | return new YoungCaptain(); 37 | } 38 | 39 | public Sailor createSailor() { 40 | return new YoungSailor(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /adapter/README.md: -------------------------------------------------------------------------------- 1 | # 适配器模式 ( Adapter ) 2 | 3 | ## 别名 4 | 5 | > Wrapper (包装器) 6 | 7 | ## 用途 8 | 9 | > 将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容 10 | 而不能一起工作的那些类可以一起工作。 11 | 12 | ## 实例 13 | 14 | > 假设一下你的存储卡里有一些照片,你需要把它们转移到你的电脑上。为了转移它们,你 15 | 需要一些与你的电脑接口兼容的适配器,这样你就可以将存储卡连接到你的电脑上。这里, 16 | 读卡器就起到了适配器的作用。更经典的例子就是人尽皆知的电源适配器了,一个三脚的 17 | 插头不能连接到两个孔的插座上,它需要一个电源适配器,使它与两个分叉的插座兼容。 18 | 19 | ## 模式分析 20 | 21 | > 假定现在有一个小汽车司机,他只会驾驶小型汽车,并不会驾驶公共汽车。 22 | > 23 | > 现在分别定义小汽车 ( Car ) 和公共汽车 ( Bus ) 两种接口 24 | 25 | ``` 26 | public interface Car { 27 | void drive(); 28 | } 29 | 30 | public class Bus { 31 | private static final Logger LOGGER = LoggerFactory.getLogger(Bus.class); 32 | public void run() { 33 | LOGGER.info("公共汽车在行驶"); 34 | } 35 | } 36 | ``` 37 | > 这个司机需要获得一辆小汽车才能进行驾驶活动 38 | 39 | ``` 40 | public class Driver implements Car { 41 | private Car car; 42 | @Override 43 | public void drive() { 44 | car.drive(); 45 | } 46 | public Driver(Car car) { 47 | this.car = car; 48 | } 49 | } 50 | ``` 51 | > 现在这个司机有急事,必须马上赶往公司,可是只借到了一辆公共汽车,并且车上没有任何人, 52 | 现在必须马上将司机的驾驶小汽车的技能转化成驾驶公共汽车的技能 53 | 54 | ``` 55 | public class BusAdapter implements Car { 56 | 57 | private Bus bus; 58 | 59 | public BusAdapter() { 60 | this.bus = new Bus(); 61 | } 62 | 63 | @Override 64 | public void drive() { 65 | bus.run(); 66 | } 67 | } 68 | ``` 69 | > 经过技能转化,司机成功的驾驶公共汽车驶向了公司 70 | 71 | ## 适用场景 72 | 73 | >* 需要使用的类的接口与需要的接口类型不匹配 74 | >* 需要创建一个可重用的类,它可以与不相关的或不可预见的类进行协作,也就是说, 75 | 类不一定具有兼容的接口 76 | >* 需要使用几个现有的子类,但是通过子类化每个子类来调整它们的接口是不切实际的。 77 | 对象适配器可以调整其父类的接口。 78 | 79 | ## 类适配器和对象适配器的区别 80 | 81 | >* 类适配器的适配功能是通过被适配类(接口)的一个对象间接完成的。因此,类适配器不能无法 82 | 对被适配类的所有子类进行适配 83 | >* 类适配器重写了被适配类(接口)的方法,所以,类适配器可以看做被适配类(接口)的一个子类 84 | >* 类适配器一般只需持有一个被适配类(接口)的对象,并不需要额外的对被适配类(接口)的引用 85 | >* 对象适配器可以和多个被适配类(接口)的对象协同工作——即被适配类(接口)本身及其所有子类。适配器也可以一次为所有的被适配类(接口)添加自定义的功能。 86 | >* 对象适配器使得重写被适配类(接口)的方法变成了不可能,这时需要的是被适配类(接口)的子类,而不是被适配类(接口)本身 -------------------------------------------------------------------------------- /adapter/src/main/java/me/zbl/adapter/Application.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.adapter; 25 | 26 | /** 27 | * Adapter 28 | */ 29 | public class Application { 30 | 31 | public static void main(String[] args) { 32 | Driver driver = new Driver(new BusAdapter()); 33 | driver.drive(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /adapter/src/main/java/me/zbl/adapter/Bus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.adapter; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | /** 30 | * 公共汽车 31 | */ 32 | public class Bus { 33 | 34 | private static final Logger LOGGER = LoggerFactory.getLogger(Bus.class); 35 | 36 | public void run() { 37 | LOGGER.info("公共汽车在行驶"); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /adapter/src/main/java/me/zbl/adapter/BusAdapter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.adapter; 25 | 26 | /** 27 | * 公共汽车适配器 28 | */ 29 | public class BusAdapter implements Car { 30 | 31 | private Bus bus; 32 | 33 | public BusAdapter() { 34 | this.bus = new Bus(); 35 | } 36 | 37 | @Override 38 | public void drive() { 39 | bus.run(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /adapter/src/main/java/me/zbl/adapter/Car.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.adapter; 25 | 26 | /** 27 | * 小汽车 28 | */ 29 | public interface Car { 30 | 31 | void drive(); 32 | } 33 | -------------------------------------------------------------------------------- /adapter/src/main/java/me/zbl/adapter/Driver.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.adapter; 25 | 26 | /** 27 | * 公交车司机 28 | */ 29 | public class Driver implements Car { 30 | 31 | private Car car; 32 | 33 | @Override 34 | public void drive() { 35 | car.drive(); 36 | } 37 | 38 | public Driver(Car car) { 39 | this.car = car; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /bridge/src/main/java/me/zbl/bridge/Enemy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | /** 27 | * 敌人 28 | */ 29 | public interface Enemy { 30 | 31 | void onStartWar(); 32 | 33 | void onCombatting(); 34 | 35 | void onStopWar(); 36 | } 37 | -------------------------------------------------------------------------------- /bridge/src/main/java/me/zbl/bridge/IntrepidEnemy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | /** 30 | * 强大的敌人 31 | */ 32 | public class IntrepidEnemy implements Enemy { 33 | 34 | private static final Logger LOGGER = LoggerFactory.getLogger(IntrepidEnemy.class); 35 | 36 | @Override 37 | public void onStartWar() { 38 | LOGGER.info("敌人信心满满,准备迎战"); 39 | } 40 | 41 | @Override 42 | public void onCombatting() { 43 | LOGGER.info("敌人正在积极反抗"); 44 | } 45 | 46 | @Override 47 | public void onStopWar() { 48 | LOGGER.info("双方达成了平手"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /bridge/src/main/java/me/zbl/bridge/TenderEnemy.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | import org.slf4j.Logger; 27 | import org.slf4j.LoggerFactory; 28 | 29 | /** 30 | * 不堪一击的敌人 31 | */ 32 | public class TenderEnemy implements Enemy { 33 | 34 | private static final Logger LOGGER = LoggerFactory.getLogger(TenderEnemy.class); 35 | 36 | @Override 37 | public void onStartWar() { 38 | LOGGER.info("敌人士气不足,无奈迎战"); 39 | } 40 | 41 | @Override 42 | public void onCombatting() { 43 | LOGGER.info("敌人纷纷溃败"); 44 | } 45 | 46 | @Override 47 | public void onStopWar() { 48 | LOGGER.info("敌人不堪一击,只得投降"); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /bridge/src/main/java/me/zbl/bridge/War.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | /** 27 | * 战争 28 | */ 29 | public interface War { 30 | 31 | Enemy getEnemy(); 32 | 33 | void startWar(); 34 | 35 | void combatting(); 36 | 37 | void stopWar(); 38 | } 39 | -------------------------------------------------------------------------------- /bridge/src/test/java/me/zbl/bridge/AncientWarTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.mockito.Mockito.mock; 29 | import static org.mockito.Mockito.spy; 30 | 31 | /** 32 | * 测试古代战争 33 | */ 34 | public class AncientWarTest extends WarTest { 35 | 36 | @Test 37 | public void testAncientWar() { 38 | final AncientWar war = spy(new AncientWar(mock(IntrepidEnemy.class))); 39 | testWarEvents(war); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /bridge/src/test/java/me/zbl/bridge/MordernWarTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.bridge; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.mockito.Mockito.mock; 29 | import static org.mockito.Mockito.spy; 30 | 31 | /** 32 | * 测试现代战争 33 | */ 34 | public class MordernWarTest extends WarTest { 35 | 36 | @Test 37 | public void testMordernWar() { 38 | MordernWar war = spy(new MordernWar(mock(IntrepidEnemy.class))); 39 | testWarEvents(war); 40 | } 41 | } -------------------------------------------------------------------------------- /builder/src/main/java/me/zbl/builder/Nationality.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.builder; 25 | 26 | /** 27 | * 国籍 28 | */ 29 | public enum Nationality { 30 | 31 | CHINA("中国"), RUSSIA("俄罗斯"), USA("美国"), JAPAN("日本"), UK("英国"); 32 | 33 | private String name; 34 | 35 | Nationality(String name) { 36 | this.name = name; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return name; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /builder/src/main/java/me/zbl/builder/SkinColor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.builder; 25 | 26 | /** 27 | * 肤色 28 | */ 29 | public enum SkinColor { 30 | 31 | YELLOW("黄色"), BLACK("黑色"), WHITE("白色"); 32 | 33 | private String color; 34 | 35 | SkinColor(String color) { 36 | this.color = color; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return color; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /chain/src/main/java/me/zbl/chain/Application.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.chain; 25 | 26 | /** 27 | * Chian of Responsibility 28 | */ 29 | public class Application { 30 | 31 | public static void main(String[] args) { 32 | 33 | Commander commander = new Commander(); 34 | 35 | commander.handleRequest(new Request("正常航行", Request.RequestType.SAILING)); 36 | commander.handleRequest(new Request("原地待命", Request.RequestType.AWAIT_ORDERS)); 37 | commander.handleRequest(new Request("发射驱逐导弹", Request.RequestType.SHOOTING)); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /chain/src/main/java/me/zbl/chain/Captain.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.chain; 25 | 26 | /** 27 | * 舰长 28 | */ 29 | public class Captain extends RequestHandler { 30 | 31 | public Captain(RequestHandler next) { 32 | super(next); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "舰长"; 38 | } 39 | 40 | @Override 41 | public void handleRequest(Request request) { 42 | if (request.getType() == Request.RequestType.SAILING) { 43 | printHandleMessage(request); 44 | request.markRequest(); 45 | } else { 46 | super.handleRequest(request); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /chain/src/main/java/me/zbl/chain/Commander.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.chain; 25 | 26 | /** 27 | * 指挥官 28 | */ 29 | public class Commander { 30 | 31 | private RequestHandler chain; 32 | 33 | public Commander() { 34 | createChain(); 35 | } 36 | 37 | private void createChain() { 38 | chain = new Captain(new Gunny(new Gunner(null))); 39 | } 40 | 41 | public void handleRequest(Request request) { 42 | chain.handleRequest(request); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /chain/src/main/java/me/zbl/chain/Gunner.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.chain; 25 | 26 | /** 27 | * 炮手 28 | */ 29 | public class Gunner extends RequestHandler { 30 | 31 | public Gunner(RequestHandler next) { 32 | super(next); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "炮手"; 38 | } 39 | 40 | @Override 41 | public void handleRequest(Request request) { 42 | if (request.getType() == Request.RequestType.SHOOTING) { 43 | printHandleMessage(request); 44 | request.markRequest(); 45 | } else { 46 | super.handleRequest(request); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /chain/src/main/java/me/zbl/chain/Gunny.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.chain; 25 | 26 | /** 27 | * 枪炮长 28 | */ 29 | public class Gunny extends RequestHandler { 30 | 31 | public Gunny(RequestHandler next) { 32 | super(next); 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "枪炮长"; 38 | } 39 | 40 | @Override 41 | public void handleRequest(Request request) { 42 | if (request.getType() == Request.RequestType.AWAIT_ORDERS) { 43 | printHandleMessage(request); 44 | request.markRequest(); 45 | } else { 46 | super.handleRequest(request); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/Color.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 字体颜色 28 | */ 29 | public enum Color { 30 | 31 | RED("红"), BLACK("黑"); 32 | 33 | private String name; 34 | 35 | Color(String name) { 36 | this.name = name; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return name; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/Command.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 命令抽象类 28 | */ 29 | public abstract class Command { 30 | 31 | public abstract void execute(); 32 | 33 | public abstract void undo(); 34 | 35 | public abstract void redo(); 36 | 37 | @Override 38 | public abstract String toString(); 39 | } 40 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/Enlarge.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 设置字体为大号 28 | */ 29 | public class Enlarge extends Command { 30 | 31 | private final AbstractFont font; 32 | private final Size oriSize; 33 | 34 | public Enlarge(AbstractFont font) { 35 | this.font = font; 36 | oriSize = font.getSize(); 37 | } 38 | 39 | @Override 40 | public void execute() { 41 | font.setSize(Size.LARGE); 42 | } 43 | 44 | @Override 45 | public void undo() { 46 | font.setSize(oriSize); 47 | } 48 | 49 | @Override 50 | public void redo() { 51 | execute(); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "设置字体为大号"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/RegularScript.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 字体 28 | */ 29 | public class RegularScript extends AbstractFont { 30 | 31 | public RegularScript() { 32 | setColor(Color.BLACK); 33 | setSize(Size.SMALL); 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "楷体"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/Rubify.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 设置字体颜色为红色的命令 28 | */ 29 | public class Rubify extends Command { 30 | 31 | private final AbstractFont font; 32 | private final Color oriColor; 33 | 34 | public Rubify(AbstractFont font) { 35 | this.font = font; 36 | oriColor = font.getColor(); 37 | } 38 | 39 | @Override 40 | public void execute() { 41 | font.setColor(Color.RED); 42 | } 43 | 44 | @Override 45 | public void undo() { 46 | font.setColor(oriColor); 47 | } 48 | 49 | @Override 50 | public void redo() { 51 | execute(); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "设置字体为红色"; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /command/src/main/java/me/zbl/command/Size.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.command; 25 | 26 | /** 27 | * 字体大小 28 | */ 29 | public enum Size { 30 | 31 | LARGE("大"), SMALL("小"); 32 | 33 | private String name; 34 | 35 | Size(String name) { 36 | this.name = name; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return name; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /composite/src/main/java/me/zbl/composite/Application.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.composite; 25 | 26 | /** 27 | * Composite 28 | */ 29 | public class Application { 30 | 31 | public static void main(String[] args) { 32 | CharacterComposite chineseComposition = new Writer().sentenceByChinese(); 33 | chineseComposition.print(); 34 | 35 | CharacterComposite englishComposition = new Writer().sentenceByEnglish(); 36 | englishComposition.print(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /composite/src/main/java/me/zbl/composite/Character.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | package me.zbl.composite; 25 | 26 | /** 27 | * 字 28 | */ 29 | public class Character extends CharacterComposite { 30 | 31 | private char c; 32 | 33 | public Character(char c) { 34 | this.c = c; 35 | } 36 | 37 | @Override 38 | public void print() { 39 | System.out.print(c); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /composite/src/main/java/me/zbl/composite/ChineseSentence.java: -------------------------------------------------------------------------------- 1 | /** 2 | * MIT License 3 | *
4 | * Copyright (c) 2017 James 5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | *
13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.composite;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * 句子
30 | */
31 | public class ChineseSentence extends CharacterComposite {
32 |
33 | public ChineseSentence(List
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.composite;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * 词
30 | */
31 | public class ChineseWord extends CharacterComposite {
32 |
33 | public ChineseWord(List
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.composite;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * 句子
30 | */
31 | public class EnglishSentence extends CharacterComposite {
32 |
33 | public EnglishSentence(List
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.composite;
25 |
26 | import java.util.List;
27 |
28 | /**
29 | * 词
30 | */
31 | public class EnglishWord extends CharacterComposite {
32 |
33 | public EnglishWord(List
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.decorator;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 木匠的工作
31 | */
32 | public class CarpenterOperation implements Operation {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(CarpenterOperation.class);
35 |
36 | @Override
37 | public void checkBefore() {
38 | LOGGER.info("检查木材");
39 | }
40 |
41 | @Override
42 | public void join() {
43 | LOGGER.info("打造锤把");
44 | }
45 |
46 | @Override
47 | public void chekcAfter() {
48 | LOGGER.info("检查成品锤把");
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/decorator/src/main/java/me/zbl/decorator/Operation.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.decorator;
25 |
26 | /**
27 | * 流水线上操作行为的接口
28 | */
29 | public interface Operation {
30 |
31 | void checkBefore();
32 |
33 | void join();
34 |
35 | void chekcAfter();
36 | }
37 |
--------------------------------------------------------------------------------
/decorator/uml/Decorator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JamesZBL/java_design_patterns/517a9b371396dc60d62f1afb1c25e51b0ef0ba20/decorator/uml/Decorator.png
--------------------------------------------------------------------------------
/decorator/uml/Decorator_impl.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JamesZBL/java_design_patterns/517a9b371396dc60d62f1afb1c25e51b0ef0ba20/decorator/uml/Decorator_impl.png
--------------------------------------------------------------------------------
/facade/src/main/java/me/zbl/facade/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.facade;
25 |
26 | /**
27 | * Facade
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | CourseFacade participator = new CourseFacade();
33 | participator.prepare();
34 | participator.proceed();
35 | participator.stop();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/facade/src/main/java/me/zbl/facade/CourseStudent.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.facade;
25 |
26 | /**
27 | * 学生
28 | */
29 | public class CourseStudent extends CourseParticipator {
30 |
31 | @Override
32 | public String name() {
33 | return "学生";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/facade/src/main/java/me/zbl/facade/CourseTeacher.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.facade;
25 |
26 | /**
27 | * 教师
28 | */
29 | public class CourseTeacher extends CourseParticipator {
30 |
31 | @Override
32 | public String name() {
33 | return "老师";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/factory-method/README.md:
--------------------------------------------------------------------------------
1 | # 工厂方法 ( Factory Method )
2 |
3 | ## 用途
4 |
5 | > 定义一个用于创建对象的接口, 让子类决定实例化哪一个类。 Factory Method使一个类的
6 | 实例化延迟到其子类
7 |
8 | ## 别名
9 |
10 | > 虚构造器 ( Virtual Constructor )
11 |
12 | ## 实例
13 |
14 | > 假定现在有两个厨师,一个只会做中餐,另一个只会做西餐,餐品分为熟食和生食两类。顾客需要顾客需要根据自己的口味来选择对应的厨师并告知其需要熟食还是生食,厨师根据顾客的口味来进行烹制
15 |
16 | ## 模式分析
17 |
18 | > 在基于类的设计中,工厂方法模式通常作为创建模式来使用。它使用工厂方法来处理创建对象的过程,无需指定创建对象的确切类型。客户端通过调用工厂方法来创建对象,这里的方法是在接口中指定的,或是由子类实现的,或是由基类实现,或者通过子类进行方法覆盖,从头至尾无需调用具体类的构造函数
19 | >
20 | > 以厨师为例,首先定义厨师和食物两个接口
21 |
22 | ```
23 | public interface Cook {
24 | Food cookFood(FoodType foodType);
25 | }
26 |
27 | public interface Food {
28 | FoodType getFoodType();
29 | }
30 | ```
31 |
32 | > 定义食物的枚举类型
33 | ```
34 | public enum FoodType {
35 | HOT("热的"), COLD("凉的");
36 | private String name;
37 | FoodType(String foodType) {
38 | this.name = foodType;
39 | }
40 | public String getName() {
41 | return name;
42 | }
43 | }
44 | ```
45 |
46 | > 定义两种厨师的实现类,分别负责西餐和中餐的烹饪工作
47 | ```
48 | public class WesternCook implements Cook {
49 | public Food cookFood(FoodType foodType) {
50 | return new WesternFood(foodType);
51 | }
52 | }
53 |
54 | public class ChineseCook implements Cook {
55 | public Food cookFood(FoodType foodType) {
56 | return new ChineseFood(foodType);
57 | }
58 | }
59 | ```
60 | > 生成食物对象时只需调用厨师对象的工厂方法即可
61 | ```
62 | Cook cook1 = new WesternCook();
63 | Cook cook2 = new ChineseCook();
64 | Food food1 = cook1.cookFood(FoodType.COLD);
65 | Food food2 = cook2.cookFood(FoodType.HOT);
66 | ```
67 |
68 | ## 适用场景
69 |
70 | >* 当一个类不知道它所必须创建的对象的类的时候
71 | >* 当一个类希望由它的子类来指定它所创建的对象的时候
72 | >* 当类将创建对象的职责委托给多个帮助子类中的某一个, 并且你希望将哪一个帮助子类是代理者这一信息局部化的时候
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/ChineseCook.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 中餐厨师
28 | */
29 | public class ChineseCook implements Cook {
30 |
31 | @Override
32 | public Food cookFood(FoodType foodType) {
33 | return new ChineseFood(foodType);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/ChineseFood.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 中餐
28 | */
29 | public class ChineseFood implements Food {
30 |
31 | private FoodType foodType;
32 |
33 | public ChineseFood(FoodType foodType) {
34 | this.foodType = foodType;
35 | }
36 |
37 | @Override
38 | public FoodType getFoodType() {
39 | return foodType;
40 | }
41 |
42 | @Override
43 | public String toString() {
44 | return foodType.getName() + "中餐";
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/Cook.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 厨师
28 | */
29 | public interface Cook {
30 |
31 | Food cookFood(FoodType foodType);
32 | }
33 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/Food.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 食物
28 | */
29 | public interface Food {
30 |
31 | FoodType getFoodType();
32 | }
33 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/FoodType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 食物类型
28 | */
29 | public enum FoodType {
30 |
31 | HOT("热的"), COLD("凉的");
32 |
33 | private String name;
34 |
35 | FoodType(String foodType) {
36 | this.name = foodType;
37 | }
38 |
39 | public String getName() {
40 | return name;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/WesternCook.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 西餐厨师
28 | */
29 | public class WesternCook implements Cook {
30 |
31 | @Override
32 | public Food cookFood(FoodType foodType) {
33 | return new WesternFood(foodType);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/factory-method/src/main/java/me/zbl/factory/method/WesternFood.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.factory.method;
25 |
26 | /**
27 | * 西餐
28 | */
29 | public class WesternFood implements Food {
30 |
31 | private FoodType foodType;
32 |
33 | public WesternFood(FoodType foodType) {
34 | this.foodType = foodType;
35 | }
36 |
37 | @Override
38 | public FoodType getFoodType() {
39 | return foodType;
40 | }
41 |
42 | @Override
43 | public String toString() {
44 | return foodType.getName() + "西餐";
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | /**
27 | * Flyweight
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | WeaponShop shop = new WeaponShop();
33 | shop.enumrateShelves();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/GunType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | /**
27 | * 枪械类型枚举类
28 | */
29 | public enum GunType {
30 | // 手枪
31 | HANDGUN,
32 | // 步枪
33 | MUSKET,
34 | // 狙击枪
35 | SNIPER,
36 | // 冲锋枪
37 | SUBMACHINE;
38 | }
39 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/HandGun.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 手枪
31 | */
32 | public class HandGun implements Shooting {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(HandGun.class);
35 |
36 | @Override
37 | public void shoot() {
38 | LOGGER.info("手枪开火了(Hash={})", System.identityHashCode(this));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/Musket.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 步枪
31 | */
32 | public class Musket implements Shooting {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(Musket.class);
35 |
36 | @Override
37 | public void shoot() {
38 | LOGGER.info("步枪开火了(Hash={})", System.identityHashCode(this));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/Shooting.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | /**
27 | * 武器接口
28 | */
29 | public interface Shooting {
30 |
31 | void shoot();
32 | }
33 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/Sniper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 狙击枪
31 | */
32 | public class Sniper implements Shooting {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(Sniper.class);
35 |
36 | @Override
37 | public void shoot() {
38 | LOGGER.info("狙击枪开火了(Hash={})", System.identityHashCode(this));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/flyweight/src/main/java/me/zbl/flyweight/Submachine.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.flyweight;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 冲锋枪
31 | */
32 | public class Submachine implements Shooting {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(Submachine.class);
35 |
36 | @Override
37 | public void shoot() {
38 | LOGGER.info("冲锋枪开火了(Hash={})", System.identityHashCode(this));
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/flyweight/uml/Flyweight.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JamesZBL/java_design_patterns/517a9b371396dc60d62f1afb1c25e51b0ef0ba20/flyweight/uml/Flyweight.png
--------------------------------------------------------------------------------
/flyweight/uml/Weapon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JamesZBL/java_design_patterns/517a9b371396dc60d62f1afb1c25e51b0ef0ba20/flyweight/uml/Weapon.png
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/DivisionExpression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 除法表达式
28 | */
29 | public class DivisionExpression extends Expression {
30 |
31 | private Expression expressionLeft;
32 | private Expression expressionRight;
33 |
34 | public DivisionExpression(Expression expressionLeft, Expression expressionRight) {
35 | this.expressionLeft = expressionLeft;
36 | this.expressionRight = expressionRight;
37 | }
38 |
39 | @Override
40 | public int interpret() throws Exception{
41 | return expressionLeft.interpret() / expressionRight.interpret();
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "/";
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/Expression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 表达式
28 | */
29 | public abstract class Expression {
30 |
31 | public abstract int interpret() throws Exception;
32 |
33 | @Override
34 | public abstract String toString();
35 | }
36 |
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/MinusExpression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 减法表达式
28 | */
29 | public class MinusExpression extends Expression {
30 |
31 | private Expression expressionLeft;
32 | private Expression expressionRight;
33 |
34 | public MinusExpression(Expression expressionLeft, Expression expressionRight) {
35 | this.expressionLeft = expressionLeft;
36 | this.expressionRight = expressionRight;
37 | }
38 |
39 | @Override
40 | public int interpret() throws Exception {
41 | return expressionLeft.interpret() - expressionRight.interpret();
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "-";
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/MultipleExpression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 乘法表达式
28 | */
29 | public class MultipleExpression extends Expression {
30 |
31 | private Expression expressionLeft;
32 | private Expression expressionRight;
33 |
34 | public MultipleExpression(Expression expressionLeft, Expression expressionRight) {
35 | this.expressionLeft = expressionLeft;
36 | this.expressionRight = expressionRight;
37 | }
38 |
39 | @Override
40 | public int interpret() throws Exception {
41 | return expressionLeft.interpret() * expressionRight.interpret();
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "*";
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/NumberExpression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 数字表达式
28 | */
29 | public class NumberExpression extends Expression {
30 |
31 | private int number;
32 |
33 | public NumberExpression(int number) {
34 | this.number = number;
35 | }
36 |
37 | public NumberExpression(String numberString) {
38 | this.number = Integer.parseInt(numberString);
39 | }
40 |
41 | @Override
42 | public int interpret() {
43 | return number;
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "数字";
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/interpreter/src/main/java/me/zbl/interpreter/PlusExpression.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.interpreter;
25 |
26 | /**
27 | * 加法表达式
28 | */
29 | public class PlusExpression extends Expression {
30 |
31 | private Expression expressionLeft;
32 | private Expression expressionRight;
33 |
34 | public PlusExpression(Expression expressionLeft, Expression expressionRight) {
35 | this.expressionLeft = expressionLeft;
36 | this.expressionRight = expressionRight;
37 | }
38 |
39 | @Override
40 | public int interpret() throws Exception {
41 | return expressionLeft.interpret() + expressionRight.interpret();
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "+";
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/iterator/src/main/java/Item.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | /**
26 | * 元素
27 | */
28 | public class Item {
29 |
30 | private ItemType type;
31 | private String name;
32 |
33 | public Item(ItemType type, String name) {
34 | this.type = type;
35 | this.name = name;
36 | }
37 |
38 | public final void setType(ItemType type) {
39 | this.type = type;
40 | }
41 |
42 | public ItemType getType() {
43 | return type;
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return name;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/iterator/src/main/java/ItemIterator.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | /**
26 | * 迭代器接口
27 | */
28 | public interface ItemIterator {
29 |
30 | boolean hasNext();
31 |
32 | Item next();
33 | }
34 |
--------------------------------------------------------------------------------
/iterator/src/main/java/ItemType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | /**
26 | * 元素类型
27 | */
28 | public enum ItemType {
29 |
30 | IT, FICTION, CARTOON
31 | }
32 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Activity.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 活动
28 | */
29 | public enum Activity {
30 | SHOOT("射击", "Shooting"), GUESS("猜灯谜", "Guess"), DESKTOP_GAME("桌游", "Desktop games"), SING("唱歌", "singing");
31 |
32 | private String name;
33 | private String description;
34 |
35 | Activity(String name, String description) {
36 | this.name = name;
37 | this.description = description;
38 | }
39 |
40 | public String getDescription() {
41 | return description;
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return name;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Businessman.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 商人
28 | */
29 | public class Businessman extends AbstractPartyMember {
30 |
31 | @Override
32 | public String toString() {
33 | return "商人";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Officer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 官员
28 | */
29 | public class Officer extends AbstractPartyMember {
30 |
31 | @Override
32 | public String toString() {
33 | return "官员";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Oldman.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 老人
28 | */
29 | public class Oldman extends AbstractPartyMember {
30 |
31 | @Override
32 | public String toString() {
33 | return "老人";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Party.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 派对
28 | */
29 | public interface Party {
30 |
31 | void addMember(PartyMember member);
32 |
33 | void letAct(PartyMember member, Activity activity);
34 | }
35 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/PartyMember.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 派对成员
28 | */
29 | public interface PartyMember {
30 |
31 | void joinParty(Party party);
32 |
33 | void partyActivity(Activity activity);
34 |
35 | void act(Activity activity);
36 | }
37 |
--------------------------------------------------------------------------------
/mediator/src/main/java/me/zbl/mediator/Student.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.mediator;
25 |
26 | /**
27 | * 学生
28 | */
29 | public class Student extends AbstractPartyMember {
30 |
31 | @Override
32 | public String toString() {
33 | return "学生";
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/memento/src/main/java/me/zbl/memento/FlowerType.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.memento;
25 |
26 | /**
27 | * 花朵的状态
28 | */
29 | public enum FlowerType {
30 |
31 | SEED("种子"), BURGEON("发芽"), BUD("花苞"), BLOOM("开放"), DEAD("凋零");
32 |
33 | private String name;
34 |
35 | FlowerType(String name) {
36 | this.name = name;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return name;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/memento/src/main/java/me/zbl/memento/Plant.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.memento;
25 |
26 | /**
27 | * 植物接口
28 | */
29 | public interface Plant {
30 |
31 | int getWeight();
32 |
33 | int getHeight();
34 |
35 | FlowerType getType();
36 | }
37 |
--------------------------------------------------------------------------------
/observer/src/main/java/me/zbl/observer/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.observer;
25 |
26 | /**
27 | * Observer
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | Time time = new Time();
33 | time.addObserver(new Northern());
34 | time.addObserver(new Southern());
35 |
36 | time.passing();
37 | time.passing();
38 | time.passing();
39 | time.passing();
40 | time.passing();
41 | time.passing();
42 | time.passing();
43 | time.passing();
44 | time.passing();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/observer/src/main/java/me/zbl/observer/TimeObserver.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.observer;
25 |
26 | /**
27 | * 时间观察者接口
28 | */
29 | public interface TimeObserver {
30 |
31 | void update(TimePoint time);
32 | }
33 |
--------------------------------------------------------------------------------
/observer/src/main/java/me/zbl/observer/TimePoint.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.observer;
25 |
26 | /**
27 | * 一天中的几个时间点
28 | */
29 | public enum TimePoint {
30 | MORNING("早晨"), NOON("中午"), AFTERNOON("下午"), EVENING("晚上");
31 | private String name;
32 |
33 | TimePoint(String name) {
34 | this.name = name;
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return name;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/prototype/README.md:
--------------------------------------------------------------------------------
1 | # 原型模式 ( Prototype )
2 |
3 | ## 用途
4 |
5 | > 用原型实例指定创建对象的种类, 并且通过拷贝这些原型创建新的对象
6 |
7 | ## 实例
8 |
9 | > 首先要联想到克隆,最经典的一个例子就是克隆羊多利了。原型模式的核心就是在已经提供的对象的基础上来构造一个新的对象
10 |
11 | ## 模式分析
12 |
13 | > 在 Java 中,克隆操作可以通过实现 Cloneable 接口并且重写定义于 Object 类中的 clone() 方法
14 | ```
15 | public Sheep implements Cloneable {
16 | private String name;
17 | public Sheep(String name){
18 | this.name = name;
19 | }
20 | public void setName(String name){
21 | this.name = name;
22 | }
23 | public String getName(){
24 | return this.name;
25 | }
26 | @Override
27 | public Sheep clone() throws CloneNotSupportedException {
28 | return new Sheep(this.name);
29 | }
30 | }
31 | ```
32 | > 克隆一个羊对象可以这样操作
33 | ```
34 | ...
35 | Sheep origin = new Sheep("团团");
36 | System.out.println(origin.getName()); //团团
37 |
38 | origin.setName("圆圆");
39 | Sheep clone = origin.clone();
40 | System.out.println(clone.getName()); //圆圆
41 | ...
42 | ```
43 |
44 | ## 适用场景
45 |
46 | > 当一个系统应该独立于它的产品创建、 构成和表示时, 要使用Prototype模式; 以及
47 | >* 当要实例化的类是在运行时刻指定时, 例如, 通过动态装载
48 | >* 为了避免创建一个与产品类层次平行的工厂类层次时
49 | >* 当一个类的实例只能有几个不同状态组合中的一种时,建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/Driver.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 司机
28 | */
29 | public abstract class Driver extends Prototype {
30 |
31 | @Override
32 | protected abstract Driver clone() throws CloneNotSupportedException;
33 | }
34 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/EstateDriver.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 旅行车司机
28 | */
29 | public class EstateDriver extends Driver {
30 |
31 | @Override
32 | protected EstateDriver clone() throws CloneNotSupportedException {
33 | return new EstateDriver();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一名旅行车司机";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/EstatePassanger.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 旅行车乘客
28 | */
29 | public class EstatePassanger extends Passenger {
30 |
31 | @Override
32 | protected EstatePassanger clone() throws CloneNotSupportedException {
33 | return new EstatePassanger();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一名旅行车乘客";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/EstateVehicle.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 旅行车
28 | */
29 | public class EstateVehicle extends Vehicle {
30 |
31 | @Override
32 | protected EstateVehicle clone() throws CloneNotSupportedException {
33 | return new EstateVehicle();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一辆旅行车";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/OffRoadDriver.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 越野车司机
28 | */
29 | public class OffRoadDriver extends Driver {
30 |
31 | @Override
32 | protected OffRoadDriver clone() throws CloneNotSupportedException {
33 | return new OffRoadDriver();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一名越野车司机";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/OffRoadPassanger.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 越野车乘客
28 | */
29 | public class OffRoadPassanger extends Passenger {
30 |
31 | @Override
32 | protected OffRoadPassanger clone() throws CloneNotSupportedException {
33 | return new OffRoadPassanger();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一名越野车乘客";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/OffRoadVehicle.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 越野车
28 | */
29 | public class OffRoadVehicle extends Vehicle {
30 |
31 | @Override
32 | protected OffRoadVehicle clone() throws CloneNotSupportedException {
33 | return new OffRoadVehicle();
34 | }
35 |
36 | @Override
37 | public String toString() {
38 | return "这是一辆越野车";
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/Passenger.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 乘客
28 | */
29 | public abstract class Passenger extends Prototype {
30 |
31 | @Override
32 | protected abstract Passenger clone() throws CloneNotSupportedException;
33 | }
34 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/Prototype.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 原型
28 | */
29 | public abstract class Prototype implements Cloneable {
30 |
31 | @Override
32 | protected abstract Prototype clone() throws CloneNotSupportedException;
33 | }
34 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/TeamFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 团队工厂接口
28 | */
29 | public interface TeamFactory {
30 |
31 | Driver createDriver();
32 |
33 | Passenger createPassenger();
34 |
35 | Vehicle createVehicle();
36 | }
37 |
--------------------------------------------------------------------------------
/prototype/src/main/java/me/zbl/prototype/Vehicle.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.prototype;
25 |
26 | /**
27 | * 车
28 | */
29 | public abstract class Vehicle extends Prototype {
30 |
31 | @Override
32 | protected abstract Vehicle clone() throws CloneNotSupportedException;
33 | }
34 |
--------------------------------------------------------------------------------
/proxy/src/main/java/me/zbl/proxy/Customer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.proxy;
25 |
26 | /**
27 | * 顾客
28 | */
29 | public class Customer {
30 |
31 | private String name;
32 |
33 | public Customer(String name) {
34 | this.name = name;
35 | }
36 |
37 | @Override
38 | public String toString() {
39 | return this.name;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/proxy/src/main/java/me/zbl/proxy/DiningRoom.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.proxy;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 餐厅
31 | */
32 | public class DiningRoom implements Room {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(DiningRoom.class);
35 |
36 | @Override
37 | public void enter(Customer customer) {
38 | LOGGER.info("顾客{}进来了", customer);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/proxy/src/main/java/me/zbl/proxy/Room.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.proxy;
25 |
26 | /**
27 | * 房间接口
28 | */
29 | public interface Room {
30 |
31 | void enter(Customer customer);
32 | }
33 |
--------------------------------------------------------------------------------
/readme_template/README.md:
--------------------------------------------------------------------------------
1 | # 组合模式 ( Composite )
2 |
3 | ## 用途
4 |
5 |
6 | ## 适用场景
7 |
8 | ## 模式要点
9 |
10 | ### 组成部分
11 |
12 | ### 协作原理
13 |
14 |
15 |
16 | ## 实例分析
17 |
18 |
19 |
20 | ## 效果
21 |
--------------------------------------------------------------------------------
/singleton/src/main/java/me/zbl/singleton/Director.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * “饿汉式” 的单例实现方式
28 | *
29 | * 可以保证线程安全
30 | */
31 | public final class Director {
32 |
33 | /**
34 | * 静态的本类实例
35 | */
36 | private static final Director INSTANCE = new Director();
37 |
38 | /**
39 | * 私有化的构造方法保证不被其它类调用
40 | */
41 | private Director() {
42 | }
43 |
44 | /**
45 | * 客户端调用获取单例实例
46 | *
47 | * @return 单例实例
48 | */
49 | public static Director getInstance() {
50 | return INSTANCE;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/singleton/src/main/java/me/zbl/singleton/EnumDirector.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 采用枚举类型的单例模式
28 | */
29 | public enum EnumDirector {
30 |
31 | INSTANCE;
32 |
33 | @Override
34 | public String toString() {
35 | return getDeclaringClass().getCanonicalName() + "@" + hashCode();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/singleton/src/main/java/me/zbl/singleton/ThreadSafeLazyLoadDirector.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * “懒汉式”的单例模式-线程安全的
28 | */
29 | public final class ThreadSafeLazyLoadDirector {
30 |
31 | private static volatile ThreadSafeLazyLoadDirector INSTANCE;
32 |
33 | private ThreadSafeLazyLoadDirector() {
34 | // 防止通过反射进行实例化
35 | if (null != INSTANCE) {
36 | throw new IllegalStateException("该实例已经存在");
37 | }
38 | }
39 |
40 | /**
41 | * 此方法被第一次调用时才会生成单例实例,实现懒加载
42 | */
43 | public static synchronized ThreadSafeLazyLoadDirector getInstance() {
44 | if (null == INSTANCE) {
45 | INSTANCE = new ThreadSafeLazyLoadDirector();
46 | }
47 | return INSTANCE;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/singleton/src/test/java/me/zbl/singleton/DirectorTest.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 测试普通单例模式
28 | */
29 | public class DirectorTest extends SingletonTest
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 测试枚举类型单例模式
28 | */
29 | public class EnumDirectorTest extends SingletonTest
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 测试懒加载型单例模式
28 | */
29 | public class LazyInitializationDirectorTest extends SingletonTest
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 测试线程安全的双检锁单例模式
28 | */
29 | public class ThreadSafeDoubleCheckLockingTest extends SingletonTest
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.singleton;
25 |
26 | /**
27 | * 测试线程安全型的懒加载单例模式
28 | */
29 | public class ThreadSafeLazyLoadDirectorTest extends SingletonTest
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.state;
25 |
26 | /**
27 | * State
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | Coder coder = new Coder();
33 | coder.prepare();
34 | coder.timePass();
35 | coder.prepare();
36 | coder.timePass();
37 | coder.prepare();
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/state/src/main/java/me/zbl/state/Coder.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.state;
25 |
26 | /**
27 | * 程序员
28 | */
29 | public class Coder {
30 |
31 | private State state;
32 |
33 | public Coder() {
34 | this.state = new IdleState(this);
35 | }
36 |
37 | private void changeStateTo(State newState) {
38 | this.state = newState;
39 | state.onEnterState();
40 | }
41 |
42 | public void timePass() {
43 | if (state.getClass().equals(ImpatientState.class)) {
44 | changeStateTo(new IdleState(this));
45 | } else {
46 | changeStateTo(new ImpatientState(this));
47 | }
48 | }
49 |
50 | public void prepare() {
51 | state.onPreparing();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/state/src/main/java/me/zbl/state/IdleState.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.state;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 清闲的状态
31 | */
32 | public class IdleState implements State {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(IdleState.class);
35 |
36 | private Coder coder;
37 |
38 | public IdleState(Coder coder) {
39 | this.coder = coder;
40 | }
41 |
42 | @Override
43 | public void onPreparing() {
44 | LOGGER.info("{}正努力使自己变得平静", coder);
45 | }
46 |
47 | @Override
48 | public void onEnterState() {
49 | LOGGER.info("{}正悠闲的听着歌", coder);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/state/src/main/java/me/zbl/state/ImpatientState.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.state;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 焦躁的状态
31 | */
32 | public class ImpatientState implements State {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(ImpatientState.class);
35 |
36 | private Coder coder;
37 |
38 | public ImpatientState(Coder coder) {
39 | this.coder = coder;
40 | }
41 |
42 | @Override
43 | public void onPreparing() {
44 | LOGGER.info("{}面对一堆 bug,开始逐渐焦躁起来", coder);
45 | }
46 |
47 | @Override
48 | public void onEnterState() {
49 | LOGGER.info("{}已经被 bug 搞的进入了极度狂躁的状态", coder);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/state/src/main/java/me/zbl/state/State.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.state;
25 |
26 | /**
27 | * 状态接口
28 | */
29 | public interface State {
30 |
31 | void onPreparing();
32 |
33 | void onEnterState();
34 | }
35 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | /**
27 | * Strategy
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | BusinessMan man = new BusinessMan(new TransportationAirplane());
33 | man.transport();
34 |
35 | man.changetStrategy(new TransportationTrain());
36 | man.transport();
37 |
38 | man.changetStrategy(new TransportationVehicle());
39 | man.transport();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/BusinessMan.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | /**
27 | * 商人
28 | */
29 | public class BusinessMan {
30 |
31 | private TransportationStrategy strategy;
32 |
33 | public BusinessMan(TransportationStrategy strategy) {
34 | this.strategy = strategy;
35 | }
36 |
37 | public void changetStrategy(TransportationStrategy strategy) {
38 | this.strategy = strategy;
39 | }
40 |
41 | public void transport() {
42 | this.strategy.go();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/TransportationAirplane.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 乘飞机
31 | */
32 | public class TransportationAirplane implements TransportationStrategy {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(TransportationAirplane.class);
35 |
36 | @Override
37 | public void go() {
38 | LOGGER.info("乘飞机从北京去广州");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/TransportationStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | /**
27 | * 交通方式策略
28 | */
29 | @FunctionalInterface
30 | public interface TransportationStrategy {
31 |
32 | void go();
33 | }
34 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/TransportationTrain.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 乘火车
31 | */
32 | public class TransportationTrain implements TransportationStrategy {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(TransportationTrain.class);
35 |
36 | @Override
37 | public void go() {
38 | LOGGER.info("乘高铁从北京去上海");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/strategy/src/main/java/me/zbl/strategy/TransportationVehicle.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.strategy;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 驾车
31 | */
32 | public class TransportationVehicle implements TransportationStrategy {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(TransportationVehicle.class);
35 |
36 | @Override
37 | public void go() {
38 | LOGGER.info("驾车从北京去天津");
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/template-method/src/main/java/me/zbl/template/method/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.template.method;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * Template method
31 | */
32 | public class Application {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
35 |
36 | public static void main(String[] args) {
37 | Student student = new Student(new PositiveLearningMethod());
38 | student.learn("上课走神", "同学");
39 | LOGGER.info("更换学习方法");
40 | student.changeMethod(new NegativeLearinngMethod());
41 | student.learn("认证听讲", "老师");
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/template-method/src/main/java/me/zbl/template/method/Student.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.template.method;
25 |
26 | /**
27 | * 学生
28 | */
29 | public class Student {
30 |
31 | private LearningMethod method;
32 |
33 | public Student(LearningMethod method) {
34 | this.method = method;
35 | }
36 |
37 | /**
38 | * 学习
39 | *
40 | * @param description 状态
41 | * @param adviser 请教对象
42 | */
43 | public void learn(String description, String adviser) {
44 | method.learn(description, adviser);
45 | }
46 |
47 | /**
48 | * 更换学习方法
49 | *
50 | * @param method
51 | */
52 | public void changeMethod(LearningMethod method) {
53 | this.method = method;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/Application.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * visitor
28 | */
29 | public class Application {
30 |
31 | public static void main(String[] args) {
32 | Boss boss = new Boss(new Manager(new Engineer(), new Engineer(), new Engineer()), new Manager(new Engineer(), new Engineer()), new Manager(new Engineer()));
33 | boss.beVisited(new BossVisitor());
34 | boss.beVisited(new ManagerVisitor());
35 | boss.beVisited(new EngineerVisitor());
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/Boss.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * 老板
28 | */
29 | public class Boss extends Unit {
30 |
31 | public Boss(Unit... children) {
32 | super(children);
33 | }
34 |
35 | @Override
36 | public void beVisited(UnitVisitor visitor) {
37 | visitor.visitBoss(this);
38 | super.beVisited(visitor);
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "老板";
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/BossVisitor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 老板的访问者
31 | */
32 | public class BossVisitor implements UnitVisitor {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(BossVisitor.class);
35 |
36 | @Override
37 | public void visitEngineer(Engineer engineer) {
38 |
39 | }
40 |
41 | @Override
42 | public void visitBoss(Boss boss) {
43 | LOGGER.info("你好,{}", boss);
44 | }
45 |
46 | @Override
47 | public void visitManager(Manager manager) {
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/Engineer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * 工程师
28 | */
29 | public class Engineer extends Unit {
30 |
31 | public Engineer(Unit... children) {
32 | super(children);
33 | }
34 |
35 | @Override
36 | public void beVisited(UnitVisitor visitor) {
37 | visitor.visitEngineer(this);
38 | super.beVisited(visitor);
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "工程师";
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/EngineerVisitor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 工程师的访问者
31 | */
32 | public class EngineerVisitor implements UnitVisitor {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(EngineerVisitor.class);
35 |
36 | @Override
37 | public void visitEngineer(Engineer engineer) {
38 | LOGGER.info("你好,{}", engineer);
39 | }
40 |
41 | @Override
42 | public void visitBoss(Boss boss) {
43 |
44 | }
45 |
46 | @Override
47 | public void visitManager(Manager manager) {
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/Manager.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * 经理
28 | */
29 | public class Manager extends Unit {
30 |
31 | public Manager(Unit... children) {
32 | super(children);
33 | }
34 |
35 | @Override
36 | public void beVisited(UnitVisitor visitor) {
37 | visitor.visitManager(this);
38 | super.beVisited(visitor);
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "经理";
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/ManagerVisitor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | /**
30 | * 经理的访问者
31 | */
32 | public class ManagerVisitor implements UnitVisitor {
33 |
34 | private static final Logger LOGGER = LoggerFactory.getLogger(ManagerVisitor.class);
35 |
36 | @Override
37 | public void visitEngineer(Engineer engineer) {
38 |
39 | }
40 |
41 | @Override
42 | public void visitBoss(Boss boss) {
43 |
44 | }
45 |
46 | @Override
47 | public void visitManager(Manager manager) {
48 | LOGGER.info("你好,{}", manager);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/Unit.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * 可访问的单元
28 | */
29 | public abstract class Unit {
30 |
31 | private Unit[] children;
32 |
33 | public Unit(Unit... children) {
34 | this.children = children;
35 | }
36 |
37 | /**
38 | * 接受访问
39 | */
40 | public void beVisited(UnitVisitor visitor) {
41 | for (Unit childUnit : children) {
42 | childUnit.beVisited(visitor);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/visitor/src/main/java/me/zbl/visitor/UnitVisitor.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 James
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package me.zbl.visitor;
25 |
26 | /**
27 | * 访问者接口
28 | */
29 | public interface UnitVisitor {
30 |
31 | void visitEngineer(Engineer engineer);
32 |
33 | void visitBoss(Boss boss);
34 |
35 | void visitManager(Manager manager);
36 | }
37 |
--------------------------------------------------------------------------------