├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── abstract-factory ├── .gitignore ├── etc │ ├── abstract-factory.png │ ├── abstract-factory.ucls │ └── abstract-factory_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── abstractfactory │ │ ├── App.java │ │ ├── Army.java │ │ ├── Castle.java │ │ ├── ElfArmy.java │ │ ├── ElfCastle.java │ │ ├── ElfKing.java │ │ ├── ElfKingdomFactory.java │ │ ├── King.java │ │ ├── KingdomFactory.java │ │ ├── OrcArmy.java │ │ ├── OrcCastle.java │ │ ├── OrcKing.java │ │ └── OrcKingdomFactory.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── abstractfactory │ └── AppTest.java ├── adapter ├── etc │ ├── adapter.png │ ├── adapter.ucls │ └── adapter_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── adapter │ │ ├── App.java │ │ ├── Engineer.java │ │ ├── GnomeEngineer.java │ │ ├── GnomeEngineeringManager.java │ │ └── GoblinGlider.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── adapter │ └── AppTest.java ├── bridge ├── etc │ ├── bridge.png │ ├── bridge.ucls │ └── bridge_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── bridge │ │ ├── App.java │ │ ├── BlindingMagicWeapon.java │ │ ├── BlindingMagicWeaponImp.java │ │ ├── Excalibur.java │ │ ├── FlyingMagicWeapon.java │ │ ├── FlyingMagicWeaponImp.java │ │ ├── MagicWeapon.java │ │ ├── MagicWeaponImp.java │ │ ├── Mjollnir.java │ │ ├── SoulEatingMagicWeapon.java │ │ ├── SoulEatingMagicWeaponImp.java │ │ └── Stormbringer.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── bridge │ └── AppTest.java ├── builder ├── .gitignore ├── etc │ ├── builder.png │ ├── builder.ucls │ └── builder_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── builder │ │ ├── App.java │ │ ├── Armor.java │ │ ├── HairColor.java │ │ ├── HairType.java │ │ ├── Hero.java │ │ ├── Profession.java │ │ └── Weapon.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── builder │ └── AppTest.java ├── callback ├── etc │ ├── callback.png │ └── callback.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── callback │ │ ├── App.java │ │ ├── Callback.java │ │ ├── SimpleTask.java │ │ └── Task.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── callback │ └── AppTest.java ├── chain ├── etc │ ├── chain.png │ ├── chain.ucls │ └── chain_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── chain │ │ ├── App.java │ │ ├── OrcCommander.java │ │ ├── OrcKing.java │ │ ├── OrcOfficer.java │ │ ├── OrcSoldier.java │ │ ├── Request.java │ │ ├── RequestHandler.java │ │ └── RequestType.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── chain │ └── AppTest.java ├── command ├── etc │ ├── command.png │ └── command.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── command │ │ ├── App.java │ │ ├── Command.java │ │ ├── Goblin.java │ │ ├── InvisibilitySpell.java │ │ ├── ShrinkSpell.java │ │ ├── Size.java │ │ ├── Target.java │ │ ├── Visibility.java │ │ └── Wizard.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── command │ └── AppTest.java ├── composite ├── etc │ ├── composite.png │ ├── composite.ucls │ └── composite_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── composite │ │ ├── App.java │ │ ├── Letter.java │ │ ├── LetterComposite.java │ │ ├── Messenger.java │ │ ├── Sentence.java │ │ └── Word.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── composite │ └── AppTest.java ├── dao ├── etc │ ├── dao.png │ └── dao.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── dao │ │ ├── App.java │ │ ├── Customer.java │ │ ├── CustomerDao.java │ │ └── CustomerDaoImpl.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── dao │ └── AppTest.java ├── decorator ├── etc │ ├── decorator.png │ ├── decorator.ucls │ └── decorator_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── decorator │ │ ├── App.java │ │ ├── Hostile.java │ │ ├── SmartTroll.java │ │ └── Troll.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── decorator │ └── AppTest.java ├── dependency-injection ├── etc │ ├── dependency-injection.png │ └── dependency-injection.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── dependencyinjection │ │ ├── AdvancedWizard.java │ │ ├── App.java │ │ ├── GuiceWizard.java │ │ ├── OldTobyTobacco.java │ │ ├── RivendellTobacco.java │ │ ├── SecondBreakfastTobacco.java │ │ ├── SimpleWizard.java │ │ ├── Tobacco.java │ │ ├── TobaccoModule.java │ │ └── Wizard.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── dependencyinjection │ └── AppTest.java ├── double-checked-locking ├── etc │ ├── double-checked-locking.png │ ├── double-checked-locking.ucls │ └── double_checked_locking_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── doublecheckedlocking │ │ ├── App.java │ │ ├── Inventory.java │ │ └── Item.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── doublecheckedlocking │ └── AppTest.java ├── double-dispatch ├── etc │ ├── double-dispatch.png │ └── double-dispatch.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── doubledispatch │ │ ├── App.java │ │ ├── FlamingAsteroid.java │ │ ├── GameObject.java │ │ ├── Meteoroid.java │ │ ├── Rectangle.java │ │ ├── SpaceStationIss.java │ │ └── SpaceStationMir.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── doubledispatch │ ├── AppTest.java │ └── RectangleTest.java ├── event-aggregator ├── etc │ ├── classes.png │ └── classes.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── eventaggregator │ │ ├── App.java │ │ ├── Event.java │ │ ├── EventEmitter.java │ │ ├── EventObserver.java │ │ ├── KingJoffrey.java │ │ ├── KingsHand.java │ │ ├── LordBaelish.java │ │ ├── LordVarys.java │ │ ├── Scout.java │ │ └── Weekday.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── eventaggregator │ └── AppTest.java ├── execute-around ├── etc │ ├── execute-around.png │ └── execute-around.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── executearound │ │ ├── App.java │ │ ├── FileWriterAction.java │ │ └── SimpleFileWriter.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── executearound │ └── AppTest.java ├── facade ├── etc │ ├── facade.png │ ├── facade.ucls │ └── facade_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── facade │ │ ├── App.java │ │ ├── DwarvenCartOperator.java │ │ ├── DwarvenGoldDigger.java │ │ ├── DwarvenGoldmineFacade.java │ │ ├── DwarvenMineWorker.java │ │ └── DwarvenTunnelDigger.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── facade │ └── AppTest.java ├── factory-method ├── .gitignore ├── etc │ ├── factory-method.png │ ├── factory-method.ucls │ └── factory-method_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── factorymethod │ │ ├── App.java │ │ ├── Blacksmith.java │ │ ├── ElfBlacksmith.java │ │ ├── ElfWeapon.java │ │ ├── OrcBlacksmith.java │ │ ├── OrcWeapon.java │ │ ├── Weapon.java │ │ └── WeaponType.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── factorymethod │ └── AppTest.java ├── flux ├── etc │ ├── flux.png │ └── flux.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── flux │ │ ├── action │ │ ├── Action.java │ │ ├── ActionType.java │ │ ├── Content.java │ │ ├── ContentAction.java │ │ ├── MenuAction.java │ │ └── MenuItem.java │ │ ├── app │ │ └── App.java │ │ ├── dispatcher │ │ └── Dispatcher.java │ │ ├── store │ │ ├── ContentStore.java │ │ ├── MenuStore.java │ │ └── Store.java │ │ └── view │ │ ├── ContentView.java │ │ ├── MenuView.java │ │ └── View.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── flux │ └── app │ └── AppTest.java ├── flyweight ├── etc │ ├── flyweight.png │ ├── flyweight.ucls │ └── flyweight_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── flyweight │ │ ├── AlchemistShop.java │ │ ├── App.java │ │ ├── HealingPotion.java │ │ ├── HolyWaterPotion.java │ │ ├── InvisibilityPotion.java │ │ ├── PoisonPotion.java │ │ ├── Potion.java │ │ ├── PotionFactory.java │ │ ├── PotionType.java │ │ └── StrengthPotion.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── flyweight │ └── AppTest.java ├── intercepting-filter ├── etc │ ├── intercepting-filter.png │ └── intercepting-filter.ucls ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── iluwatar │ └── interceptingfilter │ ├── AbstractFilter.java │ ├── AddressFilter.java │ ├── App.java │ ├── Client.java │ ├── ContactFilter.java │ ├── DepositFilter.java │ ├── Filter.java │ ├── FilterChain.java │ ├── FilterManager.java │ ├── NameFilter.java │ ├── Order.java │ ├── OrderFilter.java │ └── Target.java ├── interpreter ├── etc │ ├── interpreter.png │ ├── interpreter.ucls │ └── interpreter_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── interpreter │ │ ├── App.java │ │ ├── Expression.java │ │ ├── MinusExpression.java │ │ ├── MultiplyExpression.java │ │ ├── NumberExpression.java │ │ └── PlusExpression.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── interpreter │ └── AppTest.java ├── iterator ├── etc │ ├── iterator.png │ ├── iterator.ucls │ └── iterator_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── iterator │ │ ├── App.java │ │ ├── Item.java │ │ ├── ItemIterator.java │ │ ├── ItemType.java │ │ ├── TreasureChest.java │ │ └── TreasureChestItemIterator.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── iterator │ └── AppTest.java ├── lazy-loading ├── etc │ ├── lazy-loading.png │ └── lazy-loading.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── lazyloading │ │ ├── App.java │ │ ├── Heavy.java │ │ ├── HolderNaive.java │ │ ├── HolderThreadSafe.java │ │ └── Java8Holder.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── lazyloading │ └── AppTest.java ├── mediator ├── etc │ ├── mediator.png │ ├── mediator.ucls │ └── mediator_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── mediator │ │ ├── Action.java │ │ ├── App.java │ │ ├── Hobbit.java │ │ ├── Hunter.java │ │ ├── Party.java │ │ ├── PartyImpl.java │ │ ├── PartyMember.java │ │ ├── PartyMemberBase.java │ │ ├── Rogue.java │ │ └── Wizard.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── mediator │ └── AppTest.java ├── memento ├── etc │ ├── memento.png │ └── memento.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── memento │ │ ├── App.java │ │ ├── Star.java │ │ ├── StarMemento.java │ │ └── StarType.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── memento │ └── AppTest.java ├── model-view-controller ├── etc │ ├── model-view-controller.png │ └── model-view-controller.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── modelviewcontroller │ │ ├── App.java │ │ ├── Fatigue.java │ │ ├── GiantController.java │ │ ├── GiantModel.java │ │ ├── GiantView.java │ │ ├── Health.java │ │ └── Nourishment.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── modelviewcontroller │ └── AppTest.java ├── model-view-presenter ├── etc │ ├── data │ │ └── test.txt │ ├── model-view-presenter.png │ ├── model-view-presenter.ucls │ └── model-view-presenter_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── modelviewpresenter │ │ ├── FileLoader.java │ │ ├── FileSelectorJFrame.java │ │ ├── FileSelectorPresenter.java │ │ ├── FileSelectorStub.java │ │ ├── FileSelectorView.java │ │ └── MainApp.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── modelviewpresenter │ └── FileSelectorPresenterTest.java ├── multiton ├── etc │ ├── multiton.png │ └── multiton.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── multiton │ │ ├── App.java │ │ ├── Nazgul.java │ │ └── NazgulName.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── multiton │ └── AppTest.java ├── null-object ├── etc │ ├── null-object.png │ └── null-object.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── nullobject │ │ ├── App.java │ │ ├── Node.java │ │ ├── NodeImpl.java │ │ └── NullNode.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── nullobject │ └── AppTest.java ├── object-pool ├── etc │ ├── object-pool.png │ └── object-pool.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── objectpool │ │ ├── App.java │ │ ├── ObjectPool.java │ │ ├── Oliphaunt.java │ │ └── OliphauntPool.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── objectpool │ └── AppTest.java ├── observer ├── etc │ ├── observer.png │ ├── observer.ucls │ └── observer_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── observer │ │ ├── App.java │ │ ├── Hobbits.java │ │ ├── Orcs.java │ │ ├── Weather.java │ │ ├── WeatherObserver.java │ │ ├── WeatherType.java │ │ └── generic │ │ ├── GHobbits.java │ │ ├── GOrcs.java │ │ ├── GWeather.java │ │ ├── Observable.java │ │ ├── Observer.java │ │ └── Race.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── observer │ └── AppTest.java ├── poison-pill ├── etc │ ├── poison-pill.png │ └── poison-pill.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── poisonpill │ │ ├── App.java │ │ ├── Consumer.java │ │ ├── MQPublishPoint.java │ │ ├── MQSubscribePoint.java │ │ ├── Message.java │ │ ├── MessageQueue.java │ │ ├── Producer.java │ │ ├── SimpleMessage.java │ │ └── SimpleMessageQueue.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── poisonpill │ └── AppTest.java ├── pom.xml ├── private-class-data ├── etc │ ├── private-class-data.png │ └── private-class-data.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── privateclassdata │ │ ├── App.java │ │ ├── ImmutableStew.java │ │ ├── Stew.java │ │ └── StewData.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── privateclassdata │ └── AppTest.java ├── property ├── etc │ ├── property.png │ └── property.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── property │ │ ├── App.java │ │ ├── Character.java │ │ ├── Prototype.java │ │ └── Stats.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── property │ └── AppTest.java ├── prototype ├── etc │ ├── prototype.png │ ├── prototype.ucls │ └── prototype_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── prototype │ │ ├── App.java │ │ ├── Beast.java │ │ ├── ElfBeast.java │ │ ├── ElfMage.java │ │ ├── ElfWarlord.java │ │ ├── HeroFactory.java │ │ ├── HeroFactoryImpl.java │ │ ├── Mage.java │ │ ├── OrcBeast.java │ │ ├── OrcMage.java │ │ ├── OrcWarlord.java │ │ ├── Prototype.java │ │ └── Warlord.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── prototype │ └── AppTest.java ├── proxy ├── etc │ ├── proxy.png │ ├── proxy.ucls │ └── proxy_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── proxy │ │ ├── App.java │ │ ├── Wizard.java │ │ ├── WizardTower.java │ │ └── WizardTowerProxy.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── proxy │ └── AppTest.java ├── resource-acquisition-is-initialization ├── etc │ ├── resource-acquisition-is-initialization.png │ └── resource-acquisition-is-initialization.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── resourceacquisitionisinitialization │ │ ├── App.java │ │ ├── SlidingDoor.java │ │ └── TreasureChest.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── resourceacquisitionisinitialization │ └── AppTest.java ├── servant ├── etc │ ├── servant-pattern.png │ └── servant-pattern.ucls ├── pom.xml └── src │ ├── etc │ ├── servant.jpg │ ├── servant.svg │ └── servant.xml │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── servant │ │ ├── App.java │ │ ├── King.java │ │ ├── Queen.java │ │ ├── Royalty.java │ │ └── Servant.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── servant │ └── AppTest.java ├── service-layer ├── bin │ └── pom.xml ├── etc │ ├── service-layer.png │ └── service-layer.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── servicelayer │ │ ├── app │ │ └── App.java │ │ ├── common │ │ ├── BaseEntity.java │ │ ├── Dao.java │ │ └── DaoBaseImpl.java │ │ ├── hibernate │ │ └── HibernateUtil.java │ │ ├── magic │ │ ├── MagicService.java │ │ └── MagicServiceImpl.java │ │ ├── servicelayer │ │ └── spell │ │ │ ├── Spell.java │ │ │ ├── SpellDao.java │ │ │ └── SpellDaoImpl.java │ │ ├── spellbook │ │ ├── Spellbook.java │ │ ├── SpellbookDao.java │ │ └── SpellbookDaoImpl.java │ │ └── wizard │ │ ├── Wizard.java │ │ ├── WizardDao.java │ │ └── WizardDaoImpl.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── servicelayer │ └── app │ └── AppTest.java ├── service-locator ├── etc │ ├── service-locator.png │ └── service-locator.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── servicelocator │ │ ├── App.java │ │ ├── InitContext.java │ │ ├── Service.java │ │ ├── ServiceCache.java │ │ ├── ServiceImpl.java │ │ └── ServiceLocator.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── servicelocator │ └── AppTest.java ├── singleton ├── etc │ ├── singleton.png │ ├── singleton.ucls │ └── singleton_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── singleton │ │ ├── App.java │ │ ├── EnumIvoryTower.java │ │ ├── InitializingOnDemandHolderIdiom.java │ │ ├── IvoryTower.java │ │ ├── ThreadSafeDoubleCheckLocking.java │ │ └── ThreadSafeLazyLoadedIvoryTower.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── singleton │ └── AppTest.java ├── specification ├── etc │ ├── specification.png │ └── specification.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── specification │ │ ├── app │ │ └── App.java │ │ ├── creature │ │ ├── AbstractCreature.java │ │ ├── Creature.java │ │ ├── Dragon.java │ │ ├── Goblin.java │ │ ├── KillerBee.java │ │ ├── Octopus.java │ │ ├── Shark.java │ │ └── Troll.java │ │ ├── property │ │ ├── Color.java │ │ ├── Movement.java │ │ └── Size.java │ │ └── selector │ │ ├── ColorSelector.java │ │ ├── MovementSelector.java │ │ └── SizeSelector.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── specification │ └── app │ └── AppTest.java ├── state ├── etc │ ├── state.png │ ├── state.ucls │ └── state_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── state │ │ ├── AngryState.java │ │ ├── App.java │ │ ├── Mammoth.java │ │ ├── PeacefulState.java │ │ └── State.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── state │ └── AppTest.java ├── strategy ├── etc │ ├── strategy.png │ ├── strategy.ucls │ └── strategy_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── strategy │ │ ├── App.java │ │ ├── DragonSlayer.java │ │ ├── DragonSlayingStrategy.java │ │ ├── MeleeStrategy.java │ │ ├── ProjectileStrategy.java │ │ └── SpellStrategy.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── strategy │ └── AppTest.java ├── template-method ├── etc │ ├── template-method.png │ ├── template-method.ucls │ └── template-method_1.png ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── templatemethod │ │ ├── App.java │ │ ├── HalflingThief.java │ │ ├── HitAndRunMethod.java │ │ ├── StealingMethod.java │ │ └── SubtleMethod.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── templatemethod │ └── AppTest.java ├── thread-pool ├── etc │ ├── thread-pool.png │ └── thread-pool.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── threadpool │ │ ├── App.java │ │ ├── CoffeeMakingTask.java │ │ ├── PotatoPeelingTask.java │ │ ├── Task.java │ │ └── Worker.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── threadpool │ └── AppTest.java ├── tolerant-reader ├── etc │ ├── tolerant-reader.png │ └── tolerant-reader.ucls ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── iluwatar │ │ └── tolerantreader │ │ ├── App.java │ │ ├── RainbowFish.java │ │ ├── RainbowFishSerializer.java │ │ └── RainbowFishV2.java │ └── test │ └── java │ └── com │ └── iluwatar │ └── tolerantreader │ └── AppTest.java └── visitor ├── etc ├── visitor.png ├── visitor.ucls └── visitor_1.png ├── pom.xml └── src ├── main └── java │ └── com │ └── iluwatar │ └── visitor │ ├── App.java │ ├── Commander.java │ ├── CommanderVisitor.java │ ├── Sergeant.java │ ├── SergeantVisitor.java │ ├── Soldier.java │ ├── SoldierVisitor.java │ ├── Unit.java │ └── UnitVisitor.java └── test └── java └── com └── iluwatar └── visitor └── AppTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .metadata 3 | .settings 4 | .classpath 5 | .project 6 | *.class 7 | # Package Files # 8 | *.jar 9 | *.war 10 | *.ear 11 | .idea 12 | *.iml 13 | *.swp 14 | 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: 4 | - oraclejdk8 5 | 6 | # whitelist 7 | branches: 8 | only: 9 | - master 10 | 11 | after_success: 12 | - mvn clean test jacoco:report coveralls:report -------------------------------------------------------------------------------- /abstract-factory/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /abstract-factory/etc/abstract-factory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/abstract-factory/etc/abstract-factory.png -------------------------------------------------------------------------------- /abstract-factory/etc/abstract-factory_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/abstract-factory/etc/abstract-factory_1.png -------------------------------------------------------------------------------- /abstract-factory/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | abstract-factory 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/Army.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public interface Army { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/Castle.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public interface Castle { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfArmy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class ElfArmy implements Army { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Elven Army!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfCastle.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class ElfCastle implements Castle { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Elven castle!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKing.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class ElfKing implements King { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Elven king!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKingdomFactory.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | /** 4 | * 5 | * Concrete factory. 6 | * 7 | */ 8 | public class ElfKingdomFactory implements KingdomFactory { 9 | 10 | public Castle createCastle() { 11 | return new ElfCastle(); 12 | } 13 | 14 | public King createKing() { 15 | return new ElfKing(); 16 | } 17 | 18 | public Army createArmy() { 19 | return new ElfArmy(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/King.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public interface King { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/KingdomFactory.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | /** 4 | * 5 | * The factory interface. 6 | * 7 | */ 8 | public interface KingdomFactory { 9 | 10 | Castle createCastle(); 11 | 12 | King createKing(); 13 | 14 | Army createArmy(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcArmy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class OrcArmy implements Army { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Orcish Army!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcCastle.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class OrcCastle implements Castle { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Orcish castle!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKing.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | public class OrcKing implements King { 4 | 5 | @Override 6 | public String toString() { 7 | return "This is the Orc king!"; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKingdomFactory.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | 3 | /** 4 | * 5 | * Concrete factory. 6 | * 7 | */ 8 | public class OrcKingdomFactory implements KingdomFactory { 9 | 10 | public Castle createCastle() { 11 | return new OrcCastle(); 12 | } 13 | 14 | public King createKing() { 15 | return new OrcKing(); 16 | } 17 | 18 | public Army createArmy() { 19 | return new OrcArmy(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /abstract-factory/src/test/java/com/iluwatar/abstractfactory/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.abstractfactory; 2 | import org.junit.Test; 3 | 4 | import com.iluwatar.abstractfactory.App; 5 | 6 | public class AppTest { 7 | 8 | @Test 9 | public void test() { 10 | String[] args = {}; 11 | App.main(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /adapter/etc/adapter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/adapter/etc/adapter.png -------------------------------------------------------------------------------- /adapter/etc/adapter_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/adapter/etc/adapter_1.png -------------------------------------------------------------------------------- /adapter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | adapter 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /adapter/src/main/java/com/iluwatar/adapter/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | /** 4 | * 5 | * There are two variations of the Adapter pattern: The class adapter implements 6 | * the adaptee's interface whereas the object adapter uses composition to 7 | * contain the adaptee in the adapter object. This example uses the object 8 | * adapter approach. 9 | * 10 | * The Adapter (GnomeEngineer) converts the interface of the target class 11 | * (GoblinGlider) into a suitable one expected by the client 12 | * (GnomeEngineeringManager). 13 | * 14 | */ 15 | public class App { 16 | 17 | public static void main(String[] args) { 18 | Engineer manager = new GnomeEngineeringManager(); 19 | manager.operateDevice(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /adapter/src/main/java/com/iluwatar/adapter/Engineer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | /** 4 | * 5 | * Engineers can operate devices. 6 | * 7 | */ 8 | public interface Engineer { 9 | 10 | void operateDevice(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /adapter/src/main/java/com/iluwatar/adapter/GnomeEngineer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | /** 4 | * 5 | * Adapter class. Adapts the interface of the device (GoblinGlider) into 6 | * Engineer interface expected by the client (GnomeEngineeringManager). 7 | * 8 | */ 9 | public class GnomeEngineer implements Engineer { 10 | 11 | private GoblinGlider glider; 12 | 13 | public GnomeEngineer() { 14 | glider = new GoblinGlider(); 15 | } 16 | 17 | @Override 18 | public void operateDevice() { 19 | glider.attachGlider(); 20 | glider.gainSpeed(); 21 | glider.takeOff(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | /** 4 | * 5 | * GnomeEngineering manager uses Engineer to operate devices. 6 | * 7 | */ 8 | public class GnomeEngineeringManager implements Engineer { 9 | 10 | private Engineer engineer; 11 | 12 | public GnomeEngineeringManager() { 13 | engineer = new GnomeEngineer(); 14 | } 15 | 16 | @Override 17 | public void operateDevice() { 18 | engineer.operateDevice(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /adapter/src/main/java/com/iluwatar/adapter/GoblinGlider.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | /** 4 | * 5 | * Device class (adaptee in the pattern). 6 | * 7 | */ 8 | public class GoblinGlider { 9 | 10 | public void attachGlider() { 11 | System.out.println("Glider attached."); 12 | } 13 | 14 | public void gainSpeed() { 15 | System.out.println("Gaining speed."); 16 | } 17 | 18 | public void takeOff() { 19 | System.out.println("Lift-off!"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /adapter/src/test/java/com/iluwatar/adapter/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.adapter; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.adapter.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /bridge/etc/bridge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/bridge/etc/bridge.png -------------------------------------------------------------------------------- /bridge/etc/bridge_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/bridge/etc/bridge_1.png -------------------------------------------------------------------------------- /bridge/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | bridge 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class BlindingMagicWeapon extends MagicWeapon { 4 | 5 | public BlindingMagicWeapon(BlindingMagicWeaponImp imp) { 6 | super(imp); 7 | } 8 | 9 | @Override 10 | public BlindingMagicWeaponImp getImp() { 11 | return (BlindingMagicWeaponImp) imp; 12 | } 13 | 14 | @Override 15 | public void wield() { 16 | getImp().wieldImp(); 17 | } 18 | 19 | @Override 20 | public void swing() { 21 | getImp().swingImp(); 22 | } 23 | 24 | @Override 25 | public void unwield() { 26 | getImp().unwieldImp(); 27 | } 28 | 29 | public void blind() { 30 | getImp().blindImp(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeaponImp.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public abstract class BlindingMagicWeaponImp extends MagicWeaponImp { 4 | 5 | public abstract void blindImp(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/Excalibur.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class Excalibur extends BlindingMagicWeaponImp { 4 | 5 | @Override 6 | public void wieldImp() { 7 | System.out.println("wielding Excalibur"); 8 | } 9 | 10 | @Override 11 | public void swingImp() { 12 | System.out.println("swinging Excalibur"); 13 | } 14 | 15 | @Override 16 | public void unwieldImp() { 17 | System.out.println("unwielding Excalibur"); 18 | } 19 | 20 | @Override 21 | public void blindImp() { 22 | System.out 23 | .println("bright light streams from Excalibur blinding the enemy"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class FlyingMagicWeapon extends MagicWeapon { 4 | 5 | public FlyingMagicWeapon(FlyingMagicWeaponImp imp) { 6 | super(imp); 7 | } 8 | 9 | public FlyingMagicWeaponImp getImp() { 10 | return (FlyingMagicWeaponImp) imp; 11 | } 12 | 13 | @Override 14 | public void wield() { 15 | getImp().wieldImp(); 16 | } 17 | 18 | @Override 19 | public void swing() { 20 | getImp().swingImp(); 21 | } 22 | 23 | @Override 24 | public void unwield() { 25 | getImp().unwieldImp(); 26 | } 27 | 28 | public void fly() { 29 | getImp().flyImp(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeaponImp.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public abstract class FlyingMagicWeaponImp extends MagicWeaponImp { 4 | 5 | public abstract void flyImp(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/MagicWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | /** 4 | * 5 | * Abstraction interface. 6 | * 7 | */ 8 | public abstract class MagicWeapon { 9 | 10 | protected MagicWeaponImp imp; 11 | 12 | public MagicWeapon(MagicWeaponImp imp) { 13 | this.imp = imp; 14 | } 15 | 16 | public abstract void wield(); 17 | 18 | public abstract void swing(); 19 | 20 | public abstract void unwield(); 21 | 22 | public MagicWeaponImp getImp() { 23 | return imp; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/MagicWeaponImp.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | /** 4 | * 5 | * Implementation interface. 6 | * 7 | */ 8 | public abstract class MagicWeaponImp { 9 | 10 | public abstract void wieldImp(); 11 | 12 | public abstract void swingImp(); 13 | 14 | public abstract void unwieldImp(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/Mjollnir.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class Mjollnir extends FlyingMagicWeaponImp { 4 | 5 | @Override 6 | public void wieldImp() { 7 | System.out.println("wielding Mjollnir"); 8 | } 9 | 10 | @Override 11 | public void swingImp() { 12 | System.out.println("swinging Mjollnir"); 13 | } 14 | 15 | @Override 16 | public void unwieldImp() { 17 | System.out.println("unwielding Mjollnir"); 18 | } 19 | 20 | @Override 21 | public void flyImp() { 22 | System.out 23 | .println("Mjollnir hits the enemy in the air and returns back to the owner's hand"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class SoulEatingMagicWeapon extends MagicWeapon { 4 | 5 | public SoulEatingMagicWeapon(SoulEatingMagicWeaponImp imp) { 6 | super(imp); 7 | } 8 | 9 | @Override 10 | public SoulEatingMagicWeaponImp getImp() { 11 | return (SoulEatingMagicWeaponImp) imp; 12 | } 13 | 14 | @Override 15 | public void wield() { 16 | getImp().wieldImp(); 17 | } 18 | 19 | @Override 20 | public void swing() { 21 | getImp().swingImp(); 22 | } 23 | 24 | @Override 25 | public void unwield() { 26 | getImp().unwieldImp(); 27 | } 28 | 29 | public void eatSoul() { 30 | getImp().eatSoulImp(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeaponImp.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public abstract class SoulEatingMagicWeaponImp extends MagicWeaponImp { 4 | 5 | public abstract void eatSoulImp(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /bridge/src/main/java/com/iluwatar/bridge/Stormbringer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | public class Stormbringer extends SoulEatingMagicWeaponImp { 4 | 5 | @Override 6 | public void wieldImp() { 7 | System.out.println("wielding Stormbringer"); 8 | } 9 | 10 | @Override 11 | public void swingImp() { 12 | System.out.println("swinging Stormbringer"); 13 | } 14 | 15 | @Override 16 | public void unwieldImp() { 17 | System.out.println("unwielding Stormbringer"); 18 | } 19 | 20 | @Override 21 | public void eatSoulImp() { 22 | System.out.println("Stormbringer devours the enemy's soul"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /bridge/src/test/java/com/iluwatar/bridge/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.bridge; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.bridge.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /builder/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /builder/etc/builder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/builder/etc/builder.png -------------------------------------------------------------------------------- /builder/etc/builder_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/builder/etc/builder_1.png -------------------------------------------------------------------------------- /builder/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | builder 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /builder/src/main/java/com/iluwatar/builder/Armor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | public enum Armor { 4 | 5 | CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail"); 6 | 7 | private String title; 8 | 9 | Armor(String title) { 10 | this.title = title; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return title; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /builder/src/main/java/com/iluwatar/builder/HairColor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | public enum HairColor { 4 | 5 | WHITE, BLOND, RED, BROWN, BLACK; 6 | 7 | @Override 8 | public String toString() { 9 | return name().toLowerCase(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /builder/src/main/java/com/iluwatar/builder/HairType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | public enum HairType { 4 | 5 | BALD("bald"), SHORT("short"), CURLY("curly"), LONG_STRAIGHT("long straight"), LONG_CURLY("long curly"); 6 | 7 | private String title; 8 | 9 | HairType(String title) { 10 | this.title = title; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return title; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /builder/src/main/java/com/iluwatar/builder/Profession.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | public enum Profession { 4 | 5 | WARRIOR, THIEF, MAGE, PRIEST; 6 | 7 | @Override 8 | public String toString() { 9 | return name().toLowerCase(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /builder/src/main/java/com/iluwatar/builder/Weapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | public enum Weapon { 4 | 5 | DAGGER, SWORD, AXE, WARHAMMER, BOW; 6 | 7 | @Override 8 | public String toString() { 9 | return name().toLowerCase(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /builder/src/test/java/com/iluwatar/builder/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.builder; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar. builder.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /callback/etc/callback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/callback/etc/callback.png -------------------------------------------------------------------------------- /callback/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | callback 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /callback/src/main/java/com/iluwatar/callback/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.callback; 2 | 3 | /** 4 | * Callback pattern is more native for functional languages where function is treated as first-class citizen. 5 | * Prior to Java8 can be simulated using simple (alike command) interfaces. 6 | */ 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | Task task = new SimpleTask(); 11 | Callback callback = new Callback() { 12 | @Override 13 | public void call() { 14 | System.out.println("I'm done now."); 15 | } 16 | }; 17 | task.executeWith(callback); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /callback/src/main/java/com/iluwatar/callback/Callback.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.callback; 2 | 3 | /** 4 | * Callback interface 5 | */ 6 | public interface Callback { 7 | 8 | public void call(); 9 | } 10 | -------------------------------------------------------------------------------- /callback/src/main/java/com/iluwatar/callback/SimpleTask.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.callback; 2 | 3 | /** 4 | * Implementation of task that need to be executed 5 | */ 6 | public class SimpleTask extends Task { 7 | 8 | @Override 9 | public void execute() { 10 | System.out.println("Perform some important activity."); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /callback/src/main/java/com/iluwatar/callback/Task.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.callback; 2 | 3 | /** 4 | * Template-method class for callback hook execution 5 | */ 6 | public abstract class Task { 7 | 8 | public final void executeWith(Callback callback) { 9 | execute(); 10 | if (callback != null) { 11 | callback.call(); 12 | } 13 | } 14 | 15 | public abstract void execute(); 16 | } 17 | -------------------------------------------------------------------------------- /callback/src/test/java/com/iluwatar/callback/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.callback; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.callback.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /chain/etc/chain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/chain/etc/chain.png -------------------------------------------------------------------------------- /chain/etc/chain_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/chain/etc/chain_1.png -------------------------------------------------------------------------------- /chain/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | chain 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | /** 4 | * 5 | * Chain of Responsibility organizes request handlers (RequestHandler) into a 6 | * chain where each handler has a chance to act on the request on its turn. In 7 | * this example the king (OrcKing) makes requests and the military orcs 8 | * (OrcCommander, OrcOfficer, OrcSoldier) form the handler chain. 9 | * 10 | */ 11 | public class App { 12 | 13 | public static void main(String[] args) { 14 | 15 | OrcKing king = new OrcKing(); 16 | king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); 17 | king.makeRequest(new Request(RequestType.TORTURE_PRISONER, 18 | "torture prisoner")); 19 | king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/OrcCommander.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | public class OrcCommander extends RequestHandler { 4 | 5 | public OrcCommander(RequestHandler handler) { 6 | super(handler); 7 | } 8 | 9 | @Override 10 | public void handleRequest(Request req) { 11 | if (req.getRequestType().equals(RequestType.DEFEND_CASTLE)) { 12 | printHandling(req); 13 | } else { 14 | super.handleRequest(req); 15 | } 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Orc commander"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/OrcKing.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | /** 4 | * 5 | * Makes requests that are handled by the chain. 6 | * 7 | */ 8 | public class OrcKing { 9 | 10 | RequestHandler chain; 11 | 12 | public OrcKing() { 13 | buildChain(); 14 | } 15 | 16 | private void buildChain() { 17 | chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null))); 18 | } 19 | 20 | public void makeRequest(Request req) { 21 | chain.handleRequest(req); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/OrcOfficer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | public class OrcOfficer extends RequestHandler { 4 | 5 | public OrcOfficer(RequestHandler handler) { 6 | super(handler); 7 | } 8 | 9 | @Override 10 | public void handleRequest(Request req) { 11 | if (req.getRequestType().equals(RequestType.TORTURE_PRISONER)) { 12 | printHandling(req); 13 | } else { 14 | super.handleRequest(req); 15 | } 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Orc officer"; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/OrcSoldier.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | public class OrcSoldier extends RequestHandler { 4 | 5 | public OrcSoldier(RequestHandler handler) { 6 | super(handler); 7 | } 8 | 9 | @Override 10 | public void handleRequest(Request req) { 11 | if (req.getRequestType().equals(RequestType.COLLECT_TAX)) { 12 | printHandling(req); 13 | } else { 14 | super.handleRequest(req); 15 | } 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "Orc soldier"; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/RequestHandler.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | public abstract class RequestHandler { 4 | 5 | private RequestHandler next; 6 | 7 | public RequestHandler(RequestHandler next) { 8 | this.next = next; 9 | } 10 | 11 | public void handleRequest(Request req) { 12 | if (next != null) { 13 | next.handleRequest(req); 14 | } 15 | } 16 | 17 | protected void printHandling(Request req) { 18 | System.out.println(this + " handling request \"" + req + "\""); 19 | } 20 | 21 | @Override 22 | public abstract String toString(); 23 | } 24 | -------------------------------------------------------------------------------- /chain/src/main/java/com/iluwatar/chain/RequestType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | public enum RequestType { 4 | 5 | DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX 6 | 7 | } 8 | -------------------------------------------------------------------------------- /chain/src/test/java/com/iluwatar/chain/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.chain; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.chain.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /command/etc/command.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/command/etc/command.png -------------------------------------------------------------------------------- /command/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | command 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/Command.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * Interface for Commands. 6 | * 7 | */ 8 | public abstract class Command { 9 | 10 | public abstract void execute(Target target); 11 | 12 | public abstract void undo(); 13 | 14 | public abstract void redo(); 15 | 16 | @Override 17 | public abstract String toString(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/Goblin.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * Goblin is the target of the spells 6 | * 7 | */ 8 | public class Goblin extends Target { 9 | 10 | public Goblin() { 11 | setSize(Size.NORMAL); 12 | setVisibility(Visibility.VISIBLE); 13 | } 14 | 15 | @Override 16 | public String toString() { 17 | return "Goblin"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/InvisibilitySpell.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * InvisibilitySpell is a concrete command 6 | * 7 | */ 8 | public class InvisibilitySpell extends Command { 9 | 10 | private Target target; 11 | 12 | @Override 13 | public void execute(Target target) { 14 | target.setVisibility(Visibility.INVISIBLE); 15 | this.target = target; 16 | } 17 | 18 | @Override 19 | public void undo() { 20 | if (target != null) { 21 | target.setVisibility(Visibility.VISIBLE); 22 | } 23 | } 24 | 25 | @Override 26 | public void redo() { 27 | if (target != null) { 28 | target.setVisibility(Visibility.INVISIBLE); 29 | } 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Invisibility spell"; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/ShrinkSpell.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * ShrinkSpell is a concrete command 6 | * 7 | */ 8 | public class ShrinkSpell extends Command { 9 | 10 | private Size oldSize; 11 | private Target target; 12 | 13 | @Override 14 | public void execute(Target target) { 15 | oldSize = target.getSize(); 16 | target.setSize(Size.SMALL); 17 | this.target = target; 18 | } 19 | 20 | @Override 21 | public void undo() { 22 | if (oldSize != null && target != null) { 23 | Size temp = target.getSize(); 24 | target.setSize(oldSize); 25 | oldSize = temp; 26 | } 27 | } 28 | 29 | @Override 30 | public void redo() { 31 | undo(); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "Shrink spell"; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/Size.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * Enumeration for target size. 6 | * 7 | */ 8 | public enum Size { 9 | 10 | SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED(""); 11 | 12 | private String title; 13 | 14 | Size(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /command/src/main/java/com/iluwatar/command/Visibility.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | /** 4 | * 5 | * Enumeration for target visibility. 6 | * 7 | */ 8 | public enum Visibility { 9 | 10 | VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED(""); 11 | 12 | private String title; 13 | 14 | Visibility(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /command/src/test/java/com/iluwatar/command/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.command; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.command.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /composite/etc/composite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/composite/etc/composite.png -------------------------------------------------------------------------------- /composite/etc/composite_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/composite/etc/composite_1.png -------------------------------------------------------------------------------- /composite/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | composite 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composite/src/main/java/com/iluwatar/composite/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | /** 4 | * 5 | * With Composite we can treat tree hierarchies of objects with uniform 6 | * interface (LetterComposite). In this example we have sentences composed of 7 | * words composed of letters. 8 | * 9 | */ 10 | public class App { 11 | 12 | public static void main(String[] args) { 13 | System.out.println("Message from the orcs: "); 14 | 15 | LetterComposite orcMessage = new Messenger().messageFromOrcs(); 16 | orcMessage.print(); 17 | 18 | System.out.println("\n"); 19 | 20 | System.out.println("Message from the elves: "); 21 | 22 | LetterComposite elfMessage = new Messenger().messageFromElves(); 23 | elfMessage.print(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /composite/src/main/java/com/iluwatar/composite/Letter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | public class Letter extends LetterComposite { 4 | 5 | private char c; 6 | 7 | public Letter(char c) { 8 | this.c = c; 9 | } 10 | 11 | @Override 12 | protected void printThisBefore() { 13 | System.out.print(c); 14 | } 15 | 16 | @Override 17 | protected void printThisAfter() { 18 | // nop 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /composite/src/main/java/com/iluwatar/composite/LetterComposite.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * Composite interface. 9 | * 10 | */ 11 | public abstract class LetterComposite { 12 | 13 | private List children = new ArrayList(); 14 | 15 | public void add(LetterComposite letter) { 16 | children.add(letter); 17 | } 18 | 19 | public int count() { 20 | return children.size(); 21 | } 22 | 23 | protected abstract void printThisBefore(); 24 | 25 | protected abstract void printThisAfter(); 26 | 27 | public void print() { 28 | printThisBefore(); 29 | for (LetterComposite letter : children) { 30 | letter.print(); 31 | } 32 | printThisAfter(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /composite/src/main/java/com/iluwatar/composite/Sentence.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | import java.util.List; 4 | 5 | public class Sentence extends LetterComposite { 6 | 7 | public Sentence(List words) { 8 | for (Word w : words) { 9 | this.add(w); 10 | } 11 | } 12 | 13 | @Override 14 | protected void printThisBefore() { 15 | // nop 16 | } 17 | 18 | @Override 19 | protected void printThisAfter() { 20 | System.out.print("."); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /composite/src/main/java/com/iluwatar/composite/Word.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | import java.util.List; 4 | 5 | public class Word extends LetterComposite { 6 | 7 | public Word(List letters) { 8 | for (Letter l : letters) { 9 | this.add(l); 10 | } 11 | } 12 | 13 | @Override 14 | protected void printThisBefore() { 15 | System.out.print(" "); 16 | } 17 | 18 | @Override 19 | protected void printThisAfter() { 20 | // nop 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /composite/src/test/java/com/iluwatar/composite/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.composite; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.composite.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /dao/etc/dao.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/dao/etc/dao.png -------------------------------------------------------------------------------- /dao/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | dao 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /dao/src/main/java/com/iluwatar/dao/CustomerDao.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dao; 2 | 3 | import java.util.List; 4 | 5 | public interface CustomerDao { 6 | public List getAllCustomers(); 7 | public Customer getCusterById(int id); 8 | public void addCustomer(Customer customer); 9 | public void updateCustomer(Customer customer); 10 | public void deleteCustomer(Customer customer); 11 | } -------------------------------------------------------------------------------- /dao/src/test/java/com/iluwatar/dao/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dao; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.dao.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /decorator/etc/decorator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/decorator/etc/decorator.png -------------------------------------------------------------------------------- /decorator/etc/decorator_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/decorator/etc/decorator_1.png -------------------------------------------------------------------------------- /decorator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | decorator 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /decorator/src/main/java/com/iluwatar/decorator/Hostile.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.decorator; 2 | 3 | /** 4 | * 5 | * Interface for the hostile enemies. 6 | * 7 | */ 8 | public interface Hostile { 9 | 10 | void attack(); 11 | 12 | void fleeBattle(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /decorator/src/main/java/com/iluwatar/decorator/SmartTroll.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.decorator; 2 | 3 | /** 4 | * SmartTroll is a decorator for Hostile objects. 5 | * The calls to the Hostile interface are intercepted 6 | * and decorated. Finally the calls are delegated 7 | * to the decorated Hostile object. 8 | * 9 | */ 10 | public class SmartTroll implements Hostile { 11 | 12 | private Hostile decorated; 13 | 14 | public SmartTroll(Hostile decorated) { 15 | this.decorated = decorated; 16 | } 17 | 18 | @Override 19 | public void attack() { 20 | System.out.println("The troll throws a rock at you!"); 21 | decorated.attack(); 22 | } 23 | 24 | @Override 25 | public void fleeBattle() { 26 | System.out.println("The troll calls for help!"); 27 | decorated.fleeBattle(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /decorator/src/main/java/com/iluwatar/decorator/Troll.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.decorator; 2 | 3 | /** 4 | * 5 | * Troll implements Hostile interface directly. 6 | * 7 | */ 8 | public class Troll implements Hostile { 9 | 10 | public void attack() { 11 | System.out.println("The troll swings at you with a club!"); 12 | } 13 | 14 | public void fleeBattle() { 15 | System.out.println("The troll shrieks in horror and runs away!"); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /decorator/src/test/java/com/iluwatar/decorator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.decorator; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.decorator.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /dependency-injection/etc/dependency-injection.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/dependency-injection/etc/dependency-injection.png -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/AdvancedWizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * AdvancedWizard implements inversion of control. 6 | * It depends on abstraction that can be injected through 7 | * its constructor. 8 | * 9 | */ 10 | public class AdvancedWizard implements Wizard { 11 | 12 | private Tobacco tobacco; 13 | 14 | public AdvancedWizard(Tobacco tobacco) { 15 | this.tobacco = tobacco; 16 | } 17 | 18 | @Override 19 | public void smoke() { 20 | tobacco.smoke(this); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/GuiceWizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | import javax.inject.Inject; 4 | 5 | /** 6 | * 7 | * GuiceWizard implements inversion of control. 8 | * Its dependencies are injected through its constructor 9 | * by Guice framework. 10 | * 11 | */ 12 | public class GuiceWizard implements Wizard { 13 | 14 | private Tobacco tobacco; 15 | 16 | @Inject 17 | public GuiceWizard(Tobacco tobacco) { 18 | this.tobacco = tobacco; 19 | } 20 | 21 | @Override 22 | public void smoke() { 23 | tobacco.smoke(this); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/OldTobyTobacco.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * OldTobyTobacco concrete Tobacco implementation 6 | * 7 | */ 8 | public class OldTobyTobacco extends Tobacco { 9 | } 10 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/RivendellTobacco.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * RivendellTobacco concrete Tobacco implementation 6 | * 7 | */ 8 | public class RivendellTobacco extends Tobacco { 9 | } 10 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/SecondBreakfastTobacco.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * SecondBreakfastTobacco concrete Tobacco implementation 6 | * 7 | */ 8 | public class SecondBreakfastTobacco extends Tobacco { 9 | } 10 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/SimpleWizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * Naive Wizard implementation violating the inversion of control principle. 6 | * It should depend on abstraction instead. 7 | * 8 | */ 9 | public class SimpleWizard implements Wizard { 10 | 11 | private OldTobyTobacco tobacco = new OldTobyTobacco(); 12 | 13 | public void smoke() { 14 | tobacco.smoke(this); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/Tobacco.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * Tobacco abstraction 6 | * 7 | */ 8 | public abstract class Tobacco { 9 | 10 | public void smoke(Wizard wizard) { 11 | System.out.println(String.format("%s smoking %s", wizard.getClass().getSimpleName(), this.getClass().getSimpleName())); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/TobaccoModule.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | import com.google.inject.AbstractModule; 4 | 5 | /** 6 | * 7 | * Guice module for binding certain concrete Tobacco implementation. 8 | * 9 | */ 10 | public class TobaccoModule extends AbstractModule { 11 | 12 | @Override 13 | protected void configure() { 14 | bind(Tobacco.class).to(RivendellTobacco.class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /dependency-injection/src/main/java/com/iluwatar/dependencyinjection/Wizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | /** 4 | * 5 | * Wizard interface 6 | * 7 | */ 8 | public interface Wizard { 9 | 10 | void smoke(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /dependency-injection/src/test/java/com/iluwatar/dependencyinjection/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.dependencyinjection; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.dependencyinjection.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /double-checked-locking/etc/double-checked-locking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/double-checked-locking/etc/double-checked-locking.png -------------------------------------------------------------------------------- /double-checked-locking/etc/double_checked_locking_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/double-checked-locking/etc/double_checked_locking_1.png -------------------------------------------------------------------------------- /double-checked-locking/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.iluwatar 5 | java-design-patterns 6 | 1.1.0 7 | 8 | double-checked-locking 9 | 10 | 11 | junit 12 | junit 13 | test 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /double-checked-locking/src/main/java/com/iluwatar/doublecheckedlocking/Item.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doublecheckedlocking; 2 | 3 | public class Item { 4 | String name; 5 | int level; 6 | } 7 | -------------------------------------------------------------------------------- /double-checked-locking/src/test/java/com/iluwatar/doublecheckedlocking/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doublecheckedlocking; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.doublecheckedlocking.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /double-dispatch/etc/double-dispatch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/double-dispatch/etc/double-dispatch.png -------------------------------------------------------------------------------- /double-dispatch/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | double-dispatch 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /double-dispatch/src/main/java/com/iluwatar/doubledispatch/FlamingAsteroid.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doubledispatch; 2 | 3 | /** 4 | * 5 | * Flaming asteroid game object 6 | * 7 | */ 8 | public class FlamingAsteroid extends Meteoroid { 9 | 10 | public FlamingAsteroid(int left, int top, int right, int bottom) { 11 | super(left, top, right, bottom); 12 | setOnFire(true); 13 | } 14 | 15 | @Override 16 | public void collision(GameObject gameObject) { 17 | gameObject.collisionResolve(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /double-dispatch/src/main/java/com/iluwatar/doubledispatch/SpaceStationIss.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doubledispatch; 2 | 3 | /** 4 | * 5 | * Space station ISS game object 6 | * 7 | */ 8 | public class SpaceStationIss extends SpaceStationMir { 9 | 10 | public SpaceStationIss(int left, int top, int right, int bottom) { 11 | super(left, top, right, bottom); 12 | } 13 | 14 | @Override 15 | public void collision(GameObject gameObject) { 16 | gameObject.collisionResolve(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /double-dispatch/src/test/java/com/iluwatar/doubledispatch/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doubledispatch; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.doubledispatch.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /double-dispatch/src/test/java/com/iluwatar/doubledispatch/RectangleTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.doubledispatch; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import com.iluwatar.doubledispatch.Rectangle; 7 | 8 | /** 9 | * 10 | * Unit test for Rectangle 11 | * 12 | */ 13 | public class RectangleTest { 14 | 15 | @Test 16 | public void test() { 17 | Assert.assertTrue(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(0,0,1,1))); 18 | Assert.assertTrue(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(-1,-5,7,8))); 19 | Assert.assertFalse(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(2,2,3,3))); 20 | Assert.assertFalse(new Rectangle(0,0,1,1).intersectsWith(new Rectangle(-2,-2,-1,-1))); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /event-aggregator/etc/classes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/event-aggregator/etc/classes.png -------------------------------------------------------------------------------- /event-aggregator/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.iluwatar 6 | java-design-patterns 7 | 1.1.0 8 | 9 | event-aggregator 10 | 11 | 12 | junit 13 | junit 14 | test 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/Event.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * Event enumeration. 6 | * 7 | */ 8 | public enum Event { 9 | 10 | STARK_SIGHTED("Stark sighted"), WARSHIPS_APPROACHING("Warships approaching"), TRAITOR_DETECTED("Traitor detected"); 11 | 12 | private String description; 13 | 14 | Event(String description) { 15 | this.description = description; 16 | } 17 | 18 | public String toString() { 19 | return description; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/EventObserver.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * Observers of events implement this interface. 6 | * 7 | */ 8 | public interface EventObserver { 9 | 10 | void onEvent(Event e); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/KingJoffrey.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * KingJoffrey observes events from KingsHand. 6 | * 7 | */ 8 | public class KingJoffrey implements EventObserver { 9 | 10 | @Override 11 | public void onEvent(Event e) { 12 | System.out.println("Received event from the King's Hand: " + e.toString()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/KingsHand.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * KingsHand observes events from multiple sources and delivers them 6 | * to listeners. 7 | * 8 | */ 9 | public class KingsHand extends EventEmitter implements EventObserver { 10 | 11 | public KingsHand() { 12 | super(); 13 | } 14 | 15 | public KingsHand(EventObserver obs) { 16 | super(obs); 17 | } 18 | 19 | @Override 20 | public void onEvent(Event e) { 21 | notifyObservers(e); 22 | } 23 | 24 | @Override 25 | public void timePasses(Weekday day) { 26 | // NOP 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/LordBaelish.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * LordBaelish produces events. 6 | * 7 | */ 8 | public class LordBaelish extends EventEmitter { 9 | 10 | public LordBaelish() { 11 | super(); 12 | } 13 | 14 | public LordBaelish(EventObserver obs) { 15 | super(obs); 16 | } 17 | 18 | @Override 19 | public void timePasses(Weekday day) { 20 | if (day.equals(Weekday.FRIDAY)) { 21 | notifyObservers(Event.STARK_SIGHTED); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/LordVarys.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * LordVarys produces events. 6 | * 7 | */ 8 | public class LordVarys extends EventEmitter { 9 | 10 | public LordVarys() { 11 | super(); 12 | } 13 | 14 | public LordVarys(EventObserver obs) { 15 | super(obs); 16 | } 17 | 18 | @Override 19 | public void timePasses(Weekday day) { 20 | if (day.equals(Weekday.SATURDAY)) { 21 | notifyObservers(Event.TRAITOR_DETECTED); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/Scout.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | /** 4 | * 5 | * Scout produces events. 6 | * 7 | */ 8 | public class Scout extends EventEmitter { 9 | 10 | public Scout() { 11 | super(); 12 | } 13 | 14 | public Scout(EventObserver obs) { 15 | super(obs); 16 | } 17 | 18 | @Override 19 | public void timePasses(Weekday day) { 20 | if (day.equals(Weekday.TUESDAY)) { 21 | notifyObservers(Event.WARSHIPS_APPROACHING); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /event-aggregator/src/main/java/com/iluwatar/eventaggregator/Weekday.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | 3 | public enum Weekday { 4 | 5 | MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); 6 | 7 | private String description; 8 | 9 | Weekday(String description) { 10 | this.description = description; 11 | } 12 | 13 | public String toString() { 14 | return description; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /event-aggregator/src/test/java/com/iluwatar/eventaggregator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.eventaggregator; 2 | import org.junit.Test; 3 | 4 | import com.iluwatar.eventaggregator.App; 5 | 6 | public class AppTest { 7 | 8 | @Test 9 | public void test() { 10 | String[] args = {}; 11 | App.main(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /execute-around/etc/execute-around.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/execute-around/etc/execute-around.png -------------------------------------------------------------------------------- /execute-around/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | execute-around 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /execute-around/src/main/java/com/iluwatar/executearound/FileWriterAction.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.executearound; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | 6 | /** 7 | * 8 | * Interface for specifying what to do with the file resource. 9 | * 10 | */ 11 | public interface FileWriterAction { 12 | 13 | void writeFile(FileWriter writer) throws IOException; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /execute-around/src/main/java/com/iluwatar/executearound/SimpleFileWriter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.executearound; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | 6 | /** 7 | * 8 | * SimpleFileWriter handles opening and closing file for the user. The user 9 | * only has to specify what to do with the file resource through FileWriterAction 10 | * parameter. 11 | * 12 | */ 13 | public class SimpleFileWriter { 14 | 15 | public SimpleFileWriter(String filename, FileWriterAction action) throws IOException { 16 | FileWriter writer = new FileWriter(filename); 17 | try { 18 | action.writeFile(writer); 19 | } finally { 20 | writer.close(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /execute-around/src/test/java/com/iluwatar/executearound/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.executearound; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import org.junit.After; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import com.iluwatar.executearound.App; 11 | 12 | /** 13 | * 14 | * Tests execute-around example. 15 | * 16 | */ 17 | public class AppTest { 18 | 19 | @Test 20 | public void test() throws IOException { 21 | String[] args = {}; 22 | App.main(args); 23 | } 24 | 25 | @Before 26 | @After 27 | public void cleanup() { 28 | File file = new File("testfile.txt"); 29 | file.delete(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /facade/etc/facade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/facade/etc/facade.png -------------------------------------------------------------------------------- /facade/etc/facade_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/facade/etc/facade_1.png -------------------------------------------------------------------------------- /facade/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | facade 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /facade/src/main/java/com/iluwatar/facade/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.facade; 2 | 3 | /** 4 | * 5 | * Facade (DwarvenGoldmineFacade) provides simpler interface to subsystem. 6 | * http://en.wikipedia.org/wiki/Facade_pattern 7 | * 8 | */ 9 | public class App { 10 | 11 | public static void main(String[] args) { 12 | DwarvenGoldmineFacade facade = new DwarvenGoldmineFacade(); 13 | facade.startNewDay(); 14 | facade.digOutGold(); 15 | facade.endDay(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /facade/src/main/java/com/iluwatar/facade/DwarvenCartOperator.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.facade; 2 | 3 | /** 4 | * 5 | * DwarvenCartOperator is one of the goldmine subsystems. 6 | * 7 | */ 8 | public class DwarvenCartOperator extends DwarvenMineWorker { 9 | 10 | @Override 11 | public void work() { 12 | System.out.println(name() + " moves gold chunks out of the mine."); 13 | } 14 | 15 | @Override 16 | public String name() { 17 | return "Dwarf cart operator"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /facade/src/main/java/com/iluwatar/facade/DwarvenGoldDigger.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.facade; 2 | 3 | /** 4 | * 5 | * DwarvenGoldDigger is one of the goldmine subsystems. 6 | * 7 | */ 8 | public class DwarvenGoldDigger extends DwarvenMineWorker { 9 | 10 | @Override 11 | public void work() { 12 | System.out.println(name() + " digs for gold."); 13 | } 14 | 15 | @Override 16 | public String name() { 17 | return "Dwarf gold digger"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /facade/src/main/java/com/iluwatar/facade/DwarvenTunnelDigger.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.facade; 2 | 3 | /** 4 | * 5 | * DwarvenTunnelDigger is one of the goldmine subsystems. 6 | * 7 | */ 8 | public class DwarvenTunnelDigger extends DwarvenMineWorker { 9 | 10 | @Override 11 | public void work() { 12 | System.out.println(name() + " creates another promising tunnel."); 13 | } 14 | 15 | @Override 16 | public String name() { 17 | return "Dwarven tunnel digger"; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /facade/src/test/java/com/iluwatar/facade/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.facade; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.facade.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /factory-method/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /factory-method/etc/factory-method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/factory-method/etc/factory-method.png -------------------------------------------------------------------------------- /factory-method/etc/factory-method_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/factory-method/etc/factory-method_1.png -------------------------------------------------------------------------------- /factory-method/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | factory-method 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/Blacksmith.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | /** 4 | * 5 | * The interface containing method for producing objects. 6 | * 7 | */ 8 | public interface Blacksmith { 9 | 10 | Weapon manufactureWeapon(WeaponType weaponType); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/ElfBlacksmith.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | /** 4 | * 5 | * Concrete subclass for creating new objects. 6 | * 7 | */ 8 | public class ElfBlacksmith implements Blacksmith { 9 | 10 | public Weapon manufactureWeapon(WeaponType weaponType) { 11 | return new ElfWeapon(weaponType); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/ElfWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | public class ElfWeapon implements Weapon { 4 | 5 | private WeaponType weaponType; 6 | 7 | public ElfWeapon(WeaponType weaponType) { 8 | this.weaponType = weaponType; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "Elven " + weaponType; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/OrcBlacksmith.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | /** 4 | * 5 | * Concrete subclass for creating new objects. 6 | * 7 | */ 8 | public class OrcBlacksmith implements Blacksmith { 9 | 10 | public Weapon manufactureWeapon(WeaponType weaponType) { 11 | return new OrcWeapon(weaponType); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/OrcWeapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | public class OrcWeapon implements Weapon { 4 | 5 | private WeaponType weaponType; 6 | 7 | public OrcWeapon(WeaponType weaponType) { 8 | this.weaponType = weaponType; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return "Orcish " + weaponType; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/Weapon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | public interface Weapon { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /factory-method/src/main/java/com/iluwatar/factorymethod/WeaponType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | public enum WeaponType { 4 | 5 | SHORT_SWORD("short sword"), SPEAR("spear"), AXE("axe"), UNDEFINED(""); 6 | 7 | private String title; 8 | 9 | WeaponType(String title) { 10 | this.title = title; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return title; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /factory-method/src/test/java/com/iluwatar/factorymethod/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.factorymethod; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.factorymethod.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /flux/etc/flux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/flux/etc/flux.png -------------------------------------------------------------------------------- /flux/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | flux 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/Action.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | /** 4 | * 5 | * Action is the data payload dispatched to the stores when something happens. 6 | * 7 | */ 8 | public abstract class Action { 9 | 10 | private ActionType type; 11 | 12 | public Action(ActionType type) { 13 | this.type = type; 14 | } 15 | 16 | public ActionType getType() { 17 | return type; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/ActionType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | /** 4 | * 5 | * Types of actions. 6 | * 7 | */ 8 | public enum ActionType { 9 | 10 | MENU_ITEM_SELECTED, CONTENT_CHANGED; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/Content.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | /** 4 | * 5 | * Content items. 6 | * 7 | */ 8 | public enum Content { 9 | 10 | PRODUCTS("Products - This page lists the company's products."), COMPANY("Company - This page displays information about the company."); 11 | 12 | private String title; 13 | 14 | private Content(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/ContentAction.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | /** 4 | * 5 | * ContentAction is a concrete action. 6 | * 7 | */ 8 | public class ContentAction extends Action { 9 | 10 | private Content content; 11 | 12 | public ContentAction(Content content) { 13 | super(ActionType.CONTENT_CHANGED); 14 | this.content = content; 15 | } 16 | 17 | public Content getContent() { 18 | return content; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/MenuAction.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | 4 | /** 5 | * 6 | * MenuAction is a concrete action. 7 | * 8 | */ 9 | public class MenuAction extends Action { 10 | 11 | private MenuItem menuItem; 12 | 13 | public MenuAction(MenuItem menuItem) { 14 | super(ActionType.MENU_ITEM_SELECTED); 15 | this.menuItem = menuItem; 16 | } 17 | 18 | public MenuItem getMenuItem() { 19 | return menuItem; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/action/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.action; 2 | 3 | /** 4 | * 5 | * Menu items. 6 | * 7 | */ 8 | public enum MenuItem { 9 | 10 | HOME("Home"), PRODUCTS("Products"), COMPANY("Company"); 11 | 12 | private String title; 13 | 14 | MenuItem(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/store/ContentStore.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.store; 2 | 3 | import com.iluwatar.flux.action.Action; 4 | import com.iluwatar.flux.action.ActionType; 5 | import com.iluwatar.flux.action.Content; 6 | import com.iluwatar.flux.action.ContentAction; 7 | 8 | /** 9 | * 10 | * ContentStore is a concrete store. 11 | * 12 | */ 13 | public class ContentStore extends Store { 14 | 15 | private Content content = Content.PRODUCTS; 16 | 17 | @Override 18 | public void onAction(Action action) { 19 | if (action.getType().equals(ActionType.CONTENT_CHANGED)) { 20 | ContentAction contentAction = (ContentAction) action; 21 | content = contentAction.getContent(); 22 | notifyChange(); 23 | } 24 | } 25 | 26 | public Content getContent() { 27 | return content; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/store/MenuStore.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.store; 2 | 3 | import com.iluwatar.flux.action.Action; 4 | import com.iluwatar.flux.action.ActionType; 5 | import com.iluwatar.flux.action.MenuAction; 6 | import com.iluwatar.flux.action.MenuItem; 7 | 8 | /** 9 | * 10 | * MenuStore is a concrete store. 11 | * 12 | */ 13 | public class MenuStore extends Store { 14 | 15 | private MenuItem selected = MenuItem.HOME; 16 | 17 | @Override 18 | public void onAction(Action action) { 19 | if (action.getType().equals(ActionType.MENU_ITEM_SELECTED)) { 20 | MenuAction menuAction = (MenuAction) action; 21 | selected = menuAction.getMenuItem(); 22 | notifyChange(); 23 | } 24 | } 25 | 26 | public MenuItem getSelected() { 27 | return selected; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/store/Store.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.store; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | import com.iluwatar.flux.action.Action; 7 | import com.iluwatar.flux.view.View; 8 | 9 | /** 10 | * 11 | * Store is a data model. 12 | * 13 | */ 14 | public abstract class Store { 15 | 16 | private List views = new LinkedList<>(); 17 | 18 | public abstract void onAction(Action action); 19 | 20 | public void registerView(View view) { 21 | views.add(view); 22 | } 23 | 24 | protected void notifyChange() { 25 | views.stream().forEach((view) -> view.storeChanged(this)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/view/ContentView.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.view; 2 | 3 | import com.iluwatar.flux.action.Content; 4 | import com.iluwatar.flux.store.ContentStore; 5 | import com.iluwatar.flux.store.Store; 6 | 7 | /** 8 | * 9 | * ContentView is a concrete view. 10 | * 11 | */ 12 | public class ContentView implements View { 13 | 14 | private Content content = Content.PRODUCTS; 15 | 16 | @Override 17 | public void storeChanged(Store store) { 18 | ContentStore contentStore = (ContentStore) store; 19 | content = contentStore.getContent(); 20 | render(); 21 | } 22 | 23 | @Override 24 | public void render() { 25 | System.out.println(content.toString()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /flux/src/main/java/com/iluwatar/flux/view/View.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.view; 2 | 3 | import com.iluwatar.flux.store.Store; 4 | 5 | /** 6 | * 7 | * Views define the representation of data. 8 | * 9 | */ 10 | public interface View { 11 | 12 | public void storeChanged(Store store); 13 | 14 | public void render(); 15 | } 16 | -------------------------------------------------------------------------------- /flux/src/test/java/com/iluwatar/flux/app/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flux.app; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.flux.app.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /flyweight/etc/flyweight.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/flyweight/etc/flyweight.png -------------------------------------------------------------------------------- /flyweight/etc/flyweight_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/flyweight/etc/flyweight_1.png -------------------------------------------------------------------------------- /flyweight/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | flyweight 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/HealingPotion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | public class HealingPotion implements Potion { 4 | 5 | @Override 6 | public void drink() { 7 | System.out.println("You feel healed. (Potion=" 8 | + System.identityHashCode(this) + ")"); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/HolyWaterPotion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | public class HolyWaterPotion implements Potion { 4 | 5 | @Override 6 | public void drink() { 7 | System.out.println("You feel blessed. (Potion=" 8 | + System.identityHashCode(this) + ")"); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/InvisibilityPotion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | public class InvisibilityPotion implements Potion { 4 | 5 | @Override 6 | public void drink() { 7 | System.out.println("You become invisible. (Potion=" 8 | + System.identityHashCode(this) + ")"); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/PoisonPotion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | public class PoisonPotion implements Potion { 4 | 5 | @Override 6 | public void drink() { 7 | System.out.println("Urgh! This is poisonous. (Potion=" 8 | + System.identityHashCode(this) + ")"); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/Potion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | /** 4 | * 5 | * Interface for Potions. 6 | * 7 | */ 8 | public interface Potion { 9 | 10 | void drink(); 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/PotionType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | /** 4 | * 5 | * Enumeration for potion types. 6 | * 7 | */ 8 | public enum PotionType { 9 | 10 | HEALING, INVISIBILITY, STRENGTH, HOLY_WATER, POISON 11 | } 12 | -------------------------------------------------------------------------------- /flyweight/src/main/java/com/iluwatar/flyweight/StrengthPotion.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | public class StrengthPotion implements Potion { 4 | 5 | @Override 6 | public void drink() { 7 | System.out.println("You feel strong. (Potion=" 8 | + System.identityHashCode(this) + ")"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /flyweight/src/test/java/com/iluwatar/flyweight/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.flyweight; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.flyweight.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /intercepting-filter/etc/intercepting-filter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/intercepting-filter/etc/intercepting-filter.png -------------------------------------------------------------------------------- /intercepting-filter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | intercepting-filter 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/AddressFilter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * Concrete implementation of filter 5 | * This filter is responsible for checking/filtering the input in the address field. 6 | * @author joshzambales 7 | * 8 | */ 9 | public class AddressFilter extends AbstractFilter { 10 | 11 | @Override 12 | public String execute(Order order) { 13 | String result = super.execute(order); 14 | if (order.getAddress() == null || order.getAddress().isEmpty()) { 15 | return result + "Invalid address! "; 16 | } else 17 | return result; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * 5 | * This is an app that checks whether the order request is valid through pre-processing done via Filters 6 | * Each field has its own corresponding Filter 7 | * @author joshzambales 8 | * 9 | */ 10 | public class App{ 11 | 12 | public static void main(String[] args) { 13 | FilterManager filterManager = new FilterManager(new Target()); 14 | filterManager.addFilter(new NameFilter()); 15 | filterManager.addFilter(new ContactFilter()); 16 | filterManager.addFilter(new AddressFilter()); 17 | filterManager.addFilter(new DepositFilter()); 18 | filterManager.addFilter(new OrderFilter()); 19 | 20 | Client client = new Client(); 21 | client.setFilterManager(filterManager); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/DepositFilter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * Concrete implementation of filter 5 | * This checks for the deposit code 6 | * @author joshzambales 7 | * 8 | */ 9 | public class DepositFilter extends AbstractFilter { 10 | 11 | @Override 12 | public String execute(Order order) { 13 | String result = super.execute(order); 14 | if (order.getDepositNumber() == null || order.getDepositNumber().isEmpty()) { 15 | return result + "Invalid deposit number! "; 16 | } else { 17 | return result; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/FilterChain.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | 4 | /** 5 | * Filter Chain carries multiple filters and help to execute them in defined order on target. 6 | * 7 | * @author joshzambales 8 | */ 9 | public class FilterChain { 10 | 11 | private Filter chain; 12 | 13 | private final Target target; 14 | 15 | public FilterChain(Target target) { 16 | this.target = target; 17 | } 18 | 19 | public void addFilter(Filter filter) { 20 | if (chain == null) { 21 | chain = filter; 22 | } else { 23 | chain.getLast().setNext(filter); 24 | } 25 | } 26 | 27 | public String execute(Order order) { 28 | if (chain != null) { 29 | return chain.execute(order); 30 | } else { 31 | return "RUNNING..."; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/FilterManager.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * Filter Manager manages the filters and Filter Chain. 5 | * 6 | * @author joshzambales 7 | * 8 | */ 9 | public class FilterManager { 10 | 11 | private FilterChain filterChain; 12 | 13 | public FilterManager(Target target) { 14 | filterChain = new FilterChain(target); 15 | } 16 | 17 | public void addFilter(Filter filter) { 18 | filterChain.addFilter(filter); 19 | } 20 | 21 | public String filterRequest(Order order) { 22 | return filterChain.execute(order); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/NameFilter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * Concrete implementation of filter This filter checks if the input in the Name 5 | * field is valid. (alphanumeric) 6 | * 7 | * @author joshzambales 8 | * 9 | */ 10 | public class NameFilter extends AbstractFilter { 11 | 12 | @Override 13 | public String execute(Order order) { 14 | String result = super.execute(order); 15 | if (order.getName() == null || order.getName().isEmpty() || order.getName().matches(".*[^\\w|\\s]+.*")) { 16 | return result + "Invalid order! "; 17 | } else { 18 | return result; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /intercepting-filter/src/main/java/com/iluwatar/interceptingfilter/OrderFilter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interceptingfilter; 2 | 3 | /** 4 | * Concrete implementation of filter This checks for the order field 5 | * 6 | * @author joshzambales 7 | * 8 | */ 9 | public class OrderFilter extends AbstractFilter { 10 | 11 | @Override 12 | public String execute(Order order) { 13 | String result = super.execute(order); 14 | if (order.getOrder() == null || order.getOrder().isEmpty()) { 15 | return result + "Invalid order! "; 16 | } else { 17 | return result; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /interpreter/etc/interpreter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/interpreter/etc/interpreter.png -------------------------------------------------------------------------------- /interpreter/etc/interpreter_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/interpreter/etc/interpreter_1.png -------------------------------------------------------------------------------- /interpreter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | interpreter 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /interpreter/src/main/java/com/iluwatar/interpreter/Expression.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | public abstract class Expression { 4 | 5 | public abstract int interpret(); 6 | 7 | @Override 8 | public abstract String toString(); 9 | } 10 | -------------------------------------------------------------------------------- /interpreter/src/main/java/com/iluwatar/interpreter/MinusExpression.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | public class MinusExpression extends Expression { 4 | 5 | private Expression leftExpression; 6 | private Expression rightExpression; 7 | 8 | public MinusExpression(Expression leftExpression, Expression rightExpression) { 9 | this.leftExpression = leftExpression; 10 | this.rightExpression = rightExpression; 11 | } 12 | 13 | @Override 14 | public int interpret() { 15 | return leftExpression.interpret() - rightExpression.interpret(); 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "-"; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /interpreter/src/main/java/com/iluwatar/interpreter/MultiplyExpression.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | public class MultiplyExpression extends Expression { 4 | 5 | private Expression leftExpression; 6 | private Expression rightExpression; 7 | 8 | public MultiplyExpression(Expression leftExpression, 9 | Expression rightExpression) { 10 | this.leftExpression = leftExpression; 11 | this.rightExpression = rightExpression; 12 | } 13 | 14 | @Override 15 | public int interpret() { 16 | return leftExpression.interpret() * rightExpression.interpret(); 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "*"; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /interpreter/src/main/java/com/iluwatar/interpreter/NumberExpression.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | public class NumberExpression extends Expression { 4 | 5 | private int number; 6 | 7 | public NumberExpression(int number) { 8 | this.number = number; 9 | } 10 | 11 | public NumberExpression(String s) { 12 | this.number = Integer.parseInt(s); 13 | } 14 | 15 | @Override 16 | public int interpret() { 17 | return number; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "number"; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /interpreter/src/main/java/com/iluwatar/interpreter/PlusExpression.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | public class PlusExpression extends Expression { 4 | 5 | private Expression leftExpression; 6 | private Expression rightExpression; 7 | 8 | public PlusExpression(Expression leftExpression, Expression rightExpression) { 9 | this.leftExpression = leftExpression; 10 | this.rightExpression = rightExpression; 11 | } 12 | 13 | @Override 14 | public int interpret() { 15 | return leftExpression.interpret() + rightExpression.interpret(); 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return "+"; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /interpreter/src/test/java/com/iluwatar/interpreter/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.interpreter; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.interpreter.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /iterator/etc/iterator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/iterator/etc/iterator.png -------------------------------------------------------------------------------- /iterator/etc/iterator_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/iterator/etc/iterator_1.png -------------------------------------------------------------------------------- /iterator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | iterator 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /iterator/src/main/java/com/iluwatar/iterator/Item.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.iterator; 2 | 3 | public class Item { 4 | 5 | private ItemType type; 6 | private String name; 7 | 8 | public Item(ItemType type, String name) { 9 | this.setType(type); 10 | this.name = name; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return name; 16 | } 17 | 18 | public ItemType getType() { 19 | return type; 20 | } 21 | 22 | public void setType(ItemType type) { 23 | this.type = type; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /iterator/src/main/java/com/iluwatar/iterator/ItemIterator.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.iterator; 2 | 3 | /** 4 | * 5 | * Iterator interface. 6 | * 7 | */ 8 | public interface ItemIterator { 9 | 10 | boolean hasNext(); 11 | 12 | Item next(); 13 | } 14 | -------------------------------------------------------------------------------- /iterator/src/main/java/com/iluwatar/iterator/ItemType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.iterator; 2 | 3 | public enum ItemType { 4 | 5 | ANY, WEAPON, RING, POTION 6 | 7 | } 8 | -------------------------------------------------------------------------------- /iterator/src/test/java/com/iluwatar/iterator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.iterator; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.iterator.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /lazy-loading/etc/lazy-loading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/lazy-loading/etc/lazy-loading.png -------------------------------------------------------------------------------- /lazy-loading/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | lazy-loading 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /lazy-loading/src/main/java/com/iluwatar/lazyloading/Heavy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.lazyloading; 2 | 3 | /** 4 | * 5 | * Heavy objects are expensive to create. 6 | * 7 | */ 8 | public class Heavy { 9 | 10 | public Heavy() { 11 | System.out.println("Creating Heavy ..."); 12 | try { 13 | Thread.sleep(1000); 14 | } catch (InterruptedException e) { 15 | e.printStackTrace(); 16 | } 17 | System.out.println("... Heavy created"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /lazy-loading/src/main/java/com/iluwatar/lazyloading/HolderNaive.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.lazyloading; 2 | 3 | /** 4 | * 5 | * Simple implementation of the lazy loading idiom. 6 | * However, this is not thread safe. 7 | * 8 | */ 9 | public class HolderNaive { 10 | 11 | private Heavy heavy; 12 | 13 | public HolderNaive() { 14 | System.out.println("HolderNaive created"); 15 | } 16 | 17 | public Heavy getHeavy() { 18 | if (heavy == null) { 19 | heavy = new Heavy(); 20 | } 21 | return heavy; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /lazy-loading/src/main/java/com/iluwatar/lazyloading/HolderThreadSafe.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.lazyloading; 2 | 3 | /** 4 | * 5 | * Same as HolderNaive but with added synchronization. 6 | * This implementation is thread safe, but each {@link #getHeavy()} 7 | * call costs additional synchronization overhead. 8 | * 9 | */ 10 | public class HolderThreadSafe { 11 | 12 | private Heavy heavy; 13 | 14 | public HolderThreadSafe() { 15 | System.out.println("HolderThreadSafe created"); 16 | } 17 | 18 | public synchronized Heavy getHeavy() { 19 | if (heavy == null) { 20 | heavy = new Heavy(); 21 | } 22 | return heavy; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /lazy-loading/src/test/java/com/iluwatar/lazyloading/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.lazyloading; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.lazyloading.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /mediator/etc/mediator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/mediator/etc/mediator.png -------------------------------------------------------------------------------- /mediator/etc/mediator_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/mediator/etc/mediator_1.png -------------------------------------------------------------------------------- /mediator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | mediator 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Action.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Action enumeration. 6 | * 7 | */ 8 | public enum Action { 9 | 10 | HUNT("hunted a rabbit", "arrives for dinner"), 11 | TALE("tells a tale", "comes to listen"), 12 | GOLD("found gold", "takes his share of the gold"), 13 | ENEMY("spotted enemies", "runs for cover"), 14 | NONE("", ""); 15 | 16 | private String title; 17 | private String description; 18 | 19 | Action(String title, String description) { 20 | this.title = title; 21 | this.description = description; 22 | } 23 | 24 | public String getDescription() { 25 | return description; 26 | } 27 | 28 | public String toString() { 29 | return title; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Hobbit.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Hobbit party member. 6 | * 7 | */ 8 | public class Hobbit extends PartyMemberBase { 9 | 10 | @Override 11 | public String toString() { 12 | return "Hobbit"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Hunter.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Hunter party member. 6 | * 7 | */ 8 | public class Hunter extends PartyMemberBase { 9 | 10 | @Override 11 | public String toString() { 12 | return "Hunter"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Party.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Party interface. 6 | * 7 | */ 8 | public interface Party { 9 | 10 | void addMember(PartyMember member); 11 | 12 | void act(PartyMember actor, Action action); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/PartyImpl.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * Party implementation. 9 | * 10 | */ 11 | public class PartyImpl implements Party { 12 | 13 | private final List members; 14 | 15 | public PartyImpl() { 16 | members = new ArrayList<>(); 17 | } 18 | 19 | @Override 20 | public void act(PartyMember actor, Action action) { 21 | for (PartyMember member : members) { 22 | if (member != actor) { 23 | member.partyAction(action); 24 | } 25 | } 26 | } 27 | 28 | @Override 29 | public void addMember(PartyMember member) { 30 | members.add(member); 31 | member.joinedParty(this); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/PartyMember.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Interface for party members interacting with Party. 6 | * 7 | */ 8 | public interface PartyMember { 9 | 10 | void joinedParty(Party party); 11 | 12 | void partyAction(Action action); 13 | 14 | void act(Action action); 15 | } 16 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Rogue.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Rogue party member. 6 | * 7 | */ 8 | public class Rogue extends PartyMemberBase { 9 | 10 | @Override 11 | public String toString() { 12 | return "Rogue"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mediator/src/main/java/com/iluwatar/mediator/Wizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | /** 4 | * 5 | * Wizard party member. 6 | * 7 | */ 8 | public class Wizard extends PartyMemberBase { 9 | 10 | @Override 11 | public String toString() { 12 | return "Wizard"; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mediator/src/test/java/com/iluwatar/mediator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.mediator; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.mediator.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /memento/etc/memento.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/memento/etc/memento.png -------------------------------------------------------------------------------- /memento/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | memento 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /memento/src/main/java/com/iluwatar/memento/StarMemento.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.memento; 2 | 3 | /** 4 | * 5 | * External interface to memento. 6 | * 7 | */ 8 | public interface StarMemento { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /memento/src/main/java/com/iluwatar/memento/StarType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.memento; 2 | 3 | public enum StarType { 4 | 5 | SUN("sun"), RED_GIANT("red giant"), WHITE_DWARF("white dwarf"), SUPERNOVA("supernova"), DEAD("dead star"), UNDEFINED(""); 6 | 7 | private String title; 8 | 9 | StarType(String title) { 10 | this.title = title; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return title; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /memento/src/test/java/com/iluwatar/memento/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.memento; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.memento.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /model-view-controller/etc/model-view-controller.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/model-view-controller/etc/model-view-controller.png -------------------------------------------------------------------------------- /model-view-controller/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | model-view-controller 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /model-view-controller/src/main/java/com/iluwatar/modelviewcontroller/Fatigue.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.modelviewcontroller; 2 | 3 | /** 4 | * 5 | * Fatigue enumeration 6 | * 7 | */ 8 | public enum Fatigue { 9 | 10 | ALERT("alert"), TIRED("tired"), SLEEPING("sleeping"); 11 | 12 | private String title; 13 | 14 | Fatigue(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /model-view-controller/src/main/java/com/iluwatar/modelviewcontroller/GiantView.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.modelviewcontroller; 2 | 3 | /** 4 | * 5 | * GiantView displays the giant 6 | * 7 | */ 8 | public class GiantView { 9 | 10 | public void displayGiant(GiantModel giant) { 11 | System.out.println(giant); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /model-view-controller/src/main/java/com/iluwatar/modelviewcontroller/Health.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.modelviewcontroller; 2 | 3 | /** 4 | * 5 | * Health enumeration 6 | * 7 | */ 8 | public enum Health { 9 | 10 | HEALTHY("healthy"), WOUNDED("wounded"), DEAD("dead"); 11 | 12 | private String title; 13 | 14 | Health(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /model-view-controller/src/main/java/com/iluwatar/modelviewcontroller/Nourishment.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.modelviewcontroller; 2 | 3 | /** 4 | * 5 | * Nourishment enumeration 6 | * 7 | */ 8 | public enum Nourishment { 9 | 10 | SATURATED("saturated"), HUNGRY("hungry"), STARVING("starving"); 11 | 12 | private String title; 13 | 14 | Nourishment(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /model-view-controller/src/test/java/com/iluwatar/modelviewcontroller/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.modelviewcontroller; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.modelviewcontroller.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /model-view-presenter/etc/data/test.txt: -------------------------------------------------------------------------------- 1 | Test line 1 2 | Test line 2 -------------------------------------------------------------------------------- /model-view-presenter/etc/model-view-presenter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/model-view-presenter/etc/model-view-presenter.png -------------------------------------------------------------------------------- /model-view-presenter/etc/model-view-presenter_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/model-view-presenter/etc/model-view-presenter_1.png -------------------------------------------------------------------------------- /multiton/etc/multiton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/multiton/etc/multiton.png -------------------------------------------------------------------------------- /multiton/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | multiton 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /multiton/src/main/java/com/iluwatar/multiton/NazgulName.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.multiton; 2 | 3 | /** 4 | * 5 | * Each Nazgul has different NazgulName. 6 | * 7 | */ 8 | public enum NazgulName { 9 | 10 | KHAMUL, MURAZOR, DWAR, JI_INDUR, AKHORAHIL, HOARMURATH, ADUNAPHEL, REN, UVATHA; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /multiton/src/test/java/com/iluwatar/multiton/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.multiton; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.multiton.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /null-object/etc/null-object.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/null-object/etc/null-object.png -------------------------------------------------------------------------------- /null-object/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | null-object 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /null-object/src/main/java/com/iluwatar/nullobject/Node.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.nullobject; 2 | 3 | /** 4 | * 5 | * Interface for binary tree node. 6 | * 7 | */ 8 | public interface Node { 9 | 10 | String getName(); 11 | int getTreeSize(); 12 | Node getLeft(); 13 | Node getRight(); 14 | void walk(); 15 | } 16 | -------------------------------------------------------------------------------- /null-object/src/test/java/com/iluwatar/nullobject/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.nullobject; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.nullobject.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /object-pool/etc/object-pool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/object-pool/etc/object-pool.png -------------------------------------------------------------------------------- /object-pool/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | object-pool 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /object-pool/src/main/java/com/iluwatar/objectpool/Oliphaunt.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.objectpool; 2 | 3 | /** 4 | * 5 | * Oliphaunts are expensive to create 6 | * 7 | */ 8 | public class Oliphaunt { 9 | 10 | private static int counter = 1; 11 | 12 | private final int id; 13 | 14 | public Oliphaunt() { 15 | id = counter++; 16 | try { 17 | Thread.sleep(1000); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | 23 | public int getId() { 24 | return id; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return String.format("Oliphaunt id=%d", id); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /object-pool/src/main/java/com/iluwatar/objectpool/OliphauntPool.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.objectpool; 2 | 3 | /** 4 | * 5 | * Oliphaunt object pool 6 | * 7 | */ 8 | public class OliphauntPool extends ObjectPool { 9 | 10 | @Override 11 | protected Oliphaunt create() { 12 | return new Oliphaunt(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /object-pool/src/test/java/com/iluwatar/objectpool/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.objectpool; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.objectpool.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /observer/etc/observer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/observer/etc/observer.png -------------------------------------------------------------------------------- /observer/etc/observer_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/observer/etc/observer_1.png -------------------------------------------------------------------------------- /observer/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | observer 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/Hobbits.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer; 2 | 3 | public class Hobbits implements WeatherObserver { 4 | 5 | @Override 6 | public void update(WeatherType currentWeather) { 7 | switch (currentWeather) { 8 | case COLD: 9 | System.out.println("The hobbits are shivering in the cold weather."); 10 | break; 11 | case RAINY: 12 | System.out.println("The hobbits look for cover from the rain."); 13 | break; 14 | case SUNNY: 15 | System.out.println("The happy hobbits bade in the warm sun."); 16 | break; 17 | case WINDY: 18 | System.out.println("The hobbits hold their hats tightly in the windy weather."); 19 | break; 20 | default: 21 | break; 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/Orcs.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer; 2 | 3 | public class Orcs implements WeatherObserver { 4 | 5 | @Override 6 | public void update(WeatherType currentWeather) { 7 | switch (currentWeather) { 8 | case COLD: 9 | System.out.println("The orcs are freezing cold."); 10 | break; 11 | case RAINY: 12 | System.out.println("The orcs are dripping wet."); 13 | break; 14 | case SUNNY: 15 | System.out.println("The sun hurts the orcs' eyes."); 16 | break; 17 | case WINDY: 18 | System.out.println("The orc smell almost vanishes in the wind."); 19 | break; 20 | default: 21 | break; 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/WeatherObserver.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer; 2 | 3 | /** 4 | * 5 | * Observer interface. 6 | * 7 | */ 8 | public interface WeatherObserver { 9 | 10 | void update(WeatherType currentWeather); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/WeatherType.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer; 2 | 3 | public enum WeatherType { 4 | 5 | SUNNY, RAINY, WINDY, COLD; 6 | 7 | @Override 8 | public String toString() { 9 | return this.name().toLowerCase(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/generic/GWeather.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer.generic; 2 | 3 | import com.iluwatar.observer.WeatherType; 4 | 5 | public class GWeather extends Observable { 6 | 7 | private WeatherType currentWeather; 8 | 9 | public GWeather() { 10 | currentWeather = WeatherType.SUNNY; 11 | } 12 | 13 | public void timePasses() { 14 | WeatherType[] enumValues = WeatherType.values(); 15 | currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length]; 16 | System.out.println("The weather changed to " + currentWeather + "."); 17 | notifyObservers(currentWeather); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/generic/Observer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer.generic; 2 | 3 | public interface Observer, O extends Observer, A> { 4 | 5 | void update(S subject, A argument); 6 | } 7 | -------------------------------------------------------------------------------- /observer/src/main/java/com/iluwatar/observer/generic/Race.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer.generic; 2 | 3 | import com.iluwatar.observer.WeatherType; 4 | 5 | public interface Race extends Observer { 6 | } 7 | -------------------------------------------------------------------------------- /observer/src/test/java/com/iluwatar/observer/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.observer; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.observer.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /poison-pill/etc/poison-pill.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/poison-pill/etc/poison-pill.png -------------------------------------------------------------------------------- /poison-pill/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | poison-pill 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /poison-pill/src/main/java/com/iluwatar/poisonpill/MQPublishPoint.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.poisonpill; 2 | 3 | /** 4 | * Endpoint to publish {@link Message} to queue 5 | */ 6 | public interface MQPublishPoint { 7 | 8 | public void put(Message msg) throws InterruptedException; 9 | } 10 | -------------------------------------------------------------------------------- /poison-pill/src/main/java/com/iluwatar/poisonpill/MQSubscribePoint.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.poisonpill; 2 | 3 | /** 4 | * Endpoint to retrieve {@link Message} from queue 5 | */ 6 | public interface MQSubscribePoint { 7 | 8 | public Message take() throws InterruptedException; 9 | } 10 | -------------------------------------------------------------------------------- /poison-pill/src/main/java/com/iluwatar/poisonpill/MessageQueue.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.poisonpill; 2 | 3 | /** 4 | * Represents abstraction of channel (or pipe) that bounds {@link Producer} and {@link Consumer} 5 | */ 6 | public interface MessageQueue extends MQPublishPoint, MQSubscribePoint { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /poison-pill/src/main/java/com/iluwatar/poisonpill/SimpleMessageQueue.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.poisonpill; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | import java.util.concurrent.BlockingQueue; 5 | 6 | /** 7 | * Bounded blocking queue wrapper 8 | */ 9 | public class SimpleMessageQueue implements MessageQueue { 10 | 11 | private final BlockingQueue queue; 12 | 13 | public SimpleMessageQueue(int bound) { 14 | queue = new ArrayBlockingQueue(bound); 15 | } 16 | 17 | @Override 18 | public void put(Message msg) throws InterruptedException { 19 | queue.put(msg); 20 | } 21 | 22 | @Override 23 | public Message take() throws InterruptedException { 24 | return queue.take(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /poison-pill/src/test/java/com/iluwatar/poisonpill/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.poisonpill; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.poisonpill.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /private-class-data/etc/private-class-data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/private-class-data/etc/private-class-data.png -------------------------------------------------------------------------------- /private-class-data/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | private-class-data 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /private-class-data/src/main/java/com/iluwatar/privateclassdata/ImmutableStew.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.privateclassdata; 2 | 3 | /** 4 | * 5 | * Immutable stew class, protected with Private Class Data pattern 6 | * 7 | */ 8 | public class ImmutableStew { 9 | 10 | private StewData data; 11 | 12 | public ImmutableStew(int numPotatoes, int numCarrots, int numMeat, int numPeppers) { 13 | data = new StewData(numPotatoes, numCarrots, numMeat, numPeppers); 14 | } 15 | 16 | public void mix() { 17 | System.out.println(String.format("Mixing the immutable stew we find: %d potatoes, %d carrots, %d meat and %d peppers", 18 | data.getNumPotatoes(), data.getNumCarrots(), data.getNumMeat(), data.getNumPeppers())); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /private-class-data/src/test/java/com/iluwatar/privateclassdata/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.privateclassdata; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.privateclassdata.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /property/etc/property.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/property/etc/property.png -------------------------------------------------------------------------------- /property/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | property 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /property/src/main/java/com/iluwatar/property/Prototype.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.property; 2 | 3 | /** 4 | * Interface for prototype inheritance 5 | */ 6 | public interface Prototype { 7 | 8 | public Integer get(Stats stat); 9 | public boolean has(Stats stat); 10 | public void set(Stats stat, Integer val); 11 | public void remove(Stats stat); 12 | } 13 | -------------------------------------------------------------------------------- /property/src/main/java/com/iluwatar/property/Stats.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.property; 2 | 3 | /** 4 | * All possible attributes that Character can have 5 | */ 6 | public enum Stats { 7 | 8 | AGILITY, STRENGTH, ATTACK_POWER, ARMOR, INTELLECT, SPIRIT, ENERGY, RAGE 9 | } 10 | -------------------------------------------------------------------------------- /property/src/test/java/com/iluwatar/property/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.property; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.property.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /prototype/etc/prototype.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/prototype/etc/prototype.png -------------------------------------------------------------------------------- /prototype/etc/prototype_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/prototype/etc/prototype_1.png -------------------------------------------------------------------------------- /prototype/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | prototype 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/Beast.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public abstract class Beast extends Prototype { 4 | 5 | @Override 6 | public abstract Beast clone() throws CloneNotSupportedException; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class ElfBeast extends Beast { 4 | 5 | public ElfBeast() { 6 | } 7 | 8 | public ElfBeast(ElfBeast beast) { 9 | } 10 | 11 | @Override 12 | public Beast clone() throws CloneNotSupportedException { 13 | return new ElfBeast(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Elven eagle"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/ElfMage.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class ElfMage extends Mage { 4 | 5 | public ElfMage() { 6 | } 7 | 8 | public ElfMage(ElfMage mage) { 9 | } 10 | 11 | @Override 12 | public Mage clone() throws CloneNotSupportedException { 13 | return new ElfMage(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Elven mage"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class ElfWarlord extends Warlord { 4 | 5 | public ElfWarlord() { 6 | } 7 | 8 | public ElfWarlord(ElfWarlord warlord) { 9 | } 10 | 11 | @Override 12 | public Warlord clone() throws CloneNotSupportedException { 13 | return new ElfWarlord(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Elven warlord"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/HeroFactory.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | /** 4 | * 5 | * Interface for the factory class. 6 | * 7 | */ 8 | public interface HeroFactory { 9 | 10 | Mage createMage(); 11 | 12 | Warlord createWarlord(); 13 | 14 | Beast createBeast(); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/Mage.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public abstract class Mage extends Prototype { 4 | 5 | @Override 6 | public abstract Mage clone() throws CloneNotSupportedException; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class OrcBeast extends Beast { 4 | 5 | public OrcBeast() { 6 | } 7 | 8 | public OrcBeast(OrcBeast beast) { 9 | } 10 | 11 | @Override 12 | public Beast clone() throws CloneNotSupportedException { 13 | return new OrcBeast(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Orcish wolf"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/OrcMage.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class OrcMage extends Mage { 4 | 5 | public OrcMage() { 6 | } 7 | 8 | public OrcMage(OrcMage mage) { 9 | } 10 | 11 | @Override 12 | public Mage clone() throws CloneNotSupportedException { 13 | return new OrcMage(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Orcish mage"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public class OrcWarlord extends Warlord { 4 | 5 | public OrcWarlord() { 6 | } 7 | 8 | public OrcWarlord(OrcWarlord warlord) { 9 | } 10 | 11 | @Override 12 | public Warlord clone() throws CloneNotSupportedException { 13 | return new OrcWarlord(this); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Orcish warlord"; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/Prototype.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public abstract class Prototype implements Cloneable { 4 | 5 | @Override 6 | public abstract Object clone() throws CloneNotSupportedException; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /prototype/src/main/java/com/iluwatar/prototype/Warlord.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | public abstract class Warlord extends Prototype { 4 | 5 | @Override 6 | public abstract Warlord clone() throws CloneNotSupportedException; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /prototype/src/test/java/com/iluwatar/prototype/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.prototype; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.prototype.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /proxy/etc/proxy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/proxy/etc/proxy.png -------------------------------------------------------------------------------- /proxy/etc/proxy_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/proxy/etc/proxy_1.png -------------------------------------------------------------------------------- /proxy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | proxy 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /proxy/src/main/java/com/iluwatar/proxy/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.proxy; 2 | 3 | /** 4 | * 5 | * Proxy (WizardTowerProxy) controls access to the actual object (WizardTower). 6 | * 7 | */ 8 | public class App { 9 | 10 | public static void main(String[] args) { 11 | 12 | WizardTowerProxy tower = new WizardTowerProxy(); 13 | tower.enter(new Wizard("Red wizard")); 14 | tower.enter(new Wizard("White wizard")); 15 | tower.enter(new Wizard("Black wizard")); 16 | tower.enter(new Wizard("Green wizard")); 17 | tower.enter(new Wizard("Brown wizard")); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /proxy/src/main/java/com/iluwatar/proxy/Wizard.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.proxy; 2 | 3 | public class Wizard { 4 | 5 | private String name; 6 | 7 | public Wizard(String name) { 8 | this.name = name; 9 | } 10 | 11 | @Override 12 | public String toString() { 13 | return name; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /proxy/src/main/java/com/iluwatar/proxy/WizardTower.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.proxy; 2 | 3 | /** 4 | * 5 | * The object to be proxyed. 6 | * 7 | */ 8 | public class WizardTower { 9 | 10 | public void enter(Wizard wizard) { 11 | System.out.println(wizard + " enters the tower."); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /proxy/src/main/java/com/iluwatar/proxy/WizardTowerProxy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.proxy; 2 | 3 | /** 4 | * 5 | * The proxy controlling access to WizardTower. 6 | * 7 | */ 8 | public class WizardTowerProxy extends WizardTower { 9 | 10 | private static final int NUM_WIZARDS_ALLOWED = 3; 11 | 12 | private int numWizards; 13 | 14 | @Override 15 | public void enter(Wizard wizard) { 16 | if (numWizards < NUM_WIZARDS_ALLOWED) { 17 | super.enter(wizard); 18 | numWizards++; 19 | } else { 20 | System.out.println(wizard + " is not allowed to enter!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /proxy/src/test/java/com/iluwatar/proxy/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.proxy; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.proxy.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /resource-acquisition-is-initialization/etc/resource-acquisition-is-initialization.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/resource-acquisition-is-initialization/etc/resource-acquisition-is-initialization.png -------------------------------------------------------------------------------- /resource-acquisition-is-initialization/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | resource-acquisition-is-initialization 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /resource-acquisition-is-initialization/src/main/java/com/iluwatar/resourceacquisitionisinitialization/SlidingDoor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.resourceacquisitionisinitialization; 2 | 3 | /** 4 | * 5 | * SlidingDoor resource 6 | * 7 | */ 8 | public class SlidingDoor implements AutoCloseable { 9 | 10 | public SlidingDoor() { 11 | System.out.println("Sliding door opens."); 12 | } 13 | 14 | @Override 15 | public void close() throws Exception { 16 | System.out.println("Sliding door closes."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /resource-acquisition-is-initialization/src/main/java/com/iluwatar/resourceacquisitionisinitialization/TreasureChest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.resourceacquisitionisinitialization; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | 6 | /** 7 | * 8 | * TreasureChest resource 9 | * 10 | */ 11 | public class TreasureChest implements Closeable { 12 | 13 | public TreasureChest() { 14 | System.out.println("Treasure chest opens."); 15 | } 16 | 17 | @Override 18 | public void close() throws IOException { 19 | System.out.println("Treasure chest closes."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /resource-acquisition-is-initialization/src/test/java/com/iluwatar/resourceacquisitionisinitialization/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.resourceacquisitionisinitialization; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.resourceacquisitionisinitialization.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() throws Exception { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /servant/etc/servant-pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/servant/etc/servant-pattern.png -------------------------------------------------------------------------------- /servant/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | servant 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /servant/src/etc/servant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/servant/src/etc/servant.jpg -------------------------------------------------------------------------------- /servant/src/main/java/com/iluwatar/servant/King.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servant; 2 | 3 | public class King implements Royalty { 4 | private boolean isDrunk; 5 | private boolean isHungry = true; 6 | private boolean isHappy; 7 | private boolean complimentReceived; 8 | 9 | @Override 10 | public void getFed() { 11 | isHungry = false; 12 | } 13 | 14 | @Override 15 | public void getDrink() { 16 | isDrunk = true; 17 | } 18 | 19 | public void receiveCompliments() { 20 | complimentReceived = true; 21 | } 22 | 23 | @Override 24 | public void changeMood() { 25 | if (!isHungry && isDrunk) isHappy = true; 26 | if (complimentReceived) isHappy = false; 27 | } 28 | 29 | @Override 30 | public boolean getMood() { 31 | return isHappy; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /servant/src/main/java/com/iluwatar/servant/Royalty.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servant; 2 | 3 | interface Royalty { 4 | 5 | void getFed(); 6 | 7 | void getDrink(); 8 | 9 | void changeMood(); 10 | 11 | void receiveCompliments(); 12 | 13 | boolean getMood(); 14 | } 15 | -------------------------------------------------------------------------------- /servant/src/main/java/com/iluwatar/servant/Servant.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servant; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Servant { 6 | public String name; 7 | 8 | public Servant(String name){ 9 | this.name = name; 10 | } 11 | 12 | public void feed(Royalty r){ 13 | r.getFed(); 14 | } 15 | 16 | public void giveWine(Royalty r){ 17 | r.getDrink(); 18 | } 19 | 20 | public void GiveCompliments(Royalty r){ 21 | r.receiveCompliments(); 22 | } 23 | 24 | public boolean checkIfYouWillBeHanged(ArrayList tableGuests){ 25 | boolean anotherDay = true; 26 | for( Royalty r : tableGuests ) 27 | if( !r.getMood() ) anotherDay = false; 28 | 29 | return anotherDay; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /servant/src/test/java/com/iluwatar/servant/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servant; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.servant.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /service-layer/etc/service-layer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/service-layer/etc/service-layer.png -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/common/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.common; 2 | 3 | import javax.persistence.Inheritance; 4 | import javax.persistence.InheritanceType; 5 | import javax.persistence.MappedSuperclass; 6 | import javax.persistence.Version; 7 | 8 | /** 9 | * 10 | * Base class for entities. 11 | * 12 | */ 13 | @MappedSuperclass 14 | @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) 15 | public class BaseEntity { 16 | 17 | @Version 18 | private Long version; 19 | } 20 | -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/common/Dao.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.common; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * 7 | * Dao interface. 8 | * 9 | * @param 10 | * 11 | */ 12 | public interface Dao { 13 | 14 | E find(Long id); 15 | 16 | void persist(E entity); 17 | 18 | E merge(E entity); 19 | 20 | void delete(E entity); 21 | 22 | List findAll(); 23 | } 24 | -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/magic/MagicService.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.magic; 2 | 3 | import java.util.List; 4 | 5 | import com.iluwatar.servicelayer.servicelayer.spell.Spell; 6 | import com.iluwatar.servicelayer.spellbook.Spellbook; 7 | import com.iluwatar.servicelayer.wizard.Wizard; 8 | 9 | 10 | /** 11 | * 12 | * Service interface. 13 | * 14 | */ 15 | public interface MagicService { 16 | 17 | List findAllWizards(); 18 | 19 | List findAllSpellbooks(); 20 | 21 | List findAllSpells(); 22 | 23 | List findWizardsWithSpellbook(String name); 24 | 25 | List findWizardsWithSpell(String name); 26 | } 27 | -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/servicelayer/spell/SpellDao.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.servicelayer.spell; 2 | 3 | import com.iluwatar.servicelayer.common.Dao; 4 | 5 | /** 6 | * 7 | * SpellDao interface. 8 | * 9 | */ 10 | public interface SpellDao extends Dao { 11 | 12 | Spell findByName(String name); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/spellbook/SpellbookDao.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.spellbook; 2 | 3 | import com.iluwatar.servicelayer.common.Dao; 4 | 5 | /** 6 | * 7 | * SpellbookDao interface. 8 | * 9 | */ 10 | public interface SpellbookDao extends Dao { 11 | 12 | Spellbook findByName(String name); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /service-layer/src/main/java/com/iluwatar/servicelayer/wizard/WizardDao.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.wizard; 2 | 3 | import com.iluwatar.servicelayer.common.Dao; 4 | 5 | /** 6 | * 7 | * WizardDao interface. 8 | * 9 | */ 10 | public interface WizardDao extends Dao { 11 | 12 | Wizard findByName(String name); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /service-layer/src/test/java/com/iluwatar/servicelayer/app/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelayer.app; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.servicelayer.app.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /service-locator/etc/service-locator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/service-locator/etc/service-locator.png -------------------------------------------------------------------------------- /service-locator/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | service-locator 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /service-locator/src/main/java/com/iluwatar/servicelocator/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelocator; 2 | 3 | /** 4 | * Service locator pattern, used to lookup jndi services 5 | * and cache them for subsequent requests. 6 | * 7 | * @author saifasif 8 | */ 9 | public class App { 10 | public static void main(String[] args) { 11 | Service service = ServiceLocator.getService("jndi/serviceA"); 12 | service.execute(); 13 | service = ServiceLocator.getService("jndi/serviceB"); 14 | service.execute(); 15 | service = ServiceLocator.getService("jndi/serviceA"); 16 | service.execute(); 17 | service = ServiceLocator.getService("jndi/serviceA"); 18 | service.execute(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /service-locator/src/main/java/com/iluwatar/servicelocator/Service.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelocator; 2 | 3 | /** 4 | * This is going to be the parent service interface which we will 5 | * use to create our services. All services will have a 6 | *
  • service name
  • 7 | *
  • unique id
  • 8 | *
  • execution work flow
  • 9 | * @author saifasif 10 | * 11 | */ 12 | public interface Service { 13 | 14 | /* 15 | * The human readable name of the service 16 | */ 17 | String getName(); 18 | 19 | /* 20 | * Unique ID of the particular service 21 | */ 22 | int getId(); 23 | 24 | /* 25 | * The workflow method that defines what this service does 26 | */ 27 | void execute(); 28 | } 29 | -------------------------------------------------------------------------------- /service-locator/src/test/java/com/iluwatar/servicelocator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.servicelocator; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.servicelocator.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /singleton/etc/singleton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/singleton/etc/singleton.png -------------------------------------------------------------------------------- /singleton/etc/singleton_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/singleton/etc/singleton_1.png -------------------------------------------------------------------------------- /singleton/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | singleton 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.singleton; 2 | 3 | /** 4 | * 5 | * Enum Singleton class. 6 | * Effective Java 2nd Edition (Joshua Bloch) p. 18 7 | * 8 | */ 9 | public enum EnumIvoryTower { 10 | 11 | INSTANCE; 12 | 13 | @Override 14 | public String toString() { 15 | return getDeclaringClass().getCanonicalName() + "@" + hashCode(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /singleton/src/main/java/com/iluwatar/singleton/IvoryTower.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.singleton; 2 | 3 | /** 4 | * 5 | * Singleton class. 6 | * Eagerly initialized static instance guarantees thread 7 | * safety. 8 | * 9 | */ 10 | public class IvoryTower { 11 | 12 | private static IvoryTower instance = new IvoryTower(); 13 | 14 | private IvoryTower() { 15 | } 16 | 17 | public static IvoryTower getInstance() { 18 | return instance; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /singleton/src/main/java/com/iluwatar/singleton/ThreadSafeLazyLoadedIvoryTower.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.singleton; 2 | 3 | /** 4 | * 5 | * Thread-safe Singleton class. 6 | * The instance is lazily initialized and thus needs synchronization 7 | * mechanism. 8 | * 9 | */ 10 | public class ThreadSafeLazyLoadedIvoryTower { 11 | 12 | private static ThreadSafeLazyLoadedIvoryTower instance = null; 13 | 14 | private ThreadSafeLazyLoadedIvoryTower() { 15 | } 16 | 17 | public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() { 18 | /* 19 | * The instance gets created only when it is called for first time. 20 | * Lazy-loading 21 | */ 22 | if (instance == null) { 23 | instance = new ThreadSafeLazyLoadedIvoryTower(); 24 | } 25 | 26 | return instance; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /singleton/src/test/java/com/iluwatar/singleton/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.singleton; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.singleton.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /specification/etc/specification.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/specification/etc/specification.png -------------------------------------------------------------------------------- /specification/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | specification 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Creature.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Creature interface. 10 | * 11 | */ 12 | public interface Creature { 13 | 14 | String getName(); 15 | 16 | Size getSize(); 17 | 18 | Movement getMovement(); 19 | 20 | Color getColor(); 21 | } 22 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Dragon.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Dragon creature. 10 | * 11 | */ 12 | public class Dragon extends AbstractCreature { 13 | 14 | public Dragon() { 15 | super("Dragon", Size.LARGE, Movement.FLYING, Color.RED); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Goblin.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Goblin creature. 10 | * 11 | */ 12 | public class Goblin extends AbstractCreature { 13 | 14 | public Goblin() { 15 | super("Goblin", Size.SMALL, Movement.WALKING, Color.GREEN); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/KillerBee.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * KillerBee creature. 10 | * 11 | */ 12 | public class KillerBee extends AbstractCreature { 13 | 14 | public KillerBee() { 15 | super("KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Octopus.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Octopus creature. 10 | * 11 | */ 12 | public class Octopus extends AbstractCreature { 13 | 14 | public Octopus() { 15 | super("Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Shark.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Shark creature. 10 | * 11 | */ 12 | public class Shark extends AbstractCreature { 13 | 14 | public Shark() { 15 | super("Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/creature/Troll.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.creature; 2 | 3 | import com.iluwatar.specification.property.Color; 4 | import com.iluwatar.specification.property.Movement; 5 | import com.iluwatar.specification.property.Size; 6 | 7 | /** 8 | * 9 | * Troll creature. 10 | * 11 | */ 12 | public class Troll extends AbstractCreature { 13 | 14 | public Troll() { 15 | super("Troll", Size.LARGE, Movement.WALKING, Color.DARK); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/property/Color.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.property; 2 | 3 | /** 4 | * 5 | * Color property. 6 | * 7 | */ 8 | public enum Color { 9 | 10 | DARK("dark"), LIGHT("light"), GREEN("green"), RED("red"); 11 | 12 | private String title; 13 | 14 | Color(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/property/Movement.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.property; 2 | 3 | /** 4 | * 5 | * Movement property. 6 | * 7 | */ 8 | public enum Movement { 9 | 10 | WALKING("walking"), SWIMMING("swimming"), FLYING("flying"); 11 | 12 | private String title; 13 | 14 | Movement(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/property/Size.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.property; 2 | 3 | /** 4 | * 5 | * Size property. 6 | * 7 | */ 8 | public enum Size { 9 | 10 | SMALL("small"), NORMAL("normal"), LARGE("large"); 11 | 12 | private String title; 13 | 14 | Size(String title) { 15 | this.title = title; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return title; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/selector/ColorSelector.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.selector; 2 | 3 | import java.util.function.Predicate; 4 | 5 | import com.iluwatar.specification.creature.Creature; 6 | import com.iluwatar.specification.property.Color; 7 | 8 | /** 9 | * 10 | * Color selector. 11 | * 12 | */ 13 | public class ColorSelector implements Predicate { 14 | 15 | private final Color c; 16 | 17 | public ColorSelector(Color c) { 18 | this.c = c; 19 | } 20 | 21 | @Override 22 | public boolean test(Creature t) { 23 | return t.getColor().equals(c); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/selector/MovementSelector.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.selector; 2 | 3 | import java.util.function.Predicate; 4 | 5 | import com.iluwatar.specification.creature.Creature; 6 | import com.iluwatar.specification.property.Movement; 7 | 8 | /** 9 | * 10 | * Movement selector. 11 | * 12 | */ 13 | public class MovementSelector implements Predicate { 14 | 15 | private final Movement m; 16 | 17 | public MovementSelector(Movement m) { 18 | this.m = m; 19 | } 20 | 21 | @Override 22 | public boolean test(Creature t) { 23 | return t.getMovement().equals(m); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /specification/src/main/java/com/iluwatar/specification/selector/SizeSelector.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.selector; 2 | 3 | import java.util.function.Predicate; 4 | 5 | import com.iluwatar.specification.creature.Creature; 6 | import com.iluwatar.specification.property.Size; 7 | 8 | /** 9 | * 10 | * Size selector. 11 | * 12 | */ 13 | public class SizeSelector implements Predicate { 14 | 15 | private final Size s; 16 | 17 | public SizeSelector(Size s) { 18 | this.s = s; 19 | } 20 | 21 | @Override 22 | public boolean test(Creature t) { 23 | return t.getSize().equals(s); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /specification/src/test/java/com/iluwatar/specification/app/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.specification.app; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.specification.app.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /state/etc/state.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/state/etc/state.png -------------------------------------------------------------------------------- /state/etc/state_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/state/etc/state_1.png -------------------------------------------------------------------------------- /state/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | state 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /state/src/main/java/com/iluwatar/state/AngryState.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | /** 4 | * 5 | * Angry state. 6 | * 7 | */ 8 | public class AngryState implements State { 9 | 10 | private Mammoth mammoth; 11 | 12 | public AngryState(Mammoth mammoth) { 13 | this.mammoth = mammoth; 14 | } 15 | 16 | @Override 17 | public void observe() { 18 | System.out.println(String.format("%s is furious!", mammoth)); 19 | } 20 | 21 | @Override 22 | public void onEnterState() { 23 | System.out.println(String.format("%s gets angry!", mammoth)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /state/src/main/java/com/iluwatar/state/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | /** 4 | * 5 | * In State pattern the container object (Mammoth) has an internal state object (State) that 6 | * defines the current behavior. The state object can be changed to alter the 7 | * behavior. 8 | * 9 | * In this example the mammoth changes its behavior as time passes by. 10 | * 11 | */ 12 | public class App { 13 | 14 | public static void main(String[] args) { 15 | 16 | Mammoth mammoth = new Mammoth(); 17 | mammoth.observe(); 18 | mammoth.timePasses(); 19 | mammoth.observe(); 20 | mammoth.timePasses(); 21 | mammoth.observe(); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /state/src/main/java/com/iluwatar/state/Mammoth.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | /** 4 | * 5 | * Mammoth has internal state that defines its behavior. 6 | * 7 | */ 8 | public class Mammoth { 9 | 10 | private State state; 11 | 12 | public Mammoth() { 13 | state = new PeacefulState(this); 14 | } 15 | 16 | public void timePasses() { 17 | if (state.getClass().equals(PeacefulState.class)) { 18 | changeStateTo(new AngryState(this)); 19 | } else { 20 | changeStateTo(new PeacefulState(this)); 21 | } 22 | } 23 | 24 | private void changeStateTo(State newState) { 25 | this.state = newState; 26 | this.state.onEnterState(); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "The mammoth"; 32 | } 33 | 34 | public void observe() { 35 | this.state.observe(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /state/src/main/java/com/iluwatar/state/PeacefulState.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | /** 4 | * 5 | * Peaceful state. 6 | * 7 | */ 8 | public class PeacefulState implements State { 9 | 10 | private Mammoth mammoth; 11 | 12 | public PeacefulState(Mammoth mammoth) { 13 | this.mammoth = mammoth; 14 | } 15 | 16 | @Override 17 | public void observe() { 18 | System.out.println(String.format("%s is calm and peaceful.", mammoth)); 19 | } 20 | 21 | @Override 22 | public void onEnterState() { 23 | System.out.println(String.format("%s calms down.", mammoth)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /state/src/main/java/com/iluwatar/state/State.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | /** 4 | * 5 | * State interface. 6 | * 7 | */ 8 | public interface State { 9 | 10 | void onEnterState(); 11 | 12 | void observe(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /state/src/test/java/com/iluwatar/state/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.state; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.state.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /strategy/etc/strategy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/strategy/etc/strategy.png -------------------------------------------------------------------------------- /strategy/etc/strategy_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/strategy/etc/strategy_1.png -------------------------------------------------------------------------------- /strategy/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | strategy 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * Strategy (DragonSlayingStrategy) encapsulates an algorithm. The containing 6 | * object (DragonSlayer) can alter its behavior by changing its strategy. 7 | * 8 | */ 9 | public class App { 10 | 11 | public static void main(String[] args) { 12 | System.out.println("Green dragon spotted ahead!"); 13 | DragonSlayer dragonSlayer = new DragonSlayer(new MeleeStrategy()); 14 | dragonSlayer.goToBattle(); 15 | System.out.println("Red dragon emerges."); 16 | dragonSlayer.changeStrategy(new ProjectileStrategy()); 17 | dragonSlayer.goToBattle(); 18 | System.out.println("Black dragon lands before you."); 19 | dragonSlayer.changeStrategy(new SpellStrategy()); 20 | dragonSlayer.goToBattle(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/DragonSlayer.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * DragonSlayer uses different strategies to slay the dragon. 6 | * 7 | */ 8 | public class DragonSlayer { 9 | 10 | private DragonSlayingStrategy strategy; 11 | 12 | public DragonSlayer(DragonSlayingStrategy strategy) { 13 | this.strategy = strategy; 14 | } 15 | 16 | public void changeStrategy(DragonSlayingStrategy strategy) { 17 | this.strategy = strategy; 18 | } 19 | 20 | public void goToBattle() { 21 | strategy.execute(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/DragonSlayingStrategy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * Strategy interface. 6 | * 7 | */ 8 | public interface DragonSlayingStrategy { 9 | 10 | void execute(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/MeleeStrategy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * Melee strategy. 6 | * 7 | */ 8 | public class MeleeStrategy implements DragonSlayingStrategy { 9 | 10 | @Override 11 | public void execute() { 12 | System.out.println("With your Excalibur you severe the dragon's head!"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/ProjectileStrategy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * Projectile strategy. 6 | * 7 | */ 8 | public class ProjectileStrategy implements DragonSlayingStrategy { 9 | 10 | @Override 11 | public void execute() { 12 | System.out 13 | .println("You shoot the dragon with the magical crossbow and it falls dead on the ground!"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /strategy/src/main/java/com/iluwatar/strategy/SpellStrategy.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | /** 4 | * 5 | * Spell strategy. 6 | * 7 | */ 8 | public class SpellStrategy implements DragonSlayingStrategy { 9 | 10 | @Override 11 | public void execute() { 12 | System.out 13 | .println("You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /strategy/src/test/java/com/iluwatar/strategy/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.strategy; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.strategy.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /template-method/etc/template-method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/template-method/etc/template-method.png -------------------------------------------------------------------------------- /template-method/etc/template-method_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/template-method/etc/template-method_1.png -------------------------------------------------------------------------------- /template-method/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | template-method 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /template-method/src/main/java/com/iluwatar/templatemethod/App.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | /** 4 | * 5 | * Template Method defines a skeleton for an algorithm. The algorithm subclasses 6 | * provide implementation for the blank parts. 7 | * 8 | * In this example HalflingThief contains StealingMethod that can be changed. 9 | * First the thief hits with HitAndRunMethod and then with SubtleMethod. 10 | * 11 | */ 12 | public class App { 13 | 14 | public static void main(String[] args) { 15 | HalflingThief thief = new HalflingThief(new HitAndRunMethod()); 16 | thief.steal(); 17 | thief.changeMethod(new SubtleMethod()); 18 | thief.steal(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | /** 4 | * 5 | * Halfling thief uses StealingMethod to steal. 6 | * 7 | */ 8 | public class HalflingThief { 9 | 10 | private StealingMethod method; 11 | 12 | public HalflingThief(StealingMethod method) { 13 | this.method = method; 14 | } 15 | 16 | public void steal() { 17 | method.steal(); 18 | } 19 | 20 | public void changeMethod(StealingMethod method) { 21 | this.method = method; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | /** 4 | * 5 | * HitAndRunMethod implementation of StealingMethod. 6 | * 7 | */ 8 | public class HitAndRunMethod extends StealingMethod { 9 | 10 | @Override 11 | protected String pickTarget() { 12 | return "old goblin woman"; 13 | } 14 | 15 | @Override 16 | protected void confuseTarget(String target) { 17 | System.out.println("Approach the " + target + " from behind."); 18 | } 19 | 20 | @Override 21 | protected void stealTheItem(String target) { 22 | System.out.println("Grab the handbag and run away fast!"); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | /** 4 | * 5 | * StealingMethod defines skeleton for the algorithm. 6 | * 7 | */ 8 | public abstract class StealingMethod { 9 | 10 | protected abstract String pickTarget(); 11 | 12 | protected abstract void confuseTarget(String target); 13 | 14 | protected abstract void stealTheItem(String target); 15 | 16 | public void steal() { 17 | String target = pickTarget(); 18 | System.out.println("The target has been chosen as " + target + "."); 19 | confuseTarget(target); 20 | stealTheItem(target); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | /** 4 | * 5 | * SubtleMethod implementation of StealingMethod. 6 | * 7 | */ 8 | public class SubtleMethod extends StealingMethod { 9 | 10 | @Override 11 | protected String pickTarget() { 12 | return "shop keeper"; 13 | } 14 | 15 | @Override 16 | protected void confuseTarget(String target) { 17 | System.out.println("Approach the " + target 18 | + " with tears running and hug him!"); 19 | } 20 | 21 | @Override 22 | protected void stealTheItem(String target) { 23 | System.out.println("While in close contact grab the " + target 24 | + "'s wallet."); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /template-method/src/test/java/com/iluwatar/templatemethod/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.templatemethod; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.templatemethod.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /thread-pool/etc/thread-pool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/thread-pool/etc/thread-pool.png -------------------------------------------------------------------------------- /thread-pool/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | thread-pool 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /thread-pool/src/main/java/com/iluwatar/threadpool/CoffeeMakingTask.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.threadpool; 2 | 3 | /** 4 | * 5 | * CoffeeMakingTask is a concrete task 6 | * 7 | */ 8 | public class CoffeeMakingTask extends Task { 9 | 10 | private static final int TIME_PER_CUP = 300; 11 | 12 | public CoffeeMakingTask(int numCups) { 13 | super(numCups * TIME_PER_CUP); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return String.format("%s %s", this.getClass().getSimpleName(), super.toString()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /thread-pool/src/main/java/com/iluwatar/threadpool/PotatoPeelingTask.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.threadpool; 2 | 3 | /** 4 | * 5 | * PotatoPeelingTask is a concrete task 6 | * 7 | */ 8 | public class PotatoPeelingTask extends Task { 9 | 10 | private static final int TIME_PER_POTATO = 500; 11 | 12 | public PotatoPeelingTask(int numPotatoes) { 13 | super(numPotatoes * TIME_PER_POTATO); 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return String.format("%s %s", this.getClass().getSimpleName(), super.toString()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /thread-pool/src/main/java/com/iluwatar/threadpool/Task.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.threadpool; 2 | 3 | /** 4 | * 5 | * Abstract base class for tasks 6 | * 7 | */ 8 | public abstract class Task { 9 | 10 | private static int nextId = 1; 11 | 12 | private final int id; 13 | private final int timeMs; 14 | 15 | public Task(final int timeMs) { 16 | this.id = nextId++; 17 | this.timeMs = timeMs; 18 | } 19 | 20 | public int getId() { 21 | return id; 22 | } 23 | 24 | public int getTimeMs() { 25 | return timeMs; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return String.format("id=%d timeMs=%d", id, timeMs); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /thread-pool/src/main/java/com/iluwatar/threadpool/Worker.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.threadpool; 2 | 3 | /** 4 | * 5 | * Worker implements Runnable and thus can be executed by ExecutorService 6 | * 7 | */ 8 | public class Worker implements Runnable { 9 | 10 | private final Task task; 11 | 12 | public Worker(final Task task) { 13 | this.task = task; 14 | } 15 | 16 | @Override 17 | public void run() { 18 | System.out.println(String.format("%s processing %s", Thread.currentThread().getName(), task.toString())); 19 | try { 20 | Thread.sleep(task.getTimeMs()); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /thread-pool/src/test/java/com/iluwatar/threadpool/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.threadpool; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.threadpool.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tolerant-reader/etc/tolerant-reader.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/tolerant-reader/etc/tolerant-reader.png -------------------------------------------------------------------------------- /tolerant-reader/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | tolerant-reader 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /tolerant-reader/src/test/java/com/iluwatar/tolerantreader/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.tolerantreader; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import org.junit.After; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | import com.iluwatar.tolerantreader.App; 11 | 12 | 13 | public class AppTest { 14 | 15 | @Test 16 | public void test() throws ClassNotFoundException, IOException { 17 | String[] args = {}; 18 | App.main(args); 19 | } 20 | 21 | @Before 22 | @After 23 | public void cleanup() { 24 | File file1 = new File("fish1.out"); 25 | file1.delete(); 26 | File file2 = new File("fish2.out"); 27 | file2.delete(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /visitor/etc/visitor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/visitor/etc/visitor.png -------------------------------------------------------------------------------- /visitor/etc/visitor_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengjundev/Java-Design-Patterns/db6ec3cc1a79102ce6f7e1c16a8664a09f2a417f/visitor/etc/visitor_1.png -------------------------------------------------------------------------------- /visitor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.iluwatar 7 | java-design-patterns 8 | 1.1.0 9 | 10 | visitor 11 | 12 | 13 | junit 14 | junit 15 | test 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/Commander.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * Commander 6 | * 7 | */ 8 | public class Commander extends Unit { 9 | 10 | public Commander(Unit... children) { 11 | super(children); 12 | } 13 | 14 | @Override 15 | public void accept(UnitVisitor visitor) { 16 | visitor.visitCommander(this); 17 | super.accept(visitor); 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "commander"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/CommanderVisitor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * CommanderVisitor 6 | * 7 | */ 8 | public class CommanderVisitor implements UnitVisitor { 9 | 10 | @Override 11 | public void visitSoldier(Soldier soldier) { 12 | } 13 | 14 | @Override 15 | public void visitSergeant(Sergeant sergeant) { 16 | } 17 | 18 | @Override 19 | public void visitCommander(Commander commander) { 20 | System.out.println("Good to see you " + commander); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/Sergeant.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * Sergeant 6 | * 7 | */ 8 | public class Sergeant extends Unit { 9 | 10 | public Sergeant(Unit... children) { 11 | super(children); 12 | } 13 | 14 | @Override 15 | public void accept(UnitVisitor visitor) { 16 | visitor.visitSergeant(this); 17 | super.accept(visitor); 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "sergeant"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/SergeantVisitor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * SergeantVisitor 6 | * 7 | */ 8 | public class SergeantVisitor implements UnitVisitor { 9 | 10 | @Override 11 | public void visitSoldier(Soldier soldier) { 12 | } 13 | 14 | @Override 15 | public void visitSergeant(Sergeant sergeant) { 16 | System.out.println("Hello " + sergeant); 17 | } 18 | 19 | @Override 20 | public void visitCommander(Commander commander) { 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/Soldier.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * Soldier 6 | * 7 | */ 8 | public class Soldier extends Unit { 9 | 10 | public Soldier(Unit... children) { 11 | super(children); 12 | } 13 | 14 | @Override 15 | public void accept(UnitVisitor visitor) { 16 | visitor.visitSoldier(this); 17 | super.accept(visitor); 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "soldier"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/SoldierVisitor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * SoldierVisitor 6 | * 7 | */ 8 | public class SoldierVisitor implements UnitVisitor { 9 | 10 | @Override 11 | public void visitSoldier(Soldier soldier) { 12 | System.out.println("Greetings " + soldier); 13 | } 14 | 15 | @Override 16 | public void visitSergeant(Sergeant sergeant) { 17 | } 18 | 19 | @Override 20 | public void visitCommander(Commander commander) { 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/Unit.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * Interface for the nodes in hierarchy. 6 | * 7 | */ 8 | public abstract class Unit { 9 | 10 | private Unit[] children; 11 | 12 | public Unit(Unit... children) { 13 | this.children = children; 14 | } 15 | 16 | public void accept(UnitVisitor visitor) { 17 | for (Unit child : children) { 18 | child.accept(visitor); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /visitor/src/main/java/com/iluwatar/visitor/UnitVisitor.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | /** 4 | * 5 | * Visitor interface. 6 | * 7 | */ 8 | public interface UnitVisitor { 9 | 10 | void visitSoldier(Soldier soldier); 11 | 12 | void visitSergeant(Sergeant sergeant); 13 | 14 | void visitCommander(Commander commander); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /visitor/src/test/java/com/iluwatar/visitor/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.iluwatar.visitor; 2 | 3 | import org.junit.Test; 4 | 5 | import com.iluwatar.visitor.App; 6 | 7 | public class AppTest { 8 | 9 | @Test 10 | public void test() { 11 | String[] args = {}; 12 | App.main(args); 13 | } 14 | } 15 | --------------------------------------------------------------------------------