├── .gitattributes ├── Head First 设计模式(中文版).pdf ├── HeadFirst-c++ ├── adapter │ ├── Ducks-inherit │ │ ├── Duck.h │ │ ├── Ducks-inherit.pro │ │ ├── Ducks-inherit.pro.user │ │ ├── MallardDuck.cpp │ │ ├── MallardDuck.h │ │ ├── Turkey.h │ │ ├── WildTurkey.cpp │ │ ├── WildTurkey.h │ │ ├── WildTurkeyAdapter.cpp │ │ ├── WildTurkeyAdapter.h │ │ └── main.cpp │ └── Ducks │ │ ├── Duck.h │ │ ├── DuckAdapter.cpp │ │ ├── DuckAdapter.h │ │ ├── Ducks.pro │ │ ├── Ducks.pro.user │ │ ├── MallardDuck.cpp │ │ ├── MallardDuck.h │ │ ├── Turkey.h │ │ ├── TurkeyAdapter.cpp │ │ ├── TurkeyAdapter.h │ │ ├── WildTurkey.cpp │ │ ├── WildTurkey.h │ │ └── main.cpp ├── bridge │ └── remote │ │ ├── GenericRemote.cpp │ │ ├── GenericRemote.h │ │ ├── LG.cpp │ │ ├── LG.h │ │ ├── RemoteControl.cpp │ │ ├── RemoteControl.h │ │ ├── Sony.cpp │ │ ├── Sony.h │ │ ├── SpecialRemote.cpp │ │ ├── SpecialRemote.h │ │ ├── TV.h │ │ ├── TVFactory.h │ │ ├── main.cpp │ │ ├── remote.pro │ │ └── remote.pro.user ├── builder │ └── vacation │ │ ├── Accommodation.cpp │ │ ├── Accommodation.h │ │ ├── CityVacationBuilder.cpp │ │ ├── CityVacationBuilder.h │ │ ├── Hotel.cpp │ │ ├── Hotel.h │ │ ├── LocalDate.h │ │ ├── OutdoorsVacationBuilder.cpp │ │ ├── OutdoorsVacationBuilder.h │ │ ├── Reservation.cpp │ │ ├── Reservation.h │ │ ├── Tent.cpp │ │ ├── Tent.h │ │ ├── Vacation.cpp │ │ ├── Vacation.h │ │ ├── VacationBuilder.h │ │ ├── main.cpp │ │ ├── vacation.pro │ │ └── vacation.pro.user ├── command │ └── undo │ │ ├── CeilingFan.cpp │ │ ├── CeilingFan.h │ │ ├── CeilingFanHighCommand.cpp │ │ ├── CeilingFanHighCommand.h │ │ ├── CeilingFanLowCommand.cpp │ │ ├── CeilingFanLowCommand.h │ │ ├── CeilingFanMediumCommand.cpp │ │ ├── CeilingFanMediumCommand.h │ │ ├── CeilingFanOffCommand.cpp │ │ ├── CeilingFanOffCommand.h │ │ ├── Command.h │ │ ├── Light.cpp │ │ ├── Light.h │ │ ├── LightOffCommand.cpp │ │ ├── LightOffCommand.h │ │ ├── LightOnCommand.cpp │ │ ├── LightOnCommand.h │ │ ├── NoCommand.h │ │ ├── RemoteControlWithUndo.cpp │ │ ├── RemoteControlWithUndo.h │ │ ├── main.cpp │ │ ├── undo.pro │ │ └── undo.pro.user ├── composite │ └── menu │ │ ├── Menu.cpp │ │ ├── Menu.h │ │ ├── MenuComponent.cpp │ │ ├── MenuComponent.h │ │ ├── MenuItem.cpp │ │ ├── MenuItem.h │ │ ├── Waitress.cpp │ │ ├── Waitress.h │ │ ├── main.cpp │ │ ├── menu.pro │ │ └── menu.pro.user ├── decorator │ ├── Beverage.cpp │ ├── Beverage.h │ ├── CondimentDecorator.cpp │ ├── CondimentDecorator.h │ ├── DarkRoast.cpp │ ├── DarkRoast.h │ ├── Decaf.cpp │ ├── Decaf.h │ ├── Espresso.cpp │ ├── Espresso.h │ ├── HouseBlend.cpp │ ├── HouseBlend.h │ ├── Milk.cpp │ ├── Milk.h │ ├── Mocha.cpp │ ├── Mocha.h │ ├── Soy.cpp │ ├── Soy.h │ ├── Whip.cpp │ ├── Whip.h │ ├── decorator.pro │ ├── decorator.pro.user │ └── main.cpp ├── facade │ └── hometheater │ │ ├── Amplifier.cpp │ │ ├── Amplifier.h │ │ ├── CdPlayer.cpp │ │ ├── CdPlayer.h │ │ ├── HomeTheaterFacade.cpp │ │ ├── HomeTheaterFacade.h │ │ ├── PopcornPopper.cpp │ │ ├── PopcornPopper.h │ │ ├── Projector.cpp │ │ ├── Projector.h │ │ ├── Screen.cpp │ │ ├── Screen.h │ │ ├── StreamingPlayer.cpp │ │ ├── StreamingPlayer.h │ │ ├── TheaterLights.cpp │ │ ├── TheaterLights.h │ │ ├── Tuner.cpp │ │ ├── Tuner.h │ │ ├── hometheater.pro │ │ ├── hometheater.pro.user │ │ └── main.cpp ├── factor │ ├── pizzaaf │ │ ├── AllIngredients.h │ │ ├── BlackOlives.h │ │ ├── Cheese.h │ │ ├── CheesePizza.cpp │ │ ├── CheesePizza.h │ │ ├── ChicagoPizzaIngredientFactory.cpp │ │ ├── ChicagoPizzaIngredientFactory.h │ │ ├── ChicagoPizzaStore.cpp │ │ ├── ChicagoPizzaStore.h │ │ ├── ClamPizza.cpp │ │ ├── ClamPizza.h │ │ ├── Clams.h │ │ ├── Dough.h │ │ ├── Eggplant.h │ │ ├── FreshClams.h │ │ ├── FrozenClams.h │ │ ├── Garlic.h │ │ ├── MarinaraSauce.h │ │ ├── MozzarellaCheese.h │ │ ├── Mushroom.h │ │ ├── NYPizzaIngredientFactory.cpp │ │ ├── NYPizzaIngredientFactory.h │ │ ├── NYPizzaStore.cpp │ │ ├── NYPizzaStore.h │ │ ├── Onion.h │ │ ├── Pepperoni.h │ │ ├── PepperoniPizza.cpp │ │ ├── PepperoniPizza.h │ │ ├── Pizza.cpp │ │ ├── Pizza.h │ │ ├── PizzaIngredientFactory.h │ │ ├── PizzaStore.cpp │ │ ├── PizzaStore.h │ │ ├── PlumTomatoSauce.h │ │ ├── RedPepper.h │ │ ├── ReggianoCheese.h │ │ ├── Sauce.h │ │ ├── SlicedPepperoni.h │ │ ├── Spinach.h │ │ ├── ThickCrustDough.h │ │ ├── ThinCrustDough.h │ │ ├── VeggiePizza.cpp │ │ ├── VeggiePizza.h │ │ ├── Veggies.h │ │ ├── main.cpp │ │ ├── pizzaaf.pro │ │ └── pizzaaf.pro.user │ ├── pizzafm │ │ ├── ChicagoPizzaStore.cpp │ │ ├── ChicagoPizzaStore.h │ │ ├── ChicagoStyleCheesePizza.cpp │ │ ├── ChicagoStyleCheesePizza.h │ │ ├── ChicagoStyleClamPizza.cpp │ │ ├── ChicagoStyleClamPizza.h │ │ ├── ChicagoStylePepperoniPizza.cpp │ │ ├── ChicagoStylePepperoniPizza.h │ │ ├── ChicagoStyleVeggiePizza.cpp │ │ ├── ChicagoStyleVeggiePizza.h │ │ ├── NYPizzaStore.cpp │ │ ├── NYPizzaStore.h │ │ ├── NYStyleCheesePizza.cpp │ │ ├── NYStyleCheesePizza.h │ │ ├── NYStyleClamPizza.cpp │ │ ├── NYStyleClamPizza.h │ │ ├── NYStylePepperoniPizza.cpp │ │ ├── NYStylePepperoniPizza.h │ │ ├── NYStyleVeggiePizza.cpp │ │ ├── NYStyleVeggiePizza.h │ │ ├── Pizza.cpp │ │ ├── Pizza.h │ │ ├── PizzaStore.cpp │ │ ├── PizzaStore.h │ │ ├── main.cpp │ │ ├── pizzafm.pro │ │ └── pizzafm.pro.user │ └── pizzas │ │ ├── CheesePizza.h │ │ ├── ClamPizza.h │ │ ├── PepperoniPizza.h │ │ ├── Pizza.cpp │ │ ├── Pizza.h │ │ ├── PizzaStore.cpp │ │ ├── PizzaStore.h │ │ ├── SimplePizzaFactory.cpp │ │ ├── SimplePizzaFactory.h │ │ ├── VeggiePizza.h │ │ ├── main.cpp │ │ ├── pizzas.pro │ │ └── pizzas.pro.user ├── iterator │ └── dinermerger │ │ ├── ArrayIterator.cpp │ │ ├── ArrayIterator.h │ │ ├── DinerMenu.cpp │ │ ├── DinerMenu.h │ │ ├── Iterator.h │ │ ├── Menu.h │ │ ├── MenuItem.cpp │ │ ├── MenuItem.h │ │ ├── PancakeHouseMenu.cpp │ │ ├── PancakeHouseMenu.h │ │ ├── VectorIterator.cpp │ │ ├── VectorIterator.h │ │ ├── Waitress.cpp │ │ ├── Waitress.h │ │ ├── dinermerger.pro │ │ ├── dinermerger.pro.user │ │ └── main.cpp ├── observer │ ├── CurrentConditionsDisplay.cpp │ ├── CurrentConditionsDisplay.h │ ├── DisplayElement.h │ ├── ForecastDisplay.cpp │ ├── ForecastDisplay.h │ ├── Observer.h │ ├── StatisticsDisplay.cpp │ ├── StatisticsDisplay.h │ ├── Subject.h │ ├── ThirdPartDisplay.cpp │ ├── ThirdPartDisplay.h │ ├── WeatherData.cpp │ ├── WeatherData.h │ ├── main.cpp │ ├── observer.pro │ └── observer.pro.user ├── proxy │ └── example │ │ ├── Proxy.cpp │ │ ├── Proxy.h │ │ ├── RealSubject.cpp │ │ ├── RealSubject.h │ │ ├── Subject.h │ │ ├── example.pro │ │ ├── example.pro.user │ │ └── main.cpp ├── singleton │ ├── chocolate │ │ ├── ChocolateBoiler.cpp │ │ ├── ChocolateBoiler.h │ │ ├── chocolate.pro │ │ ├── chocolate.pro.user │ │ └── main.cpp │ └── classic │ │ ├── Singleton.cpp │ │ ├── Singleton.h │ │ ├── classic.pro │ │ ├── classic.pro.user │ │ └── main.cpp ├── state │ ├── gumball │ │ ├── GumballMachine.cpp │ │ ├── GumballMachine.h │ │ ├── gumball.pro │ │ ├── gumball.pro.user │ │ └── main.cpp │ ├── gumballstate │ │ ├── GumballMachine.cpp │ │ ├── GumballMachine.h │ │ ├── HasQuarterState.cpp │ │ ├── HasQuarterState.h │ │ ├── NoQuarterState.cpp │ │ ├── NoQuarterState.h │ │ ├── SoldOutState.cpp │ │ ├── SoldOutState.h │ │ ├── SoldState.cpp │ │ ├── SoldState.h │ │ ├── State.h │ │ ├── gumballstate.pro │ │ ├── gumballstate.pro.user │ │ └── main.cpp │ └── gumballstatewinner │ │ ├── GumballMachine.cpp │ │ ├── GumballMachine.h │ │ ├── HasQuarterState.cpp │ │ ├── HasQuarterState.h │ │ ├── NoQuarterState.cpp │ │ ├── NoQuarterState.h │ │ ├── SoldOutState.cpp │ │ ├── SoldOutState.h │ │ ├── SoldState.cpp │ │ ├── SoldState.h │ │ ├── State.h │ │ ├── WinnerState.cpp │ │ ├── WinnerState.h │ │ ├── gumballstatewinner.pro │ │ ├── gumballstatewinner.pro.user │ │ └── main.cpp ├── strategy │ ├── DecoyDuck.cpp │ ├── DecoyDuck.h │ ├── Duck.cpp │ ├── Duck.h │ ├── FlyBehavior.h │ ├── FlyNoWay.cpp │ ├── FlyNoWay.h │ ├── FlyRocketPowered.cpp │ ├── FlyRocketPowered.h │ ├── FlyWithWings.cpp │ ├── FlyWithWings.h │ ├── MallardDuck.cpp │ ├── MallardDuck.h │ ├── ModelDuck.cpp │ ├── ModelDuck.h │ ├── MuteQuack.cpp │ ├── MuteQuack.h │ ├── Quack.cpp │ ├── Quack.h │ ├── QuackBehavior.h │ ├── ReadheadDuck.cpp │ ├── ReadheadDuck.h │ ├── RubberDuck.cpp │ ├── RubberDuck.h │ ├── Squeak.cpp │ ├── Squeak.h │ ├── main.cpp │ ├── strategy.pro │ └── strategy.pro.user └── templatemethod │ └── barista │ ├── CaffeineBeverage.cpp │ ├── CaffeineBeverage.h │ ├── CaffeineBeverageWithHook.cpp │ ├── CaffeineBeverageWithHook.h │ ├── Coffee.cpp │ ├── Coffee.h │ ├── CoffeeWithHook.cpp │ ├── CoffeeWithHook.h │ ├── Tea.cpp │ ├── Tea.h │ ├── TeaWithHook.cpp │ ├── TeaWithHook.h │ ├── barista.pro │ ├── barista.pro.user │ └── main.cpp ├── HeadFirst-java ├── adapter │ ├── ducks │ │ ├── Duck.java │ │ ├── DuckAdapter.java │ │ ├── DuckTestDrive.java │ │ ├── MallardDuck.java │ │ ├── Turkey.java │ │ ├── TurkeyAdapter.java │ │ ├── TurkeyTestDrive.java │ │ ├── WildTurkey.java │ │ └── challenge │ │ │ ├── Drone.java │ │ │ ├── DroneAdapter.java │ │ │ └── SuperDrone.java │ └── iterenum │ │ ├── EI.java │ │ ├── EnumerationIterator.java │ │ ├── EnumerationIteratorTestDrive.java │ │ ├── IteratorEnumeration.java │ │ └── IteratorEnumerationTestDrive.java ├── bridge │ └── remote │ │ ├── Client.java │ │ ├── GenericRemote.java │ │ ├── LG.java │ │ ├── RemoteControl.java │ │ ├── Sony.java │ │ ├── SpecialRemote.java │ │ ├── TV.java │ │ └── TVFactory.java ├── builder │ ├── house │ │ ├── GingerbreadHouseBuilder.java │ │ ├── House.java │ │ ├── HouseBuilder.java │ │ ├── HouseDirector.java │ │ ├── InteriorWall.java │ │ ├── Roof.java │ │ ├── StoneHouseBuilder.java │ │ ├── Wall.java │ │ ├── Window.java │ │ └── WoodHouseBuilder.java │ ├── pizza │ │ ├── MeatPizzaBuilder.java │ │ ├── Pizza.java │ │ ├── PizzaBuilder.java │ │ ├── PizzaDirector.java │ │ └── VeggiePizzaBuilder.java │ └── vacation │ │ ├── Accommodation.java │ │ ├── CityVacationBuilder.java │ │ ├── Hotel.java │ │ ├── OutdoorsVacationBuilder.java │ │ ├── Reservation.java │ │ ├── Tent.java │ │ ├── Vacation.java │ │ ├── VacationBuilder.java │ │ └── VacationDirector.java ├── collections │ ├── Collections.java │ ├── iterator │ │ ├── Cafe.java │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Iterator.java │ │ ├── Menu.java │ │ ├── PancakeHouseMenu.java │ │ └── PancakeHouseMenuIterator.java │ └── iterator_builtin │ │ ├── Cafe.java │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Menu.java │ │ └── PancakeHouseMenu.java ├── combined │ └── djview │ │ ├── BPMObserver.java │ │ ├── BeatBar.java │ │ ├── BeatController.java │ │ ├── BeatModel.java │ │ ├── BeatModelInterface.java │ │ ├── BeatObserver.java │ │ ├── ControllerInterface.java │ │ ├── DJTestDrive.java │ │ ├── DJView.java │ │ ├── DJViewHttpHandler.java │ │ ├── DJViewHttpServer.java │ │ ├── DJViewServlet.java │ │ ├── HeartAdapter.java │ │ ├── HeartController.java │ │ ├── HeartModel.java │ │ ├── HeartModelInterface.java │ │ ├── HeartTestDrive.java │ │ ├── README.md │ │ └── jsp │ │ ├── WEB-INF │ │ └── web.xml │ │ ├── djview.jsp │ │ └── index.html ├── combining │ ├── adapter │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckSimulator.java │ │ ├── Goose.java │ │ ├── GooseAdapter.java │ │ ├── MallardDuck.java │ │ ├── Quackable.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java │ ├── composite │ │ ├── AbstractDuckFactory.java │ │ ├── CountingDuckFactory.java │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckFactory.java │ │ ├── DuckSimulator.java │ │ ├── Flock.java │ │ ├── Goose.java │ │ ├── GooseAdapter.java │ │ ├── MallardDuck.java │ │ ├── QuackCounter.java │ │ ├── Quackable.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java │ ├── decorator │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckSimulator.java │ │ ├── Goose.java │ │ ├── GooseAdapter.java │ │ ├── MallardDuck.java │ │ ├── QuackCounter.java │ │ ├── Quackable.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java │ ├── ducks │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckSimulator.java │ │ ├── MallardDuck.java │ │ ├── Quackable.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java │ ├── factory │ │ ├── AbstractDuckFactory.java │ │ ├── CountingDuckFactory.java │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckFactory.java │ │ ├── DuckSimulator.java │ │ ├── Goose.java │ │ ├── GooseAdapter.java │ │ ├── MallardDuck.java │ │ ├── QuackCounter.java │ │ ├── Quackable.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java │ └── observer │ │ ├── .QuackDecorator.java.swp │ │ ├── AbstractDuckFactory.java │ │ ├── CountingDuckFactory.java │ │ ├── DecoyDuck.java │ │ ├── DuckCall.java │ │ ├── DuckFactory.java │ │ ├── DuckSimulator.java │ │ ├── Flock.java │ │ ├── Goose.java │ │ ├── GooseAdapter.java │ │ ├── MallardDuck.java │ │ ├── Observable.java │ │ ├── Observer.java │ │ ├── QuackCounter.java │ │ ├── QuackObservable.java │ │ ├── Quackable.java │ │ ├── Quackologist.java │ │ ├── RedheadDuck.java │ │ └── RubberDuck.java ├── command │ ├── diner │ │ ├── BurgerAndFriesOrder.java │ │ ├── Cook.java │ │ ├── Customer.java │ │ ├── Diner.java │ │ ├── Order.java │ │ └── Waitress.java │ ├── dinerLambda │ │ ├── Cook.java │ │ ├── Customer.java │ │ ├── Diner.java │ │ ├── Order.java │ │ └── Waitress.java │ ├── party │ │ ├── CeilingFan.java │ │ ├── CeilingFanHighCommand.java │ │ ├── CeilingFanMediumCommand.java │ │ ├── CeilingFanOffCommand.java │ │ ├── Command.java │ │ ├── Hottub.java │ │ ├── HottubOffCommand.java │ │ ├── HottubOnCommand.java │ │ ├── Light.java │ │ ├── LightOffCommand.java │ │ ├── LightOnCommand.java │ │ ├── LivingroomLightOffCommand.java │ │ ├── LivingroomLightOnCommand.java │ │ ├── MacroCommand.java │ │ ├── NoCommand.java │ │ ├── RemoteControl.java │ │ ├── RemoteLoader.java │ │ ├── Stereo.java │ │ ├── StereoOffCommand.java │ │ ├── StereoOnCommand.java │ │ ├── StereoOnWithCDCommand.java │ │ ├── TV.java │ │ ├── TVOffCommand.java │ │ └── TVOnCommand.java │ ├── remote │ │ ├── CeilingFan.java │ │ ├── CeilingFanOffCommand.java │ │ ├── CeilingFanOnCommand.java │ │ ├── Command.java │ │ ├── GarageDoor.java │ │ ├── GarageDoorDownCommand.java │ │ ├── GarageDoorUpCommand.java │ │ ├── Hottub.java │ │ ├── HottubOffCommand.java │ │ ├── HottubOnCommand.java │ │ ├── Light.java │ │ ├── LightOffCommand.java │ │ ├── LightOnCommand.java │ │ ├── LivingroomLightOffCommand.java │ │ ├── LivingroomLightOnCommand.java │ │ ├── NoCommand.java │ │ ├── RemoteControl.java │ │ ├── RemoteLoader.java │ │ ├── Stereo.java │ │ ├── StereoOffCommand.java │ │ ├── StereoOnWithCDCommand.java │ │ └── TV.java │ ├── remoteWL │ │ ├── CeilingFan.java │ │ ├── Command.java │ │ ├── GarageDoor.java │ │ ├── Hottub.java │ │ ├── Light.java │ │ ├── RemoteControl.java │ │ ├── RemoteLoader.java │ │ ├── Stereo.java │ │ └── TV.java │ ├── simpleremote │ │ ├── Command.java │ │ ├── GarageDoor.java │ │ ├── GarageDoorOpenCommand.java │ │ ├── Light.java │ │ ├── LightOffCommand.java │ │ ├── LightOnCommand.java │ │ ├── RemoteControlTest.java │ │ └── SimpleRemoteControl.java │ ├── simpleremoteWL │ │ ├── Command.java │ │ ├── GarageDoor.java │ │ ├── Light.java │ │ ├── RemoteControlTest.java │ │ └── SimpleRemoteControl.java │ ├── swing │ │ └── SwingCommandExample.java │ └── undo │ │ ├── CeilingFan.java │ │ ├── CeilingFanHighCommand.java │ │ ├── CeilingFanLowCommand.java │ │ ├── CeilingFanMediumCommand.java │ │ ├── CeilingFanOffCommand.java │ │ ├── Command.java │ │ ├── DimmerLightOffCommand.java │ │ ├── DimmerLightOnCommand.java │ │ ├── Light.java │ │ ├── LightOffCommand.java │ │ ├── LightOnCommand.java │ │ ├── NoCommand.java │ │ ├── RemoteControlWithUndo.java │ │ └── RemoteLoader.java ├── composite │ ├── menu │ │ ├── Menu.java │ │ ├── MenuComponent.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ └── Waitress.java │ └── menuiterator │ │ ├── CompositeIterator.java │ │ ├── Menu.java │ │ ├── MenuComponent.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── NullIterator.java │ │ └── Waitress.java ├── decorator │ ├── io │ │ ├── InputTest.java │ │ └── LowerCaseInputStream.java │ ├── pizza │ │ ├── Cheese.java │ │ ├── Olives.java │ │ ├── Pizza.java │ │ ├── PizzaStore.java │ │ ├── ThickcrustPizza.java │ │ ├── ThincrustPizza.java │ │ └── ToppingDecorator.java │ ├── starbuzz │ │ ├── Beverage.java │ │ ├── CondimentDecorator.java │ │ ├── DarkRoast.java │ │ ├── Decaf.java │ │ ├── Espresso.java │ │ ├── HouseBlend.java │ │ ├── Milk.java │ │ ├── Mocha.java │ │ ├── Soy.java │ │ ├── StarbuzzCoffee.java │ │ └── Whip.java │ └── starbuzzWithSizes │ │ ├── Beverage.java │ │ ├── CondimentDecorator.java │ │ ├── DarkRoast.java │ │ ├── Decaf.java │ │ ├── Espresso.java │ │ ├── HouseBlend.java │ │ ├── Milk.java │ │ ├── Mocha.java │ │ ├── Soy.java │ │ ├── StarbuzzCoffee.java │ │ └── Whip.java ├── ducks │ ├── Duck.java │ ├── DuckAdapter.java │ ├── DuckTestDrive.java │ ├── MallardDuck.java │ ├── Turkey.java │ ├── TurkeyAdapter.java │ ├── TurkeyTestDrive.java │ └── WildTurkey.java ├── facade │ └── hometheater │ │ ├── Amplifier.java │ │ ├── CdPlayer.java │ │ ├── HomeTheaterFacade.java │ │ ├── HomeTheaterTestDrive.java │ │ ├── PopcornPopper.java │ │ ├── Projector.java │ │ ├── Screen.java │ │ ├── StreamingPlayer.java │ │ ├── TheaterLights.java │ │ └── Tuner.java ├── factory │ ├── challenge │ │ ├── Calendar.java │ │ ├── CalendarTestDrive.java │ │ ├── PacificCalendar.java │ │ ├── Zone.java │ │ ├── ZoneCentral.java │ │ ├── ZoneEastern.java │ │ ├── ZoneFactory.java │ │ ├── ZoneMountain.java │ │ └── ZonePacific.java │ ├── pizzaaf │ │ ├── BlackOlives.java │ │ ├── Cheese.java │ │ ├── CheesePizza.java │ │ ├── ChicagoPizzaIngredientFactory.java │ │ ├── ChicagoPizzaStore.java │ │ ├── ClamPizza.java │ │ ├── Clams.java │ │ ├── Dough.java │ │ ├── Eggplant.java │ │ ├── FreshClams.java │ │ ├── FrozenClams.java │ │ ├── Garlic.java │ │ ├── MarinaraSauce.java │ │ ├── MozzarellaCheese.java │ │ ├── Mushroom.java │ │ ├── NYPizzaIngredientFactory.java │ │ ├── NYPizzaStore.java │ │ ├── Onion.java │ │ ├── ParmesanCheese.java │ │ ├── Pepperoni.java │ │ ├── PepperoniPizza.java │ │ ├── Pizza.java │ │ ├── PizzaIngredientFactory.java │ │ ├── PizzaStore.java │ │ ├── PizzaTestDrive.java │ │ ├── PlumTomatoSauce.java │ │ ├── RedPepper.java │ │ ├── ReggianoCheese.java │ │ ├── Sauce.java │ │ ├── SlicedPepperoni.java │ │ ├── Spinach.java │ │ ├── ThickCrustDough.java │ │ ├── ThinCrustDough.java │ │ ├── VeggiePizza.java │ │ └── Veggies.java │ ├── pizzafm │ │ ├── ChicagoPizzaStore.java │ │ ├── ChicagoStyleCheesePizza.java │ │ ├── ChicagoStyleClamPizza.java │ │ ├── ChicagoStylePepperoniPizza.java │ │ ├── ChicagoStyleVeggiePizza.java │ │ ├── DependentPizzaStore.java │ │ ├── NYPizzaStore.java │ │ ├── NYStyleCheesePizza.java │ │ ├── NYStyleClamPizza.java │ │ ├── NYStylePepperoniPizza.java │ │ ├── NYStyleVeggiePizza.java │ │ ├── Pizza.java │ │ ├── PizzaStore.java │ │ └── PizzaTestDrive.java │ └── pizzas │ │ ├── CheesePizza.java │ │ ├── ClamPizza.java │ │ ├── PepperoniPizza.java │ │ ├── Pizza.java │ │ ├── PizzaStore.java │ │ ├── PizzaTestDrive.java │ │ ├── SimplePizzaFactory.java │ │ └── VeggiePizza.java ├── flyweight │ ├── Client.java │ ├── ConiferTree.java │ ├── DeciduousTree.java │ ├── Tree.java │ └── TreeFactory.java ├── iterator │ ├── dinermerger │ │ ├── AlternatingDinerMenuIterator.java │ │ ├── ArrayIterator.java │ │ ├── ArrayListIterator.java │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Iterator.java │ │ ├── Menu.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── PancakeHouseMenu.java │ │ ├── PancakeHouseMenuIterator.java │ │ └── Waitress.java │ ├── dinermergercafe │ │ ├── AlternatingDinerMenuIterator.java │ │ ├── CafeMenu.java │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Menu.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── PancakeHouseMenu.java │ │ └── Waitress.java │ ├── dinermergeri │ │ ├── AlternatingDinerMenuIterator.java │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Menu.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── PancakeHouseMenu.java │ │ └── Waitress.java │ ├── implicit │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Menu.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── PancakeHouseMenu.java │ │ └── Waitress.java │ └── transition │ │ ├── DinerMenu.java │ │ ├── DinerMenuIterator.java │ │ ├── Menu.java │ │ ├── MenuItem.java │ │ ├── MenuTestDrive.java │ │ ├── PancakeHouseMenu.java │ │ └── Waitress.java ├── iterenum │ ├── EI.java │ ├── EnumerationIterator.java │ ├── EnumerationIteratorTestDrive.java │ ├── IteratorEnumeration.java │ └── IteratorEnumerationTestDrive.java ├── observer │ ├── simple │ │ ├── Example.java │ │ ├── Observer.java │ │ ├── SimpleObserver.java │ │ ├── SimpleSubject.java │ │ └── Subject.java │ ├── simpleobservable │ │ ├── Example.java │ │ ├── SimpleObserver.java │ │ └── SimpleSubject.java │ ├── swing │ │ └── SwingObserverExample.java │ ├── weather │ │ ├── CurrentConditionsDisplay.java │ │ ├── DisplayElement.java │ │ ├── ForecastDisplay.java │ │ ├── HeatIndexDisplay.java │ │ ├── Observer.java │ │ ├── StatisticsDisplay.java │ │ ├── Subject.java │ │ ├── WeatherData.java │ │ ├── WeatherStation.java │ │ └── WeatherStationHeatIndex.java │ └── weatherobservable │ │ ├── CurrentConditionsDisplay.java │ │ ├── DisplayElement.java │ │ ├── ForecastDisplay.java │ │ ├── HeatIndexDisplay.java │ │ ├── StatisticsDisplay.java │ │ ├── WeatherData.java │ │ ├── WeatherStation.java │ │ └── WeatherStationHeatIndex.java ├── prototype │ ├── Client.java │ ├── Dragon.java │ ├── Drakon.java │ ├── Monster.java │ └── monsters.html ├── proxy │ ├── gumball │ │ ├── GumballMachine.java │ │ ├── GumballMachineRemote.java │ │ ├── GumballMachineTestDrive.java │ │ ├── GumballMonitor.java │ │ ├── GumballMonitorTestDrive.java │ │ ├── HasQuarterState.java │ │ ├── NoQuarterState.java │ │ ├── README.md │ │ ├── SoldOutState.java │ │ ├── SoldState.java │ │ ├── State.java │ │ └── WinnerState.java │ ├── gumballmonitor │ │ ├── GumballMachine.java │ │ ├── GumballMachineTestDrive.java │ │ ├── GumballMonitor.java │ │ ├── HasQuarterState.java │ │ ├── NoQuarterState.java │ │ ├── SoldOutState.java │ │ ├── SoldState.java │ │ ├── State.java │ │ └── WinnerState.java │ ├── javaproxy │ │ ├── MatchMakingTestDrive.java │ │ ├── NonOwnerInvocationHandler.java │ │ ├── OwnerInvocationHandler.java │ │ ├── Person.java │ │ └── PersonImpl.java │ └── virtualproxy │ │ ├── ImageComponent.java │ │ ├── ImageProxy.java │ │ └── ImageProxyTestDrive.java ├── singleton │ ├── chocolate │ │ ├── ChocolateBoiler.java │ │ └── ChocolateController.java │ ├── classic │ │ ├── Singleton.java │ │ └── SingletonClient.java │ ├── dcl │ │ ├── Singleton.java │ │ └── SingletonClient.java │ ├── enumS │ │ ├── Singleton.java │ │ └── SingletonClient.java │ ├── stat │ │ ├── Singleton.java │ │ └── SingletonClient.java │ ├── subclass │ │ ├── CoolerSingleton.java │ │ ├── HotterSingleton.java │ │ ├── Singleton.java │ │ └── SingletonTestDrive.java │ └── threadsafe │ │ ├── Singleton.java │ │ └── SingletonClient.java ├── state │ ├── gumball │ │ ├── GumballMachine.java │ │ └── GumballMachineTestDrive.java │ ├── gumballstate │ │ ├── GumballMachine.java │ │ ├── GumballMachineTestDrive.java │ │ ├── HasQuarterState.java │ │ ├── NoQuarterState.java │ │ ├── SoldOutState.java │ │ ├── SoldState.java │ │ └── State.java │ └── gumballstatewinner │ │ ├── GumballMachine.java │ │ ├── GumballMachineTestDrive.java │ │ ├── HasQuarterState.java │ │ ├── NoQuarterState.java │ │ ├── SoldOutState.java │ │ ├── SoldState.java │ │ ├── State.java │ │ └── WinnerState.java ├── strategy │ ├── AnimalTest.java │ ├── DecoyDuck.java │ ├── Duck.java │ ├── FakeQuack.java │ ├── FlyBehavior.java │ ├── FlyNoWay.java │ ├── FlyRocketPowered.java │ ├── FlyWithWings.java │ ├── MallardDuck.java │ ├── MiniDuckSimulator.java │ ├── MiniDuckSimulator1.java │ ├── ModelDuck.java │ ├── MuteQuack.java │ ├── Quack.java │ ├── QuackBehavior.java │ ├── RedHeadDuck.java │ ├── RubberDuck.java │ ├── Squeak.java │ └── challenge │ │ ├── BasicCameraApp.java │ │ ├── CameraPlusApp.java │ │ ├── Email.java │ │ ├── PhoneCameraApp.java │ │ ├── PhotoWithPhone.java │ │ ├── ShareStrategy.java │ │ ├── Social.java │ │ └── Txt.java └── templatemethod │ ├── applet │ ├── AppletSource.txt │ └── MyApplet.java │ ├── barista │ ├── BeverageTestDrive.java │ ├── CaffeineBeverage.java │ ├── CaffeineBeverageWithHook.java │ ├── Coffee.java │ ├── CoffeeWithHook.java │ ├── Tea.java │ └── TeaWithHook.java │ ├── frame │ └── MyFrame.java │ ├── list │ ├── MyListTestDrive.java │ └── MyStringList.java │ ├── simplebarista │ ├── Barista.java │ ├── Coffee.java │ └── Tea.java │ └── sort │ ├── Duck.java │ └── DuckSortTestDrive.java └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /Head First 设计模式(中文版).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HelloZhan/HeadFirst/2ee6a0f4a291347c8e4cd15122ad58ca61bb8121/Head First 设计模式(中文版).pdf -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks-inherit/Duck.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | #include 4 | using std::cout; 5 | using std::endl; 6 | class Duck{ 7 | public: 8 | virtual void quack()=0; 9 | virtual void fly(){cout<<"Duck fly"< 4 | using std::cout; 5 | using std::endl; 6 | class Turkey{ 7 | public: 8 | virtual void gobble()=0; 9 | virtual void fly()=0; 10 | virtual ~Turkey(){}; 11 | }; 12 | #endif // TURKEY_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks-inherit/WildTurkey.cpp: -------------------------------------------------------------------------------- 1 | #include "WildTurkey.h" 2 | 3 | void WildTurkey::gobble() 4 | { 5 | cout<<"Gobble gobble"< 4 | using std::cout; 5 | using std::endl; 6 | class Duck{ 7 | public: 8 | virtual void quack()=0; 9 | virtual void fly()=0; 10 | virtual ~Duck(){}; 11 | }; 12 | 13 | #endif // DUCK_H 14 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/DuckAdapter.cpp: -------------------------------------------------------------------------------- 1 | #include "DuckAdapter.h" 2 | #include 3 | DuckAdapter::DuckAdapter(Duck *duck) 4 | { 5 | this->duck=duck; 6 | } 7 | 8 | void DuckAdapter::gobble() 9 | { 10 | duck->quack(); 11 | } 12 | 13 | void DuckAdapter::fly() 14 | { 15 | int randomi=rand()%2; 16 | if(randomi)duck->fly(); 17 | } 18 | 19 | DuckAdapter::~DuckAdapter() 20 | { 21 | delete duck; 22 | } 23 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/DuckAdapter.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCKADAPTER_H 2 | #define DUCKADAPTER_H 3 | #include "Turkey.h" 4 | #include "Duck.h" 5 | class DuckAdapter:public Turkey{ 6 | public: 7 | DuckAdapter(Duck *duck); 8 | virtual void gobble()override final; 9 | virtual void fly()override final; 10 | virtual ~DuckAdapter(); 11 | private: 12 | Duck *duck; 13 | }; 14 | 15 | #endif // DUCKADAPTER_H 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/Ducks.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | DuckAdapter.cpp \ 8 | MallardDuck.cpp \ 9 | TurkeyAdapter.cpp \ 10 | WildTurkey.cpp \ 11 | main.cpp 12 | 13 | HEADERS += \ 14 | Duck.h \ 15 | DuckAdapter.h \ 16 | MallardDuck.h \ 17 | Turkey.h \ 18 | TurkeyAdapter.h \ 19 | WildTurkey.h 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/MallardDuck.cpp: -------------------------------------------------------------------------------- 1 | #include "MallardDuck.h" 2 | 3 | void MallardDuck::quack() 4 | { 5 | cout<<"Quack"< 4 | using std::cout; 5 | using std::endl; 6 | class Turkey{ 7 | public: 8 | virtual void gobble()=0; 9 | virtual void fly()=0; 10 | virtual ~Turkey(){}; 11 | }; 12 | 13 | #endif // TURKEY_H 14 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/TurkeyAdapter.cpp: -------------------------------------------------------------------------------- 1 | #include "TurkeyAdapter.h" 2 | 3 | TurkeyAdapter::TurkeyAdapter(Turkey *turkey) 4 | { 5 | this->turkey=turkey; 6 | } 7 | 8 | void TurkeyAdapter::quack() 9 | { 10 | turkey->gobble(); 11 | } 12 | 13 | void TurkeyAdapter::fly() 14 | { 15 | for(int i=0;i<5;i++)turkey->fly(); 16 | } 17 | 18 | TurkeyAdapter::~TurkeyAdapter() 19 | { 20 | delete turkey; 21 | } 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/TurkeyAdapter.h: -------------------------------------------------------------------------------- 1 | #ifndef TURKEYADAPTER_H 2 | #define TURKEYADAPTER_H 3 | #include "Turkey.h" 4 | #include "Duck.h" 5 | class TurkeyAdapter:public Duck{ 6 | public: 7 | TurkeyAdapter(Turkey *turkey); 8 | virtual void quack()override final; 9 | virtual void fly()override final; 10 | virtual ~TurkeyAdapter(); 11 | private: 12 | Turkey *turkey; 13 | }; 14 | 15 | #endif // TURKEYADAPTER_H 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/adapter/Ducks/WildTurkey.cpp: -------------------------------------------------------------------------------- 1 | #include "WildTurkey.h" 2 | 3 | void WildTurkey::gobble() 4 | { 5 | cout<<"Gobble gobble"<channel=channel; 16 | cout<<"Set the LG TV Channel to " + std::to_string(this->channel)<station=channel; 16 | cout<<"Set the Sony TV station to " + std::to_string(this->station)< 4 | using std::cout; 5 | using std::endl; 6 | class TV{ 7 | public: 8 | virtual void on()=0; 9 | virtual void off()=0; 10 | virtual void tuneChannel(int channel)=0; 11 | virtual int getChannel()=0; 12 | virtual ~TV(){}; 13 | }; 14 | 15 | #endif // TV_H 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/bridge/remote/TVFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef TVFACTORY_H 2 | #define TVFACTORY_H 3 | #include "LG.h" 4 | #include "Sony.h" 5 | #include 6 | using std::string; 7 | class TVFactory{ 8 | public: 9 | TV *getTV(string type){ 10 | if(type=="LG"){ 11 | return new LG(); 12 | }else if(type=="Sony"){ 13 | return new Sony(); 14 | }else{ 15 | std::cerr<<"Type mismatch"; 16 | return nullptr; 17 | } 18 | } 19 | }; 20 | 21 | #endif // TVFACTORY_H 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/bridge/remote/remote.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | GenericRemote.cpp \ 8 | LG.cpp \ 9 | RemoteControl.cpp \ 10 | Sony.cpp \ 11 | SpecialRemote.cpp \ 12 | main.cpp 13 | 14 | HEADERS += \ 15 | GenericRemote.h \ 16 | LG.h \ 17 | RemoteControl.h \ 18 | Sony.h \ 19 | SpecialRemote.h \ 20 | TV.h \ 21 | TVFactory.h 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Accommodation.h: -------------------------------------------------------------------------------- 1 | #ifndef ACCOMMODATION_H 2 | #define ACCOMMODATION_H 3 | #include "Reservation.h" 4 | #include 5 | using std::string; 6 | class Accommodation{ 7 | public: 8 | void setReservation(Reservation *r); 9 | Reservation *getReservation(); 10 | virtual string getLocation()=0; 11 | string toString(); 12 | virtual ~Accommodation(); 13 | string name; 14 | Reservation *reservation=nullptr; 15 | }; 16 | 17 | #endif // ACCOMMODATION_H 18 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Hotel.h: -------------------------------------------------------------------------------- 1 | #ifndef HOTEL_H 2 | #define HOTEL_H 3 | #include "Accommodation.h" 4 | class Hotel:public Accommodation{ 5 | public: 6 | Hotel(); 7 | Hotel(string name); 8 | void setRoomNumber(int n); 9 | int getRoomNumber(); 10 | virtual string getLocation()override final; 11 | virtual ~Hotel(); 12 | private: 13 | int roomNumber; 14 | }; 15 | 16 | #endif // HOTEL_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Reservation.cpp: -------------------------------------------------------------------------------- 1 | #include "Reservation.h" 2 | 3 | void Reservation::setArrivalDate(int year, int month, int day) 4 | { 5 | this->arrivalDate=LocalDate(year,month,day); 6 | 7 | } 8 | 9 | LocalDate Reservation::getArrivalDate() 10 | { 11 | return arrivalDate; 12 | } 13 | 14 | void Reservation::setNights(int nights) 15 | { 16 | this->nights=nights; 17 | } 18 | 19 | int Reservation::getNights() 20 | { 21 | return nights; 22 | } 23 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Reservation.h: -------------------------------------------------------------------------------- 1 | #ifndef RESERVATION_H 2 | #define RESERVATION_H 3 | #include "LocalDate.h" 4 | class Reservation{ 5 | public: 6 | void setArrivalDate(int year, int month, int day); 7 | LocalDate getArrivalDate(); 8 | void setNights(int nights); 9 | int getNights(); 10 | private: 11 | LocalDate arrivalDate; 12 | int nights; 13 | }; 14 | 15 | #endif // RESERVATION_H 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Tent.h: -------------------------------------------------------------------------------- 1 | #ifndef TENT_H 2 | #define TENT_H 3 | #include "Accommodation.h" 4 | class Tent:public Accommodation{ 5 | public: 6 | Tent(); 7 | Tent(string name); 8 | void setSiteNumber(int n); 9 | int getSiteNumber(); 10 | virtual string getLocation()override final; 11 | virtual ~Tent(); 12 | private: 13 | int siteNumber; 14 | }; 15 | 16 | #endif // TENT_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/builder/vacation/Vacation.h: -------------------------------------------------------------------------------- 1 | #ifndef VACATION_H 2 | #define VACATION_H 3 | #include "Accommodation.h" 4 | #include 5 | using std::vector; 6 | class Vacation{ 7 | public: 8 | void setName(string name); 9 | void setAccommodations(vector accommodations); 10 | void setEvents(vector events); 11 | string toString(); 12 | private: 13 | string name; 14 | vector events; 15 | vector accommodations; 16 | }; 17 | 18 | #endif // VACATION_H 19 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/CeilingFan.h: -------------------------------------------------------------------------------- 1 | #ifndef CEILINGFAN_H 2 | #define CEILINGFAN_H 3 | #include 4 | using std::string; 5 | enum{OFF,LOW,MEDIUM,HIGH}; 6 | class CeilingFan{ 7 | public: 8 | string location; 9 | int speed; 10 | CeilingFan(string location); 11 | void high(); 12 | void medium(); 13 | void low(); 14 | void off(); 15 | int getSpeed(); 16 | }; 17 | 18 | #endif // CEILINGFAN_H 19 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/CeilingFanHighCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef CEILINGFANHIGHCOMMAND_H 2 | #define CEILINGFANHIGHCOMMAND_H 3 | #include "Command.h" 4 | #include "CeilingFan.h" 5 | class CeilingFanHighCommand:public Command{ 6 | public: 7 | CeilingFanHighCommand(CeilingFan *ceilingFan); 8 | virtual void execute()override final; 9 | virtual void undo()override final; 10 | virtual ~CeilingFanHighCommand(); 11 | private: 12 | CeilingFan *ceilingFan; 13 | int prevSpeed; 14 | }; 15 | 16 | #endif // CEILINGFANHIGHCOMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/CeilingFanLowCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef CEILINGFANLOWCOMMAND_H 2 | #define CEILINGFANLOWCOMMAND_H 3 | #include "Command.h" 4 | #include "CeilingFan.h" 5 | class CeilingFanLowCommand:public Command{ 6 | public: 7 | CeilingFanLowCommand(CeilingFan *ceilingFan); 8 | virtual void execute()override final; 9 | virtual void undo()override final; 10 | virtual ~CeilingFanLowCommand(); 11 | private: 12 | CeilingFan *ceilingFan; 13 | int prevSpeed; 14 | }; 15 | 16 | #endif // CEILINGFANLOWCOMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/CeilingFanOffCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef CEILINGFANOFFCOMMAND_H 2 | #define CEILINGFANOFFCOMMAND_H 3 | #include "Command.h" 4 | #include "CeilingFan.h" 5 | class CeilingFanOffCommand:public Command{ 6 | public: 7 | CeilingFanOffCommand(CeilingFan *ceilingFan); 8 | virtual void execute()override final; 9 | virtual void undo()override final; 10 | virtual ~CeilingFanOffCommand(); 11 | private: 12 | CeilingFan *ceilingFan; 13 | int prevSpeed; 14 | }; 15 | 16 | #endif // CEILINGFANOFFCOMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/Command.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | #include 4 | #include 5 | using std::string; 6 | using std::cout; 7 | using std::endl; 8 | class Command{ 9 | public: 10 | std::string name="Command"; 11 | virtual void execute()=0; 12 | virtual void undo()=0; 13 | virtual ~Command(){}; 14 | }; 15 | 16 | #endif // COMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/Light.h: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | #include 4 | using std::string; 5 | class Light{ 6 | public: 7 | string location; 8 | int level; 9 | Light(string location); 10 | void on(); 11 | void off(); 12 | void dim(int level); 13 | int getLevel(); 14 | }; 15 | 16 | #endif // LIGHT_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/LightOffCommand.cpp: -------------------------------------------------------------------------------- 1 | #include "LightOffCommand.h" 2 | 3 | LightOffCommand::LightOffCommand(Light *light) 4 | { 5 | name="LightOffCommand"; 6 | this->light=light; 7 | } 8 | 9 | void LightOffCommand::execute() 10 | { 11 | level=light->getLevel(); 12 | light->off(); 13 | } 14 | 15 | void LightOffCommand::undo() 16 | { 17 | light->dim(level); 18 | } 19 | 20 | LightOffCommand::~LightOffCommand() 21 | { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/LightOffCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef LIGHTOFFCOMMAND_H 2 | #define LIGHTOFFCOMMAND_H 3 | #include "Command.h" 4 | #include "Light.h" 5 | class LightOffCommand:public Command{ 6 | public: 7 | LightOffCommand(Light *light); 8 | virtual void execute()override final; 9 | virtual void undo()override final; 10 | virtual ~LightOffCommand(); 11 | private: 12 | Light *light; 13 | int level; 14 | }; 15 | 16 | #endif // LIGHTOFFCOMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/LightOnCommand.cpp: -------------------------------------------------------------------------------- 1 | #include "LightOnCommand.h" 2 | 3 | LightOnCommand::LightOnCommand(Light *light) 4 | { 5 | name="LightOnCommand"; 6 | this->light=light; 7 | } 8 | 9 | void LightOnCommand::execute() 10 | { 11 | level=light->getLevel(); 12 | light->on(); 13 | } 14 | 15 | void LightOnCommand::undo() 16 | { 17 | light->dim(level); 18 | } 19 | 20 | LightOnCommand::~LightOnCommand() 21 | { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/LightOnCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef LIGHTONCOMMAND_H 2 | #define LIGHTONCOMMAND_H 3 | #include "Command.h" 4 | #include "Light.h" 5 | class LightOnCommand:public Command{ 6 | public: 7 | LightOnCommand(Light *light); 8 | virtual void execute()override final; 9 | virtual void undo()override final; 10 | virtual ~LightOnCommand(); 11 | private: 12 | Light *light; 13 | int level; 14 | }; 15 | 16 | #endif // LIGHTONCOMMAND_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/command/undo/NoCommand.h: -------------------------------------------------------------------------------- 1 | #ifndef NOCOMMAND_H 2 | #define NOCOMMAND_H 3 | #include "Command.h" 4 | class NoCommand:public Command{ 5 | public: 6 | NoCommand(){name="NoCommand";}; 7 | virtual void execute()override final{}; 8 | virtual void undo()override final{}; 9 | virtual ~NoCommand(){}; 10 | }; 11 | 12 | #endif // NOCOMMAND_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/composite/menu/Waitress.cpp: -------------------------------------------------------------------------------- 1 | #include "Waitress.h" 2 | 3 | Waitress::Waitress(MenuComponent *allMenus) 4 | { 5 | this->allMenus=allMenus; 6 | } 7 | 8 | void Waitress::printMenu() 9 | { 10 | allMenus->print(); 11 | } 12 | 13 | Waitress::~Waitress() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/composite/menu/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | #include "MenuComponent.h" 4 | class Waitress{ 5 | public: 6 | Waitress(MenuComponent *allMenus); 7 | void printMenu(); 8 | virtual ~Waitress(); 9 | private: 10 | MenuComponent *allMenus; 11 | }; 12 | 13 | #endif // WAITRESS_H 14 | -------------------------------------------------------------------------------- /HeadFirst-c++/composite/menu/menu.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | Menu.cpp \ 8 | MenuComponent.cpp \ 9 | MenuItem.cpp \ 10 | Waitress.cpp \ 11 | main.cpp 12 | 13 | HEADERS += \ 14 | Menu.h \ 15 | MenuComponent.h \ 16 | MenuItem.h \ 17 | Waitress.h 18 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Beverage.cpp: -------------------------------------------------------------------------------- 1 | #include "Beverage.h" 2 | 3 | Beverage::Beverage() 4 | { 5 | //cout<<"beverage"<size=size; 21 | } 22 | 23 | Size Beverage::getSize() 24 | { 25 | return size; 26 | } 27 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/CondimentDecorator.cpp: -------------------------------------------------------------------------------- 1 | #include "CondimentDecorator.h" 2 | 3 | CondimentDecorator::CondimentDecorator() 4 | { 5 | 6 | } 7 | 8 | CondimentDecorator::~CondimentDecorator() 9 | { 10 | delete beverage; 11 | } 12 | 13 | Size CondimentDecorator::getSize() 14 | { 15 | return beverage->getSize(); 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/CondimentDecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef CONDIMENTDECORATOR_H 2 | #define CONDIMENTDECORATOR_H 3 | #include "Beverage.h" 4 | class CondimentDecorator:public Beverage{ 5 | public: 6 | CondimentDecorator(); 7 | virtual string getDescription()=0; 8 | virtual ~CondimentDecorator(); 9 | Beverage *beverage; 10 | Size getSize(); 11 | }; 12 | 13 | #endif // CONDIMENTDECORATOR_H 14 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/DarkRoast.cpp: -------------------------------------------------------------------------------- 1 | #include "DarkRoast.h" 2 | 3 | DarkRoast::DarkRoast() 4 | { 5 | description="Dark Roast Coffee"; 6 | } 7 | 8 | double DarkRoast::cost() 9 | { 10 | return 0.99; 11 | } 12 | 13 | DarkRoast::~DarkRoast() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/DarkRoast.h: -------------------------------------------------------------------------------- 1 | #ifndef DARKROAST_H 2 | #define DARKROAST_H 3 | #include "Beverage.h" 4 | class DarkRoast:public Beverage{ 5 | public: 6 | DarkRoast(); 7 | virtual double cost()override final; 8 | ~DarkRoast(); 9 | }; 10 | 11 | #endif // DARKROAST_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Decaf.cpp: -------------------------------------------------------------------------------- 1 | #include "Decaf.h" 2 | 3 | Decaf::Decaf() 4 | { 5 | description="Decaf Coffee"; 6 | } 7 | 8 | double Decaf::cost() 9 | { 10 | return 1.05; 11 | } 12 | 13 | Decaf::~Decaf() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Decaf.h: -------------------------------------------------------------------------------- 1 | #ifndef DECAF_H 2 | #define DECAF_H 3 | #include "Beverage.h" 4 | class Decaf:public Beverage{ 5 | public: 6 | Decaf(); 7 | virtual double cost()override final; 8 | virtual ~Decaf(); 9 | }; 10 | 11 | #endif // DECAF_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Espresso.cpp: -------------------------------------------------------------------------------- 1 | #include "Espresso.h" 2 | 3 | Espresso::Espresso() 4 | { 5 | description="Espresso"; 6 | } 7 | 8 | double Espresso::cost() 9 | { 10 | return 1.99; 11 | } 12 | 13 | Espresso::~Espresso() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Espresso.h: -------------------------------------------------------------------------------- 1 | #ifndef ESPRESSO_H 2 | #define ESPRESSO_H 3 | #include "Beverage.h" 4 | class Espresso:public Beverage{ 5 | public: 6 | Espresso(); 7 | virtual double cost()override final; 8 | virtual ~Espresso(); 9 | }; 10 | 11 | #endif // ESPRESSO_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/HouseBlend.cpp: -------------------------------------------------------------------------------- 1 | #include "HouseBlend.h" 2 | 3 | HouseBlend::HouseBlend() 4 | { 5 | description="House Blend Coffee"; 6 | } 7 | 8 | double HouseBlend::cost() 9 | { 10 | return 0.89; 11 | } 12 | 13 | HouseBlend::~HouseBlend() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/HouseBlend.h: -------------------------------------------------------------------------------- 1 | #ifndef HOUSEBLEND_H 2 | #define HOUSEBLEND_H 3 | #include "Beverage.h" 4 | class HouseBlend:public Beverage{ 5 | public: 6 | HouseBlend(); 7 | virtual double cost()override final; 8 | virtual ~HouseBlend(); 9 | }; 10 | 11 | #endif // HOUSEBLEND_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Milk.cpp: -------------------------------------------------------------------------------- 1 | #include "Milk.h" 2 | 3 | Milk::Milk(Beverage *beverage) 4 | { 5 | this->beverage=beverage; 6 | } 7 | 8 | string Milk::getDescription() 9 | { 10 | return beverage->getDescription() + ", Milk"; 11 | } 12 | 13 | double Milk::cost() 14 | { 15 | return 0.10+beverage->cost(); 16 | } 17 | 18 | Milk::~Milk() 19 | { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Milk.h: -------------------------------------------------------------------------------- 1 | #ifndef MILK_H 2 | #define MILK_H 3 | #include "CondimentDecorator.h" 4 | class Milk:public CondimentDecorator{ 5 | public: 6 | Milk(Beverage *beverage); 7 | virtual string getDescription()override final; 8 | virtual double cost()override final; 9 | virtual ~Milk(); 10 | }; 11 | 12 | #endif // MILK_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Mocha.cpp: -------------------------------------------------------------------------------- 1 | #include "Mocha.h" 2 | 3 | Mocha::Mocha(Beverage *beverage) 4 | { 5 | this->beverage=beverage; 6 | } 7 | 8 | string Mocha::getDescription() 9 | { 10 | return beverage->getDescription() + ", Mocha"; 11 | } 12 | 13 | double Mocha::cost() 14 | { 15 | return 0.20 + beverage->cost(); 16 | } 17 | 18 | Mocha::~Mocha() 19 | { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Mocha.h: -------------------------------------------------------------------------------- 1 | #ifndef MOCHA_H 2 | #define MOCHA_H 3 | #include "CondimentDecorator.h" 4 | class Mocha:public CondimentDecorator{ 5 | public: 6 | Mocha(Beverage *beverage); 7 | virtual string getDescription()override final; 8 | virtual double cost()override final; 9 | virtual ~Mocha(); 10 | }; 11 | 12 | #endif // MOCHA_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Soy.h: -------------------------------------------------------------------------------- 1 | #ifndef SOY_H 2 | #define SOY_H 3 | #include "CondimentDecorator.h" 4 | class Soy:public CondimentDecorator{ 5 | public: 6 | Soy(Beverage *beverage); 7 | virtual string getDescription()override final; 8 | virtual double cost()override final; 9 | virtual ~Soy(); 10 | }; 11 | 12 | #endif // SOY_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Whip.cpp: -------------------------------------------------------------------------------- 1 | #include "Whip.h" 2 | 3 | Whip::Whip(Beverage *beverage) 4 | { 5 | this->beverage=beverage; 6 | } 7 | 8 | string Whip::getDescription() 9 | { 10 | return beverage->getDescription() + ", Whip"; 11 | } 12 | 13 | double Whip::cost() 14 | { 15 | return 0.10 + beverage->cost(); 16 | } 17 | 18 | Whip::~Whip() 19 | { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /HeadFirst-c++/decorator/Whip.h: -------------------------------------------------------------------------------- 1 | #ifndef WHIP_H 2 | #define WHIP_H 3 | #include "CondimentDecorator.h" 4 | class Whip:public CondimentDecorator{ 5 | public: 6 | Whip(Beverage *beverage); 7 | virtual string getDescription()override final; 8 | virtual double cost()override final; 9 | virtual ~Whip(); 10 | }; 11 | 12 | #endif // WHIP_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/facade/hometheater/PopcornPopper.h: -------------------------------------------------------------------------------- 1 | #ifndef POPCORNPOPPER_H 2 | #define POPCORNPOPPER_H 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | class PopcornPopper{ 9 | public: 10 | PopcornPopper(string description); 11 | void on(); 12 | void off(); 13 | void pop(); 14 | string toString(); 15 | private: 16 | string description; 17 | }; 18 | 19 | #endif // POPCORNPOPPER_H 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/facade/hometheater/Screen.cpp: -------------------------------------------------------------------------------- 1 | #include "Screen.h" 2 | 3 | Screen::Screen(std::string description) 4 | { 5 | this->description=description; 6 | } 7 | 8 | void Screen::up() 9 | { 10 | cout< 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | class Screen{ 9 | public: 10 | Screen(string description); 11 | void up(); 12 | void down(); 13 | string toString(); 14 | private: 15 | string description; 16 | }; 17 | 18 | #endif // SCREEN_H 19 | -------------------------------------------------------------------------------- /HeadFirst-c++/facade/hometheater/TheaterLights.h: -------------------------------------------------------------------------------- 1 | #ifndef THEATERLIGHTS_H 2 | #define THEATERLIGHTS_H 3 | #include 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | using std::string; 8 | class TheaterLights{ 9 | public: 10 | TheaterLights(string description); 11 | void on(); 12 | void off(); 13 | void dim(int level); 14 | string toString(); 15 | private: 16 | string description; 17 | }; 18 | 19 | #endif // THEATERLIGHTS_H 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/BlackOlives.h: -------------------------------------------------------------------------------- 1 | #ifndef BLACKOLIVES_H 2 | #define BLACKOLIVES_H 3 | #include "Veggies.h" 4 | class BlackOlives:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Black Olives"; 8 | }; 9 | virtual ~BlackOlives(){}; 10 | }; 11 | 12 | #endif // BLACKOLIVES_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Cheese.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_H 2 | #define CHEESE_H 3 | #include 4 | using std::string; 5 | class Cheese{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Cheese(){}; 9 | }; 10 | 11 | #endif // CHEESE_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/CheesePizza.cpp: -------------------------------------------------------------------------------- 1 | #include "CheesePizza.h" 2 | 3 | CheesePizza::CheesePizza(PizzaIngredientFactory *ingredientFactory) 4 | { 5 | this->ingredientFactory=ingredientFactory; 6 | } 7 | 8 | void CheesePizza::prepare() 9 | { 10 | cout<<"Preparing "+name<createDough(); 12 | sauce=ingredientFactory->createSauce(); 13 | cheese=ingredientFactory->createCheese(); 14 | } 15 | 16 | CheesePizza::~CheesePizza() 17 | { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/CheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESEPIZZA_H 2 | #define CHEESEPIZZA_H 3 | #include "Pizza.h" 4 | #include "PizzaIngredientFactory.h" 5 | class CheesePizza:public Pizza{ 6 | public: 7 | CheesePizza(PizzaIngredientFactory *ingredientFactory); 8 | virtual void prepare()override final; 9 | virtual ~CheesePizza(); 10 | private: 11 | PizzaIngredientFactory *ingredientFactory; 12 | }; 13 | 14 | #endif // CHEESEPIZZA_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ChicagoPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOPIZZASTORE_H 2 | #define CHICAGOPIZZASTORE_H 3 | #include "PizzaStore.h" 4 | class ChicagoPizzaStore:public PizzaStore{ 5 | public: 6 | virtual ~ChicagoPizzaStore(); 7 | protected: 8 | virtual Pizza *createPizza(string item)override final; 9 | 10 | }; 11 | 12 | #endif // CHICAGOPIZZASTORE_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ClamPizza.cpp: -------------------------------------------------------------------------------- 1 | #include "ClamPizza.h" 2 | 3 | ClamPizza::ClamPizza(PizzaIngredientFactory *ingredientFactory) 4 | { 5 | this->ingredientFactory=ingredientFactory; 6 | } 7 | 8 | void ClamPizza::prepare() 9 | { 10 | cout<<"Preparing "+name<createDough(); 12 | sauce=ingredientFactory->createSauce(); 13 | cheese=ingredientFactory->createCheese(); 14 | clam=ingredientFactory->createClam(); 15 | } 16 | 17 | ClamPizza::~ClamPizza() 18 | { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ClamPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CLAMPIZZA_H 2 | #define CLAMPIZZA_H 3 | #include "Pizza.h" 4 | #include "PizzaIngredientFactory.h" 5 | class ClamPizza:public Pizza{ 6 | public: 7 | ClamPizza(PizzaIngredientFactory *ingredientFactory); 8 | virtual void prepare()override final; 9 | virtual ~ClamPizza(); 10 | private: 11 | PizzaIngredientFactory *ingredientFactory; 12 | }; 13 | 14 | #endif // CLAMPIZZA_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Clams.h: -------------------------------------------------------------------------------- 1 | #ifndef CLAMS_H 2 | #define CLAMS_H 3 | #include 4 | using std::string; 5 | class Clams{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Clams(){}; 9 | }; 10 | 11 | #endif // CLAMS_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Dough.h: -------------------------------------------------------------------------------- 1 | #ifndef DOUGH_H 2 | #define DOUGH_H 3 | #include 4 | using std::string; 5 | class Dough{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Dough(){}; 9 | }; 10 | 11 | #endif // DOUGH_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Eggplant.h: -------------------------------------------------------------------------------- 1 | #ifndef EGGPLANT_H 2 | #define EGGPLANT_H 3 | #include "Veggies.h" 4 | class Eggplant:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Eggplant"; 8 | }; 9 | virtual ~Eggplant(){}; 10 | }; 11 | 12 | #endif // EGGPLANT_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/FreshClams.h: -------------------------------------------------------------------------------- 1 | #ifndef FRESHCLAMS_H 2 | #define FRESHCLAMS_H 3 | #include "Clams.h" 4 | class FreshClams:public Clams{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Fresh Clams from Long Island Sound"; 8 | }; 9 | virtual ~FreshClams(){}; 10 | }; 11 | 12 | #endif // FRESHCLAMS_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/FrozenClams.h: -------------------------------------------------------------------------------- 1 | #ifndef FROZENCLAMS_H 2 | #define FROZENCLAMS_H 3 | #include "Clams.h" 4 | class FrozenClams:public Clams{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Frozen Clams from Chesapeake Bay"; 8 | }; 9 | virtual ~FrozenClams(){}; 10 | }; 11 | 12 | #endif // FROZENCLAMS_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Garlic.h: -------------------------------------------------------------------------------- 1 | #ifndef GARLIC_H 2 | #define GARLIC_H 3 | #include "Veggies.h" 4 | class Garlic:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Garlic"; 8 | }; 9 | virtual ~Garlic(){}; 10 | }; 11 | 12 | #endif // GARLIC_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/MarinaraSauce.h: -------------------------------------------------------------------------------- 1 | #ifndef MARINARASAUCE_H 2 | #define MARINARASAUCE_H 3 | #include "Sauce.h" 4 | class MarinaraSauce:public Sauce{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Marinara Sauce"; 8 | }; 9 | virtual ~MarinaraSauce(){}; 10 | }; 11 | 12 | #endif // MARINARASAUCE_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/MozzarellaCheese.h: -------------------------------------------------------------------------------- 1 | #ifndef MOZZARELLACHEESE_H 2 | #define MOZZARELLACHEESE_H 3 | #include "Cheese.h" 4 | class MozzarellaCheese:public Cheese{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Shredded Mozzarella"; 8 | }; 9 | virtual ~MozzarellaCheese(){}; 10 | }; 11 | 12 | #endif // MOZZARELLACHEESE_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Mushroom.h: -------------------------------------------------------------------------------- 1 | #ifndef MUSHROOM_H 2 | #define MUSHROOM_H 3 | #include "Veggies.h" 4 | class Mushroom:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Mushrooms"; 8 | }; 9 | virtual ~Mushroom(){}; 10 | }; 11 | 12 | #endif // MUSHROOM_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/NYPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef NYPIZZASTORE_H 2 | #define NYPIZZASTORE_H 3 | #include "PizzaStore.h" 4 | class NYPizzaStore:public PizzaStore{ 5 | public: 6 | virtual ~NYPizzaStore(); 7 | protected: 8 | virtual Pizza *createPizza(string item)override final; 9 | }; 10 | 11 | #endif // NYPIZZASTORE_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Onion.h: -------------------------------------------------------------------------------- 1 | #ifndef ONION_H 2 | #define ONION_H 3 | #include "Veggies.h" 4 | class Onion:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Onion"; 8 | }; 9 | virtual ~Onion(){}; 10 | }; 11 | 12 | #endif // ONION_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Pepperoni.h: -------------------------------------------------------------------------------- 1 | #ifndef PEPPERONI_H 2 | #define PEPPERONI_H 3 | #include 4 | using std::string; 5 | class Pepperoni{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Pepperoni(){}; 9 | }; 10 | 11 | #endif // PEPPERONI_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/PepperoniPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef PEPPERONIPIZZA_H 2 | #define PEPPERONIPIZZA_H 3 | #include "Pizza.h" 4 | #include "PizzaIngredientFactory.h" 5 | class PepperoniPizza:public Pizza{ 6 | public: 7 | PepperoniPizza(PizzaIngredientFactory *ingredientFactory); 8 | virtual void prepare()override final; 9 | virtual ~PepperoniPizza(); 10 | private: 11 | PizzaIngredientFactory *ingredientFactory; 12 | }; 13 | 14 | #endif // PEPPERONIPIZZA_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | #include "PizzaStore.h" 2 | 3 | Pizza *PizzaStore::orderPizza(std::string type) 4 | { 5 | Pizza *pizza=createPizza(type); 6 | cout<<"--- Making a " + pizza->getName()+ " ---"<prepare(); 8 | pizza->bake(); 9 | pizza->cut(); 10 | pizza->box(); 11 | return pizza; 12 | } 13 | 14 | PizzaStore::~PizzaStore() 15 | { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZASTORE_H 2 | #define PIZZASTORE_H 3 | #include "Pizza.h" 4 | #include "PizzaIngredientFactory.h" 5 | class PizzaStore{ 6 | public: 7 | Pizza *orderPizza(string type); 8 | virtual ~PizzaStore(); 9 | protected: 10 | virtual Pizza *createPizza(string item)=0; 11 | 12 | }; 13 | 14 | #endif // PIZZASTORE_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/PlumTomatoSauce.h: -------------------------------------------------------------------------------- 1 | #ifndef PLUMTOMATOSAUCE_H 2 | #define PLUMTOMATOSAUCE_H 3 | #include "Sauce.h" 4 | class PlumTomatoSauce:public Sauce{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Tomato sauce with plum tomatoes"; 8 | }; 9 | virtual ~PlumTomatoSauce(){}; 10 | }; 11 | 12 | #endif // PLUMTOMATOSAUCE_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/RedPepper.h: -------------------------------------------------------------------------------- 1 | #ifndef REDPEPPER_H 2 | #define REDPEPPER_H 3 | #include "Veggies.h" 4 | class RedPepper:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Red Pepper"; 8 | }; 9 | virtual ~RedPepper(){}; 10 | }; 11 | 12 | #endif // REDPEPPER_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ReggianoCheese.h: -------------------------------------------------------------------------------- 1 | #ifndef REGGIANOCHEESE_H 2 | #define REGGIANOCHEESE_H 3 | #include "Cheese.h" 4 | class ReggianoCheese:public Cheese{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Reggiano Cheese"; 8 | }; 9 | virtual ~ReggianoCheese(){}; 10 | }; 11 | 12 | #endif // REGGIANOCHEESE_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Sauce.h: -------------------------------------------------------------------------------- 1 | #ifndef SAUCE_H 2 | #define SAUCE_H 3 | #include 4 | using std::string; 5 | class Sauce{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Sauce(){}; 9 | }; 10 | 11 | #endif // SAUCE_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/SlicedPepperoni.h: -------------------------------------------------------------------------------- 1 | #ifndef SLICEDPEPPERONI_H 2 | #define SLICEDPEPPERONI_H 3 | #include "Pepperoni.h" 4 | class SlicedPepperoni:public Pepperoni{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Sliced Pepperoni"; 8 | }; 9 | virtual ~SlicedPepperoni(){}; 10 | }; 11 | 12 | #endif // SLICEDPEPPERONI_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Spinach.h: -------------------------------------------------------------------------------- 1 | #ifndef SPINACH_H 2 | #define SPINACH_H 3 | #include "Veggies.h" 4 | class Spinach:public Veggies{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Spinach"; 8 | }; 9 | virtual ~Spinach(){}; 10 | }; 11 | 12 | #endif // SPINACH_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ThickCrustDough.h: -------------------------------------------------------------------------------- 1 | #ifndef THICKCRUSTDOUGH_H 2 | #define THICKCRUSTDOUGH_H 3 | #include "Dough.h" 4 | class ThickCrustDough:public Dough{ 5 | public: 6 | virtual string toString()override final{ 7 | return "ThickCrust style extra thick crust dough"; 8 | }; 9 | virtual ~ThickCrustDough(){}; 10 | }; 11 | 12 | #endif // THICKCRUSTDOUGH_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/ThinCrustDough.h: -------------------------------------------------------------------------------- 1 | #ifndef THINCRUSTDOUGH_H 2 | #define THINCRUSTDOUGH_H 3 | #include "Dough.h" 4 | class ThinCrustDough:public Dough{ 5 | public: 6 | virtual string toString()override final{ 7 | return "Thin Crust Dough"; 8 | }; 9 | virtual ~ThinCrustDough(){}; 10 | }; 11 | 12 | #endif // THINCRUSTDOUGH_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/VeggiePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGIEPIZZA_H 2 | #define VEGGIEPIZZA_H 3 | #include "Pizza.h" 4 | #include "PizzaIngredientFactory.h" 5 | class VeggiePizza:public Pizza{ 6 | public: 7 | VeggiePizza(PizzaIngredientFactory *ingredientFactory); 8 | virtual void prepare()override final; 9 | virtual ~VeggiePizza(); 10 | private: 11 | PizzaIngredientFactory *ingredientFactory; 12 | }; 13 | 14 | #endif // VEGGIEPIZZA_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzaaf/Veggies.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGIES_H 2 | #define VEGGIES_H 3 | #include 4 | using std::string; 5 | class Veggies{ 6 | public: 7 | virtual string toString()=0; 8 | virtual ~Veggies(){}; 9 | }; 10 | 11 | #endif // VEGGIES_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/ChicagoPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOPIZZASTORE_H 2 | #define CHICAGOPIZZASTORE_H 3 | #include "PizzaStore.h" 4 | class ChicagoPizzaStore:public PizzaStore{ 5 | public: 6 | virtual Pizza *createPizza(string item)override final; 7 | virtual ~ChicagoPizzaStore(); 8 | }; 9 | 10 | #endif // CHICAGOPIZZASTORE_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/ChicagoStyleCheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOSTYLECHEESEPIZZA_H 2 | #define CHICAGOSTYLECHEESEPIZZA_H 3 | #include "Pizza.h" 4 | class ChicagoStyleCheesePizza:public Pizza{ 5 | public: 6 | ChicagoStyleCheesePizza(); 7 | void cut(); 8 | virtual ~ChicagoStyleCheesePizza(); 9 | }; 10 | 11 | #endif // CHICAGOSTYLECHEESEPIZZA_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/ChicagoStyleClamPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOSTYLECLAMPIZZA_H 2 | #define CHICAGOSTYLECLAMPIZZA_H 3 | #include "Pizza.h" 4 | class ChicagoStyleClamPizza:public Pizza{ 5 | public: 6 | ChicagoStyleClamPizza(); 7 | void cut(); 8 | virtual ~ChicagoStyleClamPizza(); 9 | }; 10 | 11 | #endif // CHICAGOSTYLECLAMPIZZA_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/ChicagoStylePepperoniPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOSTYLEPEPPERONIPIZZA_H 2 | #define CHICAGOSTYLEPEPPERONIPIZZA_H 3 | #include "Pizza.h" 4 | class ChicagoStylePepperoniPizza:public Pizza{ 5 | public: 6 | ChicagoStylePepperoniPizza(); 7 | void cut(); 8 | virtual ~ChicagoStylePepperoniPizza(); 9 | }; 10 | 11 | #endif // CHICAGOSTYLEPEPPERONIPIZZA_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/ChicagoStyleVeggiePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGOSTYLEVEGGIEPIZZA_H 2 | #define CHICAGOSTYLEVEGGIEPIZZA_H 3 | #include "Pizza.h" 4 | class ChicagoStyleVeggiePizza:public Pizza{ 5 | public: 6 | ChicagoStyleVeggiePizza(); 7 | void cut(); 8 | virtual ~ChicagoStyleVeggiePizza(); 9 | }; 10 | 11 | #endif // CHICAGOSTYLEVEGGIEPIZZA_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef NYPIZZASTORE_H 2 | #define NYPIZZASTORE_H 3 | #include "PizzaStore.h" 4 | class NYPizzaStore:public PizzaStore{ 5 | public: 6 | virtual Pizza *createPizza(string item)override final; 7 | virtual ~NYPizzaStore(); 8 | }; 9 | 10 | #endif // NYPIZZASTORE_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStyleCheesePizza.cpp: -------------------------------------------------------------------------------- 1 | #include "NYStyleCheesePizza.h" 2 | 3 | NYStyleCheesePizza::NYStyleCheesePizza() 4 | { 5 | name = "NY Style Sauce and Cheese Pizza"; 6 | dough = "Thin Crust Dough"; 7 | sauce = "Marinara Sauce"; 8 | 9 | toppings.push_back("Grated Reggiano Cheese"); 10 | } 11 | 12 | NYStyleCheesePizza::~NYStyleCheesePizza() 13 | { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStyleCheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef NYSTYLECHEESEPIZZA_H 2 | #define NYSTYLECHEESEPIZZA_H 3 | #include "Pizza.h" 4 | class NYStyleCheesePizza:public Pizza{ 5 | public: 6 | NYStyleCheesePizza(); 7 | virtual ~NYStyleCheesePizza(); 8 | }; 9 | 10 | #endif // NYSTYLECHEESEPIZZA_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStyleClamPizza.cpp: -------------------------------------------------------------------------------- 1 | #include "NYStyleClamPizza.h" 2 | 3 | NYStyleClamPizza::NYStyleClamPizza() 4 | { 5 | name = "NY Style Clam Pizza"; 6 | dough = "Thin Crust Dough"; 7 | sauce = "Marinara Sauce"; 8 | 9 | toppings.push_back("Grated Reggiano Cheese"); 10 | toppings.push_back("Fresh Clams from Long Island Sound"); 11 | } 12 | 13 | NYStyleClamPizza::~NYStyleClamPizza() 14 | { 15 | 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStyleClamPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef NYSTYLECLAMPIZZA_H 2 | #define NYSTYLECLAMPIZZA_H 3 | #include "Pizza.h" 4 | class NYStyleClamPizza:public Pizza{ 5 | public: 6 | NYStyleClamPizza(); 7 | virtual ~NYStyleClamPizza(); 8 | }; 9 | 10 | #endif // NYSTYLECLAMPIZZA_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStylePepperoniPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef NYSTYLEPEPPERONIPIZZA_H 2 | #define NYSTYLEPEPPERONIPIZZA_H 3 | #include "Pizza.h" 4 | class NYStylePepperoniPizza:public Pizza{ 5 | public: 6 | NYStylePepperoniPizza(); 7 | virtual ~NYStylePepperoniPizza(); 8 | }; 9 | 10 | #endif // NYSTYLEPEPPERONIPIZZA_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/NYStyleVeggiePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef NYSTYLEVEGGIEPIZZA_H 2 | #define NYSTYLEVEGGIEPIZZA_H 3 | #include "Pizza.h" 4 | class NYStyleVeggiePizza:public Pizza{ 5 | public: 6 | NYStyleVeggiePizza(); 7 | virtual ~NYStyleVeggiePizza(); 8 | }; 9 | 10 | #endif // NYSTYLEVEGGIEPIZZA_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | #include "PizzaStore.h" 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | Pizza *PizzaStore::orderPizza(std::string type) 6 | { 7 | Pizza *pizza=createPizza(type); 8 | cout<<"--- Making a " + pizza->getName() + " ---"<prepare(); 10 | pizza->bake(); 11 | pizza->cut(); 12 | pizza->box(); 13 | 14 | return pizza; 15 | } 16 | 17 | PizzaStore::~PizzaStore() 18 | { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzafm/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZASTORE_H 2 | #define PIZZASTORE_H 3 | #include "Pizza.h" 4 | class PizzaStore{ 5 | public: 6 | virtual Pizza *createPizza(string item)=0; 7 | Pizza *orderPizza(string type); 8 | virtual ~PizzaStore(); 9 | }; 10 | #endif // PIZZASTORE_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/CheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESEPIZZA_H 2 | #define CHEESEPIZZA_H 3 | #include "Pizza.h" 4 | class CheesePizza:public Pizza{ 5 | public: 6 | CheesePizza(){ 7 | name = "Cheese Pizza"; 8 | dough = "Regular Crust"; 9 | sauce = "Marinara Pizza Sauce"; 10 | toppings.push_back("Fresh Mozzarella"); 11 | toppings.push_back("Parmesan"); 12 | } 13 | virtual ~CheesePizza(){}; 14 | }; 15 | 16 | #endif // CHEESEPIZZA_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/ClamPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CLAMPIZZA_H 2 | #define CLAMPIZZA_H 3 | #include "Pizza.h" 4 | class ClamPizza:public Pizza{ 5 | public: 6 | ClamPizza(){ 7 | name = "Clam Pizza"; 8 | dough = "Thin crust"; 9 | sauce = "White garlic sauce"; 10 | toppings.push_back("Clams"); 11 | toppings.push_back("Grated parmesan cheese"); 12 | } 13 | virtual ~ClamPizza(){}; 14 | }; 15 | 16 | #endif // CLAMPIZZA_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/Pizza.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | #include 4 | #include 5 | using std::string; 6 | using std::vector; 7 | class Pizza{ 8 | public: 9 | string name; 10 | string dough; 11 | string sauce; 12 | vector toppings; 13 | string getName(); 14 | void prepare(); 15 | void bake(); 16 | void cut(); 17 | void box(); 18 | string toString(); 19 | virtual ~Pizza(); 20 | }; 21 | 22 | #endif // PIZZA_H 23 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/PizzaStore.cpp: -------------------------------------------------------------------------------- 1 | #include "PizzaStore.h" 2 | 3 | PizzaStore::PizzaStore(SimplePizzaFactory *factory) 4 | { 5 | this->factory=factory; 6 | } 7 | 8 | Pizza *PizzaStore::orderPizza(std::string type) 9 | { 10 | Pizza *pizza; 11 | pizza=factory->createPizza(type); 12 | 13 | pizza->prepare(); 14 | pizza->bake(); 15 | pizza->cut(); 16 | pizza->box(); 17 | 18 | return pizza; 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZASTORE_H 2 | #define PIZZASTORE_H 3 | #include "SimplePizzaFactory.h" 4 | class PizzaStore{ 5 | public: 6 | SimplePizzaFactory *factory; 7 | PizzaStore(SimplePizzaFactory *factory); 8 | Pizza *orderPizza(string type); 9 | }; 10 | 11 | #endif // PIZZASTORE_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/SimplePizzaFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLEPIZZAFACTORY_H 2 | #define SIMPLEPIZZAFACTORY_H 3 | #include "Pizza.h" 4 | class SimplePizzaFactory{ 5 | public: 6 | Pizza *createPizza(string type); 7 | }; 8 | 9 | #endif // SIMPLEPIZZAFACTORY_H 10 | -------------------------------------------------------------------------------- /HeadFirst-c++/factor/pizzas/pizzas.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | Pizza.cpp \ 8 | PizzaStore.cpp \ 9 | SimplePizzaFactory.cpp \ 10 | main.cpp 11 | 12 | HEADERS += \ 13 | CheesePizza.h \ 14 | ClamPizza.h \ 15 | PepperoniPizza.h \ 16 | Pizza.h \ 17 | PizzaStore.h \ 18 | SimplePizzaFactory.h \ 19 | VeggiePizza.h 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/ArrayIterator.cpp: -------------------------------------------------------------------------------- 1 | #include "ArrayIterator.h" 2 | 3 | ArrayIterator::ArrayIterator(MenuItem items[], int size) 4 | { 5 | this->items=items; 6 | this->size=size; 7 | } 8 | 9 | MenuItem *ArrayIterator::next() 10 | { 11 | return &items[position++]; 12 | } 13 | 14 | bool ArrayIterator::hasNext() 15 | { 16 | if(position>=size)return false; 17 | else return true; 18 | } 19 | 20 | ArrayIterator::~ArrayIterator() 21 | { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/ArrayIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ARRAYITERATOR_H 2 | #define ARRAYITERATOR_H 3 | #include "Iterator.h" 4 | #include "MenuItem.h" 5 | class ArrayIterator:public Iterator{ 6 | public: 7 | ArrayIterator(MenuItem items[],int size); 8 | virtual MenuItem *next()override final; 9 | virtual bool hasNext()override final; 10 | virtual ~ArrayIterator(); 11 | private: 12 | MenuItem *items; 13 | int position=0; 14 | int size=0; 15 | }; 16 | 17 | #endif // ARRAYITERATOR_H 18 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | #include "MenuItem.h" 4 | class Iterator{ 5 | public: 6 | virtual bool hasNext()=0; 7 | virtual MenuItem *next()=0; 8 | virtual ~Iterator(){}; 9 | }; 10 | 11 | #endif // ITERATOR_H 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | #include "Iterator.h" 4 | class Menu{ 5 | public: 6 | virtual Iterator *createIterator()=0; 7 | virtual ~Menu(){}; 8 | }; 9 | 10 | #endif // MENU_H 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENUITEM_H 2 | #define MENUITEM_H 3 | #include 4 | using std::string; 5 | class MenuItem{ 6 | public: 7 | string name; 8 | string description; 9 | bool vegetarian; 10 | double price; 11 | MenuItem(); 12 | MenuItem(string name,string description,bool vegetarian,double price); 13 | string getName(); 14 | string getDescription(); 15 | double getPrice(); 16 | bool isVegetarian(); 17 | string toString(); 18 | }; 19 | 20 | #endif // MENUITEM_H 21 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/VectorIterator.cpp: -------------------------------------------------------------------------------- 1 | #include "VectorIterator.h" 2 | 3 | VectorIterator::VectorIterator(vector items) 4 | { 5 | this->items=items; 6 | } 7 | 8 | MenuItem *VectorIterator::next() 9 | { 10 | return items[position++]; 11 | } 12 | 13 | bool VectorIterator::hasNext() 14 | { 15 | if(position>=int(items.size())){ 16 | return false; 17 | }else{ 18 | return true; 19 | } 20 | } 21 | 22 | VectorIterator::~VectorIterator() 23 | { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /HeadFirst-c++/iterator/dinermerger/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Waitress.h" 3 | #include "PancakeHouseMenu.h" 4 | #include "DinerMenu.h" 5 | #include "Menu.h" 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | Menu *pancakeHouseMenu = new PancakeHouseMenu(); 11 | Menu *dinerMenu = new DinerMenu(); 12 | Waitress *waitress = new Waitress(pancakeHouseMenu, dinerMenu); 13 | waitress->printMenu(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/observer/DisplayElement.h: -------------------------------------------------------------------------------- 1 | #ifndef DISPLAYELEMENT_H 2 | #define DISPLAYELEMENT_H 3 | class DisplayElement{ 4 | public: 5 | virtual void display()=0; 6 | virtual ~DisplayElement(){}; 7 | }; 8 | 9 | #endif // DISPLAYELEMENT_H 10 | -------------------------------------------------------------------------------- /HeadFirst-c++/observer/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef OBSERVER_H 2 | #define OBSERVER_H 3 | class Observer{ 4 | public: 5 | virtual void update(float temperature,float humidity,float pressure)=0; 6 | virtual ~Observer(){}; 7 | }; 8 | 9 | #endif // OBSERVER_H 10 | -------------------------------------------------------------------------------- /HeadFirst-c++/observer/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBJECT_H 2 | #define SUBJECT_H 3 | #include "Observer.h" 4 | class Subject{ 5 | public: 6 | virtual void registerObserver(Observer *o)=0; 7 | virtual void removeObserver(Observer *o)=0; 8 | virtual void notifyObservers()=0; 9 | virtual ~Subject(){}; 10 | }; 11 | 12 | #endif // SUBJECT_H 13 | -------------------------------------------------------------------------------- /HeadFirst-c++/proxy/example/Proxy.cpp: -------------------------------------------------------------------------------- 1 | #include "Proxy.h" 2 | Proxy::Proxy() 3 | { 4 | subject=new RealSubject(); 5 | } 6 | 7 | void Proxy::request() 8 | { 9 | subject->request(); 10 | } 11 | 12 | Proxy::~Proxy() 13 | { 14 | delete subject; 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/proxy/example/Proxy.h: -------------------------------------------------------------------------------- 1 | #ifndef PROXY_H 2 | #define PROXY_H 3 | #include "Subject.h" 4 | #include "RealSubject.h" 5 | class Proxy:public Subject{ 6 | public: 7 | Proxy(); 8 | virtual void request()override final; 9 | virtual ~Proxy(); 10 | private: 11 | Subject *subject; 12 | }; 13 | 14 | #endif // PROXY_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/proxy/example/RealSubject.cpp: -------------------------------------------------------------------------------- 1 | #include "RealSubject.h" 2 | #include 3 | 4 | void RealSubject::request() 5 | { 6 | std::cout<<"function request"< 2 | #include "Proxy.h" 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | Proxy proxy; 8 | proxy.request(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/chocolate/ChocolateBoiler.h: -------------------------------------------------------------------------------- 1 | #ifndef CHOCOLATEBOILER_H 2 | #define CHOCOLATEBOILER_H 3 | class ChocolateBoiler{ 4 | public: 5 | static ChocolateBoiler *getInstance(); 6 | void fill(); 7 | void drain(); 8 | void boil(); 9 | bool isEmpty(); 10 | bool isBoiled(); 11 | private: 12 | ChocolateBoiler(); 13 | bool empty; 14 | bool boiled; 15 | static ChocolateBoiler *uniqueInstance; 16 | 17 | }; 18 | #endif // CHOCOLATEBOILER_H 19 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/chocolate/chocolate.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | ChocolateBoiler.cpp \ 8 | main.cpp 9 | 10 | HEADERS += \ 11 | ChocolateBoiler.h 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/chocolate/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ChocolateBoiler.h" 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | ChocolateBoiler *boiler = ChocolateBoiler::getInstance(); 8 | boiler->fill(); 9 | boiler->boil(); 10 | boiler->drain(); 11 | 12 | // will return the existing instance 13 | //ChocolateBoiler *boiler2 = ChocolateBoiler::getInstance(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/classic/Singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | #include 4 | using std::string; 5 | class Singleton{ 6 | public: 7 | static Singleton *getInstance(); 8 | string getDescription(); 9 | private: 10 | Singleton(); 11 | static Singleton *uniqueInstance; 12 | }; 13 | 14 | #endif // SINGLETON_H 15 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/classic/classic.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | Singleton.cpp \ 8 | main.cpp 9 | 10 | HEADERS += \ 11 | Singleton.h 12 | -------------------------------------------------------------------------------- /HeadFirst-c++/singleton/classic/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Singleton.h" 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | Singleton *singleton = Singleton::getInstance(); 8 | cout<getDescription()< 4 | #include 5 | using std::string; 6 | using std::cout; 7 | using std::endl; 8 | class State{ 9 | public: 10 | virtual void insertQuarter()=0; 11 | virtual void ejectQuarter()=0; 12 | virtual void turnCrank()=0; 13 | virtual void dispense()=0; 14 | virtual void refill()=0; 15 | virtual string toString()=0; 16 | virtual ~State(){}; 17 | }; 18 | 19 | #endif // STATE_H 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/state/gumballstate/gumballstate.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += \ 7 | GumballMachine.cpp \ 8 | HasQuarterState.cpp \ 9 | NoQuarterState.cpp \ 10 | SoldOutState.cpp \ 11 | SoldState.cpp \ 12 | main.cpp 13 | 14 | HEADERS += \ 15 | GumballMachine.h \ 16 | HasQuarterState.h \ 17 | NoQuarterState.h \ 18 | SoldOutState.h \ 19 | SoldState.h \ 20 | State.h 21 | -------------------------------------------------------------------------------- /HeadFirst-c++/state/gumballstatewinner/State.h: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H 2 | #define STATE_H 3 | #include 4 | #include 5 | using std::string; 6 | using std::cout; 7 | using std::endl; 8 | class State{ 9 | public: 10 | virtual void insertQuarter()=0; 11 | virtual void ejectQuarter()=0; 12 | virtual void turnCrank()=0; 13 | virtual void dispense()=0; 14 | virtual void refill()=0; 15 | virtual string toString()=0; 16 | virtual ~State(){}; 17 | }; 18 | 19 | #endif // STATE_H 20 | -------------------------------------------------------------------------------- /HeadFirst-c++/strategy/DecoyDuck.cpp: -------------------------------------------------------------------------------- 1 | #include "DecoyDuck.h" 2 | #include "FlyNoWay.h" 3 | #include "MuteQuack.h" 4 | #include 5 | using std::cout; 6 | using std::endl; 7 | 8 | DecoyDuck::DecoyDuck() 9 | { 10 | cout<<"function DecoyDuck"< 3 | using std::cout; 4 | using std::endl; 5 | void FlyNoWay::fly() 6 | { 7 | cout<<"I can't fly"< 3 | using std::cout; 4 | using std::endl; 5 | void FlyRocketPowered::fly() 6 | { 7 | cout<<"I'm flying with a rocket"< 3 | using std::cout; 4 | using std::endl; 5 | 6 | 7 | void FlyWithWings::fly() 8 | { 9 | cout<<"I'm flying!!"< 5 | using std::cout; 6 | using std::endl; 7 | MallardDuck::MallardDuck() 8 | { 9 | cout<<"function MallardDuck"< 5 | using std::cout; 6 | using std::endl; 7 | 8 | ModelDuck::ModelDuck() 9 | { 10 | cout<<"function ModelDuck"< 3 | using std::cout; 4 | using std::endl; 5 | void MuteQuack::quack() 6 | { 7 | cout<<"<< Silence >>"< 3 | using std::cout; 4 | using std::endl; 5 | 6 | void Quack::quack() 7 | { 8 | cout<<"Quack"< 3 | using std::cout; 4 | using std::endl; 5 | 6 | void Squeak::quack() 7 | { 8 | cout<<"Squeak"< 4 | using std::cout; 5 | using std::endl; 6 | class CaffeineBeverage{ 7 | public: 8 | void prepareRecipe(); 9 | virtual void brew()=0; 10 | virtual void addCondiments()=0; 11 | void boilWater(); 12 | void pourInCup(); 13 | virtual ~CaffeineBeverage(); 14 | }; 15 | 16 | #endif // CAFFEINEBEVERAGE_H 17 | -------------------------------------------------------------------------------- /HeadFirst-c++/templatemethod/barista/Coffee.cpp: -------------------------------------------------------------------------------- 1 | #include "Coffee.h" 2 | 3 | void Coffee::brew() 4 | { 5 | cout<<"Dripping Coffee through filter"< 5 | using std::string; 6 | class CoffeeWithHook:public CaffeineBeverageWithHook{ 7 | public: 8 | virtual void brew()override final; 9 | virtual void addCondiments()override final; 10 | virtual bool customerWantsCondiments()override final; 11 | private: 12 | string getUserInput(); 13 | }; 14 | 15 | #endif // COFFEEWITHHOOK_H 16 | -------------------------------------------------------------------------------- /HeadFirst-c++/templatemethod/barista/Tea.cpp: -------------------------------------------------------------------------------- 1 | #include "Tea.h" 2 | 3 | void Tea::brew() 4 | { 5 | cout<<"Steeping the tea"< 5 | using std::string; 6 | class TeaWithHook:public CaffeineBeverageWithHook{ 7 | public: 8 | virtual void brew()override final; 9 | virtual void addCondiments()override final; 10 | virtual bool customerWantsCondiments()override final; 11 | private: 12 | string getUserInput(); 13 | }; 14 | 15 | #endif // TEAWITHHOOK_H 16 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/Duck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public interface Duck { 4 | public void quack(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/DuckAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | import java.util.Random; 3 | 4 | public class DuckAdapter implements Turkey { 5 | Duck duck; 6 | Random rand; 7 | 8 | public DuckAdapter(Duck duck) { 9 | this.duck = duck; 10 | rand = new Random(); 11 | } 12 | 13 | public void gobble() { 14 | duck.quack(); 15 | } 16 | 17 | public void fly() { 18 | if (rand.nextInt(5) == 0) { 19 | duck.fly(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public class MallardDuck implements Duck { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/Turkey.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public interface Turkey { 4 | public void gobble(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/TurkeyAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public class TurkeyAdapter implements Duck { 4 | Turkey turkey; 5 | 6 | public TurkeyAdapter(Turkey turkey) { 7 | this.turkey = turkey; 8 | } 9 | 10 | public void quack() { 11 | turkey.gobble(); 12 | } 13 | 14 | public void fly() { 15 | for(int i=0; i < 5; i++) { 16 | turkey.fly(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/TurkeyTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public class TurkeyTestDrive { 4 | public static void main(String[] args) { 5 | MallardDuck duck = new MallardDuck(); 6 | Turkey duckAdapter = new DuckAdapter(duck); 7 | 8 | for(int i=0;i<10;i++) { 9 | System.out.println("The DuckAdapter says..."); 10 | duckAdapter.gobble(); 11 | duckAdapter.fly(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/WildTurkey.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks; 2 | 3 | public class WildTurkey implements Turkey { 4 | public void gobble() { 5 | System.out.println("Gobble gobble"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying a short distance"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/challenge/Drone.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks.challenge; 2 | 3 | public interface Drone { 4 | public void beep(); 5 | public void spin_rotors(); 6 | public void take_off(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/challenge/DroneAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks.challenge; 2 | 3 | import headfirst.designpatterns.adapter.ducks.Duck; 4 | 5 | public class DroneAdapter implements Duck { 6 | Drone drone; 7 | 8 | public DroneAdapter(Drone drone) { 9 | this.drone = drone; 10 | } 11 | 12 | public void quack() { 13 | drone.beep(); 14 | } 15 | 16 | public void fly() { 17 | drone.spin_rotors(); 18 | drone.take_off(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/ducks/challenge/SuperDrone.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.ducks.challenge; 2 | 3 | public class SuperDrone implements Drone { 4 | public void beep() { 5 | System.out.println("Beep beep beep"); 6 | } 7 | public void spin_rotors() { 8 | System.out.println("Rotors are spinning"); 9 | } 10 | public void take_off() { 11 | System.out.println("Taking off"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/iterenum/EnumerationIteratorTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class EnumerationIteratorTestDrive { 6 | public static void main (String args[]) { 7 | Vector v = new Vector(Arrays.asList(args)); 8 | Iterator iterator = new EnumerationIterator(v.elements()); 9 | while (iterator.hasNext()) { 10 | System.out.println(iterator.next()); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/iterenum/IteratorEnumeration.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class IteratorEnumeration implements Enumeration { 6 | Iterator iterator; 7 | 8 | public IteratorEnumeration(Iterator iterator) { 9 | this.iterator = iterator; 10 | } 11 | 12 | public boolean hasMoreElements() { 13 | return iterator.hasNext(); 14 | } 15 | 16 | public Object nextElement() { 17 | return iterator.next(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/adapter/iterenum/IteratorEnumerationTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.adapter.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class IteratorEnumerationTestDrive { 6 | public static void main (String args[]) { 7 | ArrayList l = new ArrayList(Arrays.asList(args)); 8 | Enumeration enumeration = new IteratorEnumeration(l.iterator()); 9 | while (enumeration.hasMoreElements()) { 10 | System.out.println(enumeration.nextElement()); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/GenericRemote.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public class GenericRemote extends RemoteControl { 4 | public GenericRemote(TVFactory tvFactory) { 5 | super(tvFactory); 6 | } 7 | public void nextChannel() { 8 | int channel = this.getChannel(); 9 | this.setChannel(channel+1); 10 | } 11 | public void prevChannel() { 12 | int channel = this.getChannel(); 13 | this.setChannel(channel-1); 14 | } 15 | } -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/LG.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public class LG extends TV { 4 | int channel = 1; 5 | public void on() { 6 | System.out.println("Turning on the LG TV"); 7 | } 8 | public void off() { 9 | System.out.println("Turning off the LG TV"); 10 | } 11 | public void tuneChannel(int channel) { 12 | this.channel = channel; 13 | System.out.println("Set the LG TV Channel to " + this.channel); 14 | } 15 | public int getChannel() { 16 | return channel; 17 | } 18 | } -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/Sony.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public class Sony extends TV { 4 | int station = 0; 5 | public void on() { 6 | System.out.println("Turning on the Sony TV"); 7 | } 8 | public void off() { 9 | System.out.println("Turning off the Sony TV"); 10 | } 11 | public void tuneChannel(int channel) { 12 | this.station = channel; 13 | System.out.println("Set the Sony TV station to " + this.station); 14 | } 15 | public int getChannel() { 16 | return station; 17 | } 18 | } -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/SpecialRemote.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public class SpecialRemote extends RemoteControl { 4 | public SpecialRemote(TVFactory tvFactory) { 5 | super(tvFactory); 6 | } 7 | public void up() { 8 | int channel = this.getChannel(); 9 | this.setChannel(channel+1); 10 | } 11 | public void down() { 12 | int channel = this.getChannel(); 13 | this.setChannel(channel-1); 14 | } 15 | } -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/TV.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public abstract class TV { 4 | public abstract void on(); 5 | public abstract void off(); 6 | public abstract void tuneChannel(int channel); 7 | public abstract int getChannel(); 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/bridge/remote/TVFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.bridge.remote; 2 | 3 | public class TVFactory { 4 | public TV getTV(String type) throws Exception { 5 | if (type.equals("LG")) { 6 | return new LG(); 7 | } else if (type.equals("Sony")) { 8 | return new Sony(); 9 | } else { 10 | throw new Exception("Invalid TV Type"); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /HeadFirst-java/builder/house/InteriorWall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.builder.house; 2 | 3 | public class InteriorWall extends Wall { 4 | String name; 5 | String material; 6 | 7 | public InteriorWall(String material) { 8 | super(material); 9 | this.name = "Interior wall made out of " + material; 10 | } 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | public String toString() { 15 | return this.name; 16 | } 17 | } 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /HeadFirst-java/builder/house/Roof.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.builder.house; 2 | 3 | public class Roof { 4 | String name; 5 | String material; 6 | 7 | public Roof(String material) { 8 | this.name = "Roof made out of " + material; 9 | } 10 | public void setName(String name) { 11 | this.name = name; 12 | } 13 | public String toString() { 14 | return this.name; 15 | } 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /HeadFirst-java/builder/house/Wall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.builder.house; 2 | 3 | public class Wall { 4 | String name; 5 | String material; 6 | 7 | public Wall(String material) { 8 | this.name = "Wall made out of " + material; 9 | } 10 | public void setName(String name) { 11 | this.name = name; 12 | } 13 | public String toString() { 14 | return this.name; 15 | } 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /HeadFirst-java/builder/house/Window.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.builder.house; 2 | 3 | public class Window { 4 | String name; 5 | String material; 6 | 7 | public Window(String material) { 8 | this.name = "Window made out of " + material; 9 | } 10 | public void setName(String name) { 11 | this.name = name; 12 | } 13 | public String toString() { 14 | return this.name; 15 | } 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /HeadFirst-java/collections/iterator/Iterator.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.collections.iterator; 2 | 3 | public interface Iterator { 4 | boolean hasNext(); 5 | Object next(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/collections/iterator/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.collections.iterator; 2 | 3 | public interface Menu { 4 | public Iterator createIterator(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/collections/iterator_builtin/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.collections.iterator_builtin; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/BPMObserver.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public interface BPMObserver { 4 | void updateBPM(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/BeatModelInterface.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public interface BeatModelInterface { 4 | void initialize(); 5 | 6 | void on(); 7 | 8 | void off(); 9 | 10 | void setBPM(int bpm); 11 | 12 | int getBPM(); 13 | 14 | void registerObserver(BeatObserver o); 15 | 16 | void removeObserver(BeatObserver o); 17 | 18 | void registerObserver(BPMObserver o); 19 | 20 | void removeObserver(BPMObserver o); 21 | } 22 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/BeatObserver.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public interface BeatObserver { 4 | void updateBeat(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/ControllerInterface.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public interface ControllerInterface { 4 | void start(); 5 | void stop(); 6 | void increaseBPM(); 7 | void decreaseBPM(); 8 | void setBPM(int bpm); 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/DJTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public class DJTestDrive { 4 | 5 | public static void main (String[] args) { 6 | BeatModelInterface model = new BeatModel(); 7 | ControllerInterface controller = new BeatController(model); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/HeartModelInterface.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public interface HeartModelInterface { 4 | int getHeartRate(); 5 | void registerObserver(BeatObserver o); 6 | void removeObserver(BeatObserver o); 7 | void registerObserver(BPMObserver o); 8 | void removeObserver(BPMObserver o); 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/HeartTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combined.djview; 2 | 3 | public class HeartTestDrive { 4 | 5 | public static void main (String[] args) { 6 | HeartModel heartModel = new HeartModel(); 7 | ControllerInterface model = new HeartController(heartModel); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combined/djview/jsp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DJView Web Application 6 | 7 | 8 | DJView 9 | 10 | 11 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class DecoyDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("<< Silence >>"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/DuckCall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class DuckCall implements Quackable { 4 | public void quack() { 5 | System.out.println("Kwak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/Goose.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class Goose { 4 | public void honk() { 5 | System.out.println("Honk"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/GooseAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class GooseAdapter implements Quackable { 4 | Goose goose; 5 | 6 | public GooseAdapter(Goose goose) { 7 | this.goose = goose; 8 | } 9 | 10 | public void quack() { 11 | goose.honk(); 12 | } 13 | 14 | public String toString() { 15 | return "Goose pretending to be a Duck"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class MallardDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public interface Quackable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class RedheadDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/adapter/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.adapter; 2 | 3 | public class RubberDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Squeak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/AbstractDuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public abstract class AbstractDuckFactory { 4 | 5 | public abstract Quackable createMallardDuck(); 6 | public abstract Quackable createRedheadDuck(); 7 | public abstract Quackable createDuckCall(); 8 | public abstract Quackable createRubberDuck(); 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class DecoyDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("<< Silence >>"); 7 | } 8 | 9 | public String toString() { 10 | return "Decoy Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/DuckCall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class DuckCall implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Kwak"); 7 | } 8 | 9 | public String toString() { 10 | return "Duck Call"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/DuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class DuckFactory extends AbstractDuckFactory { 4 | 5 | public Quackable createMallardDuck() { 6 | return new MallardDuck(); 7 | } 8 | 9 | public Quackable createRedheadDuck() { 10 | return new RedheadDuck(); 11 | } 12 | 13 | public Quackable createDuckCall() { 14 | return new DuckCall(); 15 | } 16 | 17 | public Quackable createRubberDuck() { 18 | return new RubberDuck(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/Goose.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class Goose { 4 | public void honk() { 5 | System.out.println("Honk"); 6 | } 7 | 8 | public String toString() { 9 | return "Goose"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/GooseAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class GooseAdapter implements Quackable { 4 | Goose goose; 5 | 6 | public GooseAdapter(Goose goose) { 7 | this.goose = goose; 8 | } 9 | 10 | public void quack() { 11 | goose.honk(); 12 | } 13 | 14 | public String toString() { 15 | return "Goose pretending to be a Duck"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class MallardDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Quack"); 7 | } 8 | 9 | public String toString() { 10 | return "Mallard Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public interface Quackable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class RedheadDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | 8 | public String toString() { 9 | return "Redhead Duck"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/composite/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.composite; 2 | 3 | public class RubberDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Squeak"); 7 | } 8 | 9 | public String toString() { 10 | return "Rubber Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class DecoyDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("<< Silence >>"); 7 | } 8 | 9 | public String toString() { 10 | return "Decoy Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/DuckCall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class DuckCall implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Kwak"); 7 | } 8 | 9 | public String toString() { 10 | return "Duck Call"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/Goose.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class Goose { 4 | public void honk() { 5 | System.out.println("Honk"); 6 | } 7 | 8 | public String toString() { 9 | return "Goose"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/GooseAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class GooseAdapter implements Quackable { 4 | Goose goose; 5 | 6 | public GooseAdapter(Goose goose) { 7 | this.goose = goose; 8 | } 9 | 10 | public void quack() { 11 | goose.honk(); 12 | } 13 | 14 | public String toString() { 15 | return "Goose pretending to be a Duck"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class MallardDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Quack"); 7 | } 8 | 9 | public String toString() { 10 | return "Mallard Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public interface Quackable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class RedheadDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/decorator/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.decorator; 2 | 3 | public class RubberDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Squeak"); 7 | } 8 | 9 | public String toString() { 10 | return "Rubber Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public class DecoyDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("<< Silence >>"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/DuckCall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public class DuckCall implements Quackable { 4 | public void quack() { 5 | System.out.println("Kwak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public class MallardDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public interface Quackable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public class RedheadDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/ducks/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.ducks; 2 | 3 | public class RubberDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Squeak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/AbstractDuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public abstract class AbstractDuckFactory { 4 | 5 | public abstract Quackable createMallardDuck(); 6 | public abstract Quackable createRedheadDuck(); 7 | public abstract Quackable createDuckCall(); 8 | public abstract Quackable createRubberDuck(); 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class DecoyDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("<< Silence >>"); 7 | } 8 | 9 | public String toString() { 10 | return "Decoy Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/DuckCall.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class DuckCall implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Kwak"); 7 | } 8 | 9 | public String toString() { 10 | return "Duck Call"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/DuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class DuckFactory extends AbstractDuckFactory { 4 | 5 | public Quackable createMallardDuck() { 6 | return new MallardDuck(); 7 | } 8 | 9 | public Quackable createRedheadDuck() { 10 | return new RedheadDuck(); 11 | } 12 | 13 | public Quackable createDuckCall() { 14 | return new DuckCall(); 15 | } 16 | 17 | public Quackable createRubberDuck() { 18 | return new RubberDuck(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/Goose.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class Goose { 4 | public void honk() { 5 | System.out.println("Honk"); 6 | } 7 | 8 | public String toString() { 9 | return "Goose"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/GooseAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class GooseAdapter implements Quackable { 4 | Goose goose; 5 | 6 | public GooseAdapter(Goose goose) { 7 | this.goose = goose; 8 | } 9 | 10 | public void quack() { 11 | goose.honk(); 12 | } 13 | 14 | public String toString() { 15 | return "Goose pretending to be a Duck"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class MallardDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Quack"); 7 | } 8 | 9 | public String toString() { 10 | return "Mallard Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public interface Quackable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class RedheadDuck implements Quackable { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/factory/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.factory; 2 | 3 | public class RubberDuck implements Quackable { 4 | 5 | public void quack() { 6 | System.out.println("Squeak"); 7 | } 8 | 9 | public String toString() { 10 | return "Rubber Duck"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/.QuackDecorator.java.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HelloZhan/HeadFirst/2ee6a0f4a291347c8e4cd15122ad58ca61bb8121/HeadFirst-java/combining/observer/.QuackDecorator.java.swp -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/AbstractDuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public abstract class AbstractDuckFactory { 4 | 5 | public abstract Quackable createMallardDuck(); 6 | public abstract Quackable createRedheadDuck(); 7 | public abstract Quackable createDuckCall(); 8 | public abstract Quackable createRubberDuck(); 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/DuckFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public class DuckFactory extends AbstractDuckFactory { 4 | 5 | public Quackable createMallardDuck() { 6 | return new MallardDuck(); 7 | } 8 | 9 | public Quackable createRedheadDuck() { 10 | return new RedheadDuck(); 11 | } 12 | 13 | public Quackable createDuckCall() { 14 | return new DuckCall(); 15 | } 16 | 17 | public Quackable createRubberDuck() { 18 | return new RubberDuck(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/Goose.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public class Goose { 4 | 5 | public void honk() { 6 | System.out.println("Honk"); 7 | } 8 | 9 | public String toString() { 10 | return "Goose"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/Observer.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public interface Observer { 4 | public void update(QuackObservable duck); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/QuackObservable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public interface QuackObservable { 4 | public void registerObserver(Observer observer); 5 | public void notifyObservers(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/Quackable.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public interface Quackable extends QuackObservable { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/combining/observer/Quackologist.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.combining.observer; 2 | 3 | public class Quackologist implements Observer { 4 | 5 | public void update(QuackObservable duck) { 6 | System.out.println("Quackologist: " + duck + " just quacked."); 7 | } 8 | 9 | public String toString() { 10 | return "Quackologist"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/BurgerAndFriesOrder.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | public class BurgerAndFriesOrder implements Order { 4 | Cook cook; 5 | public BurgerAndFriesOrder(Cook cook) { 6 | this.cook = cook; 7 | } 8 | public void orderUp() { 9 | cook.makeBurger(); 10 | cook.makeFries(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/Cook.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | public class Cook { 4 | 5 | public Cook() {} 6 | 7 | public void makeBurger() { 8 | System.out.println("Making a burger"); 9 | } 10 | 11 | public void makeFries() { 12 | System.out.println("Making fries"); 13 | } 14 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/Customer.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | public class Customer { 4 | Waitress waitress; 5 | Order order; 6 | public Customer(Waitress waitress) { 7 | this.waitress = waitress; 8 | } 9 | public void createOrder(Order order) { 10 | this.order = order; 11 | } 12 | public void hungry() { 13 | waitress.takeOrder(order); 14 | } 15 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/Diner.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | public class Diner { 4 | public static void main(String[] args) { 5 | Cook cook = new Cook(); 6 | Waitress waitress = new Waitress(); 7 | Customer customer = new Customer(waitress); 8 | customer.createOrder(new BurgerAndFriesOrder(cook)); 9 | customer.hungry(); 10 | } 11 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/Order.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | @FunctionalInterface 4 | public interface Order { 5 | public void orderUp(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/diner/Waitress.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.diner; 2 | 3 | public class Waitress { 4 | Order order; 5 | public Waitress() {} 6 | public void takeOrder(Order order) { 7 | this.order = order; 8 | order.orderUp(); 9 | } 10 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/dinerLambda/Cook.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.dinerLambda; 2 | 3 | public class Cook { 4 | 5 | public Cook() {} 6 | 7 | public void makeBurger() { 8 | System.out.println("Making a burger"); 9 | } 10 | 11 | public void makeFries() { 12 | System.out.println("Making fries"); 13 | } 14 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/dinerLambda/Customer.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.dinerLambda; 2 | 3 | public class Customer { 4 | Waitress waitress; 5 | Cook cook; 6 | Order o; 7 | public Customer(Waitress waitress, Cook cook) { 8 | this.waitress = waitress; 9 | this.cook = cook; 10 | } 11 | public void createOrder() { 12 | Order o = () -> { cook.makeBurger(); cook.makeFries(); }; 13 | } 14 | public void hungry() { 15 | waitress.takeOrder(o); 16 | } 17 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/dinerLambda/Diner.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.dinerLambda; 2 | 3 | public class Diner { 4 | public static void main(String[] args) { 5 | Cook cook = new Cook(); 6 | Waitress waitress = new Waitress(); 7 | Customer customer = new Customer(waitress, cook); 8 | customer.createOrder(); 9 | customer.hungry(); 10 | } 11 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/dinerLambda/Order.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.dinerLambda; 2 | 3 | @FunctionalInterface 4 | public interface Order { 5 | public void orderUp(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/dinerLambda/Waitress.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.dinerLambda; 2 | 3 | public class Waitress { 4 | Order order; 5 | public Waitress() {} 6 | public void takeOrder(Order order) { 7 | this.order = order; 8 | order.orderUp(); 9 | } 10 | } -------------------------------------------------------------------------------- /HeadFirst-java/command/party/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public interface Command { 4 | public void execute(); 5 | public void undo(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/HottubOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class HottubOffCommand implements Command { 4 | Hottub hottub; 5 | 6 | public HottubOffCommand(Hottub hottub) { 7 | this.hottub = hottub; 8 | } 9 | 10 | public void execute() { 11 | hottub.setTemperature(98); 12 | hottub.off(); 13 | } 14 | public void undo() { 15 | hottub.on(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/HottubOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class HottubOnCommand implements Command { 4 | Hottub hottub; 5 | 6 | public HottubOnCommand(Hottub hottub) { 7 | this.hottub = hottub; 8 | } 9 | public void execute() { 10 | hottub.on(); 11 | hottub.setTemperature(104); 12 | hottub.circulate(); 13 | } 14 | public void undo() { 15 | hottub.off(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class LightOffCommand implements Command { 4 | Light light; 5 | 6 | public LightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.off(); 12 | } 13 | 14 | public void undo() { 15 | light.on(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class LightOnCommand implements Command { 4 | Light light; 5 | 6 | public LightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.on(); 12 | } 13 | 14 | public void undo() { 15 | light.off(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/LivingroomLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class LivingroomLightOffCommand implements Command { 4 | Light light; 5 | 6 | public LivingroomLightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | public void execute() { 10 | light.off(); 11 | } 12 | public void undo() { 13 | light.on(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/LivingroomLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class LivingroomLightOnCommand implements Command { 4 | Light light; 5 | 6 | public LivingroomLightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | public void execute() { 10 | light.on(); 11 | } 12 | public void undo() { 13 | light.off(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/NoCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | public void undo() { } 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/StereoOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class StereoOffCommand implements Command { 4 | Stereo stereo; 5 | 6 | public StereoOffCommand(Stereo stereo) { 7 | this.stereo = stereo; 8 | } 9 | 10 | public void execute() { 11 | stereo.off(); 12 | } 13 | 14 | public void undo() { 15 | stereo.on(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/StereoOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class StereoOnCommand implements Command { 4 | Stereo stereo; 5 | 6 | public StereoOnCommand(Stereo stereo) { 7 | this.stereo = stereo; 8 | } 9 | 10 | public void execute() { 11 | stereo.on(); 12 | } 13 | 14 | public void undo() { 15 | stereo.off(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/StereoOnWithCDCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class StereoOnWithCDCommand implements Command { 4 | Stereo stereo; 5 | 6 | public StereoOnWithCDCommand(Stereo stereo) { 7 | this.stereo = stereo; 8 | } 9 | 10 | public void execute() { 11 | stereo.on(); 12 | stereo.setCD(); 13 | stereo.setVolume(11); 14 | } 15 | 16 | public void undo() { 17 | stereo.off(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/TVOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class TVOffCommand implements Command { 4 | TV tv; 5 | 6 | public TVOffCommand(TV tv) { 7 | this.tv= tv; 8 | } 9 | 10 | public void execute() { 11 | tv.off(); 12 | } 13 | 14 | public void undo() { 15 | tv.on(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/party/TVOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.party; 2 | 3 | public class TVOnCommand implements Command { 4 | TV tv; 5 | 6 | public TVOnCommand(TV tv) { 7 | this.tv= tv; 8 | } 9 | 10 | public void execute() { 11 | tv.on(); 12 | tv.setInputChannel(); 13 | } 14 | 15 | public void undo() { 16 | tv.off(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/CeilingFanOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class CeilingFanOffCommand implements Command { 4 | CeilingFan ceilingFan; 5 | 6 | public CeilingFanOffCommand(CeilingFan ceilingFan) { 7 | this.ceilingFan = ceilingFan; 8 | } 9 | public void execute() { 10 | ceilingFan.off(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/CeilingFanOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class CeilingFanOnCommand implements Command { 4 | CeilingFan ceilingFan; 5 | 6 | public CeilingFanOnCommand(CeilingFan ceilingFan) { 7 | this.ceilingFan = ceilingFan; 8 | } 9 | public void execute() { 10 | ceilingFan.high(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public interface Command { 4 | public void execute(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/GarageDoorDownCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class GarageDoorDownCommand implements Command { 4 | GarageDoor garageDoor; 5 | 6 | public GarageDoorDownCommand(GarageDoor garageDoor) { 7 | this.garageDoor = garageDoor; 8 | } 9 | 10 | public void execute() { 11 | garageDoor.up(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/GarageDoorUpCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class GarageDoorUpCommand implements Command { 4 | GarageDoor garageDoor; 5 | 6 | public GarageDoorUpCommand(GarageDoor garageDoor) { 7 | this.garageDoor = garageDoor; 8 | } 9 | 10 | public void execute() { 11 | garageDoor.up(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/HottubOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class HottubOffCommand implements Command { 4 | Hottub hottub; 5 | 6 | public HottubOffCommand(Hottub hottub) { 7 | this.hottub = hottub; 8 | } 9 | 10 | public void execute() { 11 | hottub.cool(); 12 | hottub.off(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/HottubOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class HottubOnCommand implements Command { 4 | Hottub hottub; 5 | 6 | public HottubOnCommand(Hottub hottub) { 7 | this.hottub = hottub; 8 | } 9 | 10 | public void execute() { 11 | hottub.on(); 12 | hottub.heat(); 13 | hottub.bubblesOn(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/Light.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class Light { 4 | String location = ""; 5 | 6 | public Light(String location) { 7 | this.location = location; 8 | } 9 | 10 | public void on() { 11 | System.out.println(location + " light is on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(location + " light is off"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class LightOffCommand implements Command { 4 | Light light; 5 | 6 | public LightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.off(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class LightOnCommand implements Command { 4 | Light light; 5 | 6 | public LightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.on(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/LivingroomLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class LivingroomLightOffCommand implements Command { 4 | Light light; 5 | 6 | public LivingroomLightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.off(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/LivingroomLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class LivingroomLightOnCommand implements Command { 4 | Light light; 5 | 6 | public LivingroomLightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.on(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/NoCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/StereoOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class StereoOffCommand implements Command { 4 | Stereo stereo; 5 | 6 | public StereoOffCommand(Stereo stereo) { 7 | this.stereo = stereo; 8 | } 9 | 10 | public void execute() { 11 | stereo.off(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/StereoOnWithCDCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class StereoOnWithCDCommand implements Command { 4 | Stereo stereo; 5 | 6 | public StereoOnWithCDCommand(Stereo stereo) { 7 | this.stereo = stereo; 8 | } 9 | 10 | public void execute() { 11 | stereo.on(); 12 | stereo.setCD(); 13 | stereo.setVolume(11); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remote/TV.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remote; 2 | 3 | public class TV { 4 | String location; 5 | int channel; 6 | 7 | public TV(String location) { 8 | this.location = location; 9 | } 10 | 11 | public void on() { 12 | System.out.println("TV is on"); 13 | } 14 | 15 | public void off() { 16 | System.out.println("TV is off"); 17 | } 18 | 19 | public void setInputChannel() { 20 | this.channel = 3; 21 | System.out.println("Channel is set for VCR"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remoteWL/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remoteWL; 2 | 3 | @FunctionalInterface 4 | public interface Command { 5 | public void execute(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remoteWL/Light.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remoteWL; 2 | 3 | public class Light { 4 | String location = ""; 5 | 6 | public Light(String location) { 7 | this.location = location; 8 | } 9 | 10 | public void on() { 11 | System.out.println(location + " light is on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(location + " light is off"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/command/remoteWL/TV.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.remoteWL; 2 | 3 | public class TV { 4 | String location; 5 | int channel; 6 | 7 | public TV(String location) { 8 | this.location = location; 9 | } 10 | 11 | public void on() { 12 | System.out.println("TV is on"); 13 | } 14 | 15 | public void off() { 16 | System.out.println("TV is off"); 17 | } 18 | 19 | public void setInputChannel() { 20 | this.channel = 3; 21 | System.out.println("Channel is set for VCR"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | public interface Command { 4 | public void execute(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/GarageDoorOpenCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | public class GarageDoorOpenCommand implements Command { 4 | GarageDoor garageDoor; 5 | 6 | public GarageDoorOpenCommand(GarageDoor garageDoor) { 7 | this.garageDoor = garageDoor; 8 | } 9 | 10 | public void execute() { 11 | garageDoor.up(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/Light.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | public class Light { 4 | 5 | public Light() { 6 | } 7 | 8 | public void on() { 9 | System.out.println("Light is on"); 10 | } 11 | 12 | public void off() { 13 | System.out.println("Light is off"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | public class LightOffCommand implements Command { 4 | Light light; 5 | 6 | public LightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.off(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | public class LightOnCommand implements Command { 4 | Light light; 5 | 6 | public LightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | light.on(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremote/SimpleRemoteControl.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremote; 2 | 3 | // 4 | // This is the invoker 5 | // 6 | public class SimpleRemoteControl { 7 | Command slot; 8 | 9 | public SimpleRemoteControl() {} 10 | 11 | public void setCommand(Command command) { 12 | slot = command; 13 | } 14 | 15 | public void buttonWasPressed() { 16 | slot.execute(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremoteWL/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremoteWL; 2 | 3 | @FunctionalInterface 4 | public interface Command { 5 | public void execute(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremoteWL/Light.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremoteWL; 2 | 3 | public class Light { 4 | 5 | public Light() { 6 | } 7 | 8 | public void on() { 9 | System.out.println("Light is on"); 10 | } 11 | 12 | public void off() { 13 | System.out.println("Light is off"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/command/simpleremoteWL/SimpleRemoteControl.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.simpleremoteWL; 2 | 3 | // 4 | // This is the invoker 5 | // 6 | public class SimpleRemoteControl { 7 | Command slot; 8 | 9 | public SimpleRemoteControl() {} 10 | 11 | public void setCommand(Command command) { 12 | slot = command; 13 | } 14 | 15 | public void buttonWasPressed() { 16 | slot.execute(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/Command.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public interface Command { 4 | public void execute(); 5 | public void undo(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/DimmerLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public class DimmerLightOffCommand implements Command { 4 | Light light; 5 | int prevLevel; 6 | 7 | public DimmerLightOffCommand(Light light) { 8 | this.light = light; 9 | prevLevel = 100; 10 | } 11 | 12 | public void execute() { 13 | prevLevel = light.getLevel(); 14 | light.off(); 15 | } 16 | 17 | public void undo() { 18 | light.dim(prevLevel); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/DimmerLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public class DimmerLightOnCommand implements Command { 4 | Light light; 5 | int prevLevel; 6 | 7 | public DimmerLightOnCommand(Light light) { 8 | this.light = light; 9 | } 10 | 11 | public void execute() { 12 | prevLevel = light.getLevel(); 13 | light.dim(75); 14 | } 15 | 16 | public void undo() { 17 | light.dim(prevLevel); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public class LightOffCommand implements Command { 4 | Light light; 5 | int level; 6 | public LightOffCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | level = light.getLevel(); 12 | light.off(); 13 | } 14 | 15 | public void undo() { 16 | light.dim(level); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public class LightOnCommand implements Command { 4 | Light light; 5 | int level; 6 | public LightOnCommand(Light light) { 7 | this.light = light; 8 | } 9 | 10 | public void execute() { 11 | level = light.getLevel(); 12 | light.on(); 13 | } 14 | 15 | public void undo() { 16 | light.dim(level); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/command/undo/NoCommand.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.command.undo; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | public void undo() { } 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/composite/menu/Waitress.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.composite.menu; 2 | 3 | public class Waitress { 4 | MenuComponent allMenus; 5 | 6 | public Waitress(MenuComponent allMenus) { 7 | this.allMenus = allMenus; 8 | } 9 | 10 | public void printMenu() { 11 | allMenus.print(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/Cheese.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public class Cheese extends ToppingDecorator { 4 | 5 | 6 | public Cheese(Pizza pizza) { 7 | this.pizza = pizza; 8 | } 9 | 10 | public String getDescription() { 11 | return pizza.getDescription() + ", Cheese"; 12 | } 13 | 14 | public double cost() { 15 | return pizza.cost(); // cheese is free 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/Olives.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public class Olives extends ToppingDecorator { 4 | 5 | 6 | public Olives(Pizza pizza) { 7 | this.pizza = pizza; 8 | } 9 | 10 | public String getDescription() { 11 | return pizza.getDescription() + ", Olives"; 12 | } 13 | 14 | public double cost() { 15 | return pizza.cost() + .30; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/Pizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public abstract class Pizza { 4 | String description = "Basic Pizza"; 5 | 6 | public String getDescription() { 7 | return description; 8 | } 9 | 10 | public abstract double cost(); 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public class PizzaStore { 4 | 5 | public static void main(String args[]) { 6 | Pizza pizza = new ThincrustPizza(); 7 | Pizza cheesePizza = new Cheese(pizza); 8 | Pizza greekPizza = new Olives(cheesePizza); 9 | 10 | System.out.println(greekPizza.getDescription() 11 | + " $" + greekPizza.cost()); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/ThickcrustPizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public class ThickcrustPizza extends Pizza { 4 | 5 | public ThickcrustPizza() { 6 | description = "Thick crust pizza, with tomato sauce"; 7 | } 8 | 9 | public double cost() { 10 | return 7.99; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/ThincrustPizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public class ThincrustPizza extends Pizza { 4 | 5 | public ThincrustPizza() { 6 | description = "Thin crust pizza, with tomato sauce"; 7 | } 8 | 9 | public double cost() { 10 | return 7.99; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/pizza/ToppingDecorator.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.pizza; 2 | 3 | public abstract class ToppingDecorator extends Pizza { 4 | Pizza pizza; 5 | public abstract String getDescription(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Beverage.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public abstract class Beverage { 4 | String description = "Unknown Beverage"; 5 | 6 | public String getDescription() { 7 | return description; 8 | } 9 | 10 | public abstract double cost(); 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/CondimentDecorator.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public abstract class CondimentDecorator extends Beverage { 4 | Beverage beverage; 5 | public abstract String getDescription(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/DarkRoast.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class DarkRoast extends Beverage { 4 | public DarkRoast() { 5 | description = "Dark Roast Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return .99; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Decaf.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Decaf extends Beverage { 4 | public Decaf() { 5 | description = "Decaf Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return 1.05; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Espresso.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Espresso extends Beverage { 4 | 5 | public Espresso() { 6 | description = "Espresso"; 7 | } 8 | 9 | public double cost() { 10 | return 1.99; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class HouseBlend extends Beverage { 4 | public HouseBlend() { 5 | description = "House Blend Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return .89; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Milk.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Milk extends CondimentDecorator { 4 | public Milk(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Milk"; 10 | } 11 | 12 | public double cost() { 13 | return .10 + beverage.cost(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Mocha.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Mocha extends CondimentDecorator { 4 | public Mocha(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Mocha"; 10 | } 11 | 12 | public double cost() { 13 | return .20 + beverage.cost(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Soy.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Soy extends CondimentDecorator { 4 | public Soy(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Soy"; 10 | } 11 | 12 | public double cost() { 13 | return .15 + beverage.cost(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzz/Whip.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzz; 2 | 3 | public class Whip extends CondimentDecorator { 4 | public Whip(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Whip"; 10 | } 11 | 12 | public double cost() { 13 | return .10 + beverage.cost(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/CondimentDecorator.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public abstract class CondimentDecorator extends Beverage { 4 | public Beverage beverage; 5 | public abstract String getDescription(); 6 | 7 | public Size getSize() { 8 | return beverage.getSize(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/DarkRoast.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class DarkRoast extends Beverage { 4 | public DarkRoast() { 5 | description = "Dark Roast Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return .99; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/Decaf.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class Decaf extends Beverage { 4 | public Decaf() { 5 | description = "Decaf Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return 1.05; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/Espresso.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class Espresso extends Beverage { 4 | 5 | public Espresso() { 6 | description = "Espresso"; 7 | } 8 | 9 | public double cost() { 10 | return 1.99; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class HouseBlend extends Beverage { 4 | public HouseBlend() { 5 | description = "House Blend Coffee"; 6 | } 7 | 8 | public double cost() { 9 | return .89; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/Milk.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class Milk extends CondimentDecorator { 4 | public Milk(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Milk"; 10 | } 11 | 12 | public double cost() { 13 | return beverage.cost() + .10; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/Mocha.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class Mocha extends CondimentDecorator { 4 | public Mocha(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Mocha"; 10 | } 11 | 12 | public double cost() { 13 | return beverage.cost() + .20; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/decorator/starbuzzWithSizes/Whip.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.decorator.starbuzzWithSizes; 2 | 3 | public class Whip extends CondimentDecorator { 4 | public Whip(Beverage beverage) { 5 | this.beverage = beverage; 6 | } 7 | 8 | public String getDescription() { 9 | return beverage.getDescription() + ", Whip"; 10 | } 11 | 12 | public double cost() { 13 | return beverage.cost() + .10; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/Duck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public interface Duck { 4 | public void quack(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/DuckAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | import java.util.Random; 3 | 4 | public class DuckAdapter implements Turkey { 5 | Duck duck; 6 | Random rand; 7 | 8 | public DuckAdapter(Duck duck) { 9 | this.duck = duck; 10 | rand = new Random(); 11 | } 12 | 13 | public void gobble() { 14 | duck.quack(); 15 | } 16 | 17 | public void fly() { 18 | if (rand.nextInt(5) == 0) { 19 | duck.fly(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public class MallardDuck implements Duck { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/Turkey.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public interface Turkey { 4 | public void gobble(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/TurkeyAdapter.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public class TurkeyAdapter implements Duck { 4 | Turkey turkey; 5 | 6 | public TurkeyAdapter(Turkey turkey) { 7 | this.turkey = turkey; 8 | } 9 | 10 | public void quack() { 11 | turkey.gobble(); 12 | } 13 | 14 | public void fly() { 15 | for(int i=0; i < 5; i++) { 16 | turkey.fly(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/TurkeyTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public class TurkeyTestDrive { 4 | public static void main(String[] args) { 5 | MallardDuck duck = new MallardDuck(); 6 | Turkey duckAdapter = new DuckAdapter(duck); 7 | 8 | for(int i=0;i<10;i++) { 9 | System.out.println("The DuckAdapter says..."); 10 | duckAdapter.gobble(); 11 | duckAdapter.fly(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/ducks/WildTurkey.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.ducks; 2 | 3 | public class WildTurkey implements Turkey { 4 | public void gobble() { 5 | System.out.println("Gobble gobble"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying a short distance"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/facade/hometheater/Screen.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.facade.hometheater; 2 | 3 | public class Screen { 4 | String description; 5 | 6 | public Screen(String description) { 7 | this.description = description; 8 | } 9 | 10 | public void up() { 11 | System.out.println(description + " going up"); 12 | } 13 | 14 | public void down() { 15 | System.out.println(description + " going down"); 16 | } 17 | 18 | 19 | public String toString() { 20 | return description; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/Calendar.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | import java.util.*; 4 | 5 | public abstract class Calendar { 6 | Zone zone; 7 | public void print() { 8 | System.out.println("--- " + zone.getDisplayName() + " Calendar ---"); 9 | // print all appointments in correct time zone 10 | System.out.println("Offset from GMT: " + zone.getOffset()); 11 | } 12 | public abstract void createCalendar(List appointments); 13 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/CalendarTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | import java.util.*; 4 | 5 | public class CalendarTestDrive { 6 | 7 | public static void main(String[] args) { 8 | ZoneFactory zoneFactory = new ZoneFactory(); 9 | Calendar calendar = new PacificCalendar(zoneFactory); 10 | List appts = Arrays.asList("appt 1", "appt 2"); 11 | calendar.createCalendar(appts); 12 | calendar.print(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/PacificCalendar.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | import java.util.*; 4 | 5 | public class PacificCalendar extends Calendar { 6 | public PacificCalendar(ZoneFactory zoneFactory) { 7 | zone = zoneFactory.createZone("US/Pacific"); 8 | // make a calendar for the pacific zone 9 | // ... 10 | } 11 | public void createCalendar(List appointments) { 12 | // make calendar from appointments 13 | System.out.println("Making the calendar"); 14 | } 15 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/Zone.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | public class Zone { 4 | String displayName; 5 | int offset; 6 | public String getDisplayName() { return displayName; } 7 | public int getOffset() { return offset; } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/ZoneCentral.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | public class ZoneCentral extends Zone { 4 | public ZoneCentral() { 5 | displayName = "US/Central"; 6 | offset = -6; 7 | } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/ZoneEastern.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | public class ZoneEastern extends Zone { 4 | public ZoneEastern() { 5 | displayName = "US/Eastern"; 6 | offset = -5; 7 | } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/ZoneMountain.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | public class ZoneMountain extends Zone { 4 | public ZoneMountain() { 5 | displayName = "US/Mountain"; 6 | offset = -7; 7 | } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/challenge/ZonePacific.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.challenge; 2 | 3 | public class ZonePacific extends Zone { 4 | public ZonePacific() { 5 | displayName = "US/Pacific"; 6 | offset = -8; 7 | } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/BlackOlives.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class BlackOlives implements Veggies { 4 | 5 | public String toString() { 6 | return "Black Olives"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Cheese.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Cheese { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Clams.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Clams { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Dough.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Dough { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Eggplant.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class Eggplant implements Veggies { 4 | 5 | public String toString() { 6 | return "Eggplant"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/FreshClams.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class FreshClams implements Clams { 4 | 5 | public String toString() { 6 | return "Fresh Clams from Long Island Sound"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/FrozenClams.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class FrozenClams implements Clams { 4 | 5 | public String toString() { 6 | return "Frozen Clams from Chesapeake Bay"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Garlic.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class Garlic implements Veggies { 4 | 5 | public String toString() { 6 | return "Garlic"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/MarinaraSauce.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class MarinaraSauce implements Sauce { 4 | public String toString() { 5 | return "Marinara Sauce"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/MozzarellaCheese.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class MozzarellaCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Mozzarella"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Mushroom.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class Mushroom implements Veggies { 4 | 5 | public String toString() { 6 | return "Mushrooms"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Onion.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class Onion implements Veggies { 4 | 5 | public String toString() { 6 | return "Onion"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/ParmesanCheese.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class ParmesanCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Parmesan"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Pepperoni.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Pepperoni { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/PizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface PizzaIngredientFactory { 4 | 5 | public Dough createDough(); 6 | public Sauce createSauce(); 7 | public Cheese createCheese(); 8 | public Veggies[] createVeggies(); 9 | public Pepperoni createPepperoni(); 10 | public Clams createClam(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public abstract class PizzaStore { 4 | 5 | protected abstract Pizza createPizza(String item); 6 | 7 | public Pizza orderPizza(String type) { 8 | Pizza pizza = createPizza(type); 9 | System.out.println("--- Making a " + pizza.getName() + " ---"); 10 | pizza.prepare(); 11 | pizza.bake(); 12 | pizza.cut(); 13 | pizza.box(); 14 | return pizza; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/PlumTomatoSauce.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class PlumTomatoSauce implements Sauce { 4 | public String toString() { 5 | return "Tomato sauce with plum tomatoes"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/RedPepper.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class RedPepper implements Veggies { 4 | 5 | public String toString() { 6 | return "Red Pepper"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/ReggianoCheese.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class ReggianoCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Reggiano Cheese"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Sauce.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Sauce { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/SlicedPepperoni.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class SlicedPepperoni implements Pepperoni { 4 | 5 | public String toString() { 6 | return "Sliced Pepperoni"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Spinach.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class Spinach implements Veggies { 4 | 5 | public String toString() { 6 | return "Spinach"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/ThickCrustDough.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class ThickCrustDough implements Dough { 4 | public String toString() { 5 | return "ThickCrust style extra thick crust dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/ThinCrustDough.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public class ThinCrustDough implements Dough { 4 | public String toString() { 5 | return "Thin Crust Dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzaaf/Veggies.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzaaf; 2 | 3 | public interface Veggies { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzafm/ChicagoStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzafm; 2 | 3 | public class ChicagoStyleCheesePizza extends Pizza { 4 | 5 | public ChicagoStyleCheesePizza() { 6 | name = "Chicago Style Deep Dish Cheese Pizza"; 7 | dough = "Extra Thick Crust Dough"; 8 | sauce = "Plum Tomato Sauce"; 9 | 10 | toppings.add("Shredded Mozzarella Cheese"); 11 | } 12 | 13 | void cut() { 14 | System.out.println("Cutting the pizza into square slices"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzafm/NYStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzafm; 2 | 3 | public class NYStyleCheesePizza extends Pizza { 4 | 5 | public NYStyleCheesePizza() { 6 | name = "NY Style Sauce and Cheese Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzafm/NYStyleClamPizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzafm; 2 | 3 | public class NYStyleClamPizza extends Pizza { 4 | 5 | public NYStyleClamPizza() { 6 | name = "NY Style Clam Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | toppings.add("Fresh Clams from Long Island Sound"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzafm/NYStyleVeggiePizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzafm; 2 | 3 | public class NYStyleVeggiePizza extends Pizza { 4 | 5 | public NYStyleVeggiePizza() { 6 | name = "NY Style Veggie Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | toppings.add("Garlic"); 12 | toppings.add("Onion"); 13 | toppings.add("Mushrooms"); 14 | toppings.add("Red Pepper"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzafm/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzafm; 2 | 3 | public abstract class PizzaStore { 4 | 5 | abstract Pizza createPizza(String item); 6 | 7 | public Pizza orderPizza(String type) { 8 | Pizza pizza = createPizza(type); 9 | System.out.println("--- Making a " + pizza.getName() + " ---"); 10 | pizza.prepare(); 11 | pizza.bake(); 12 | pizza.cut(); 13 | pizza.box(); 14 | return pizza; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzas/CheesePizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzas; 2 | 3 | public class CheesePizza extends Pizza { 4 | public CheesePizza() { 5 | name = "Cheese Pizza"; 6 | dough = "Regular Crust"; 7 | sauce = "Marinara Pizza Sauce"; 8 | toppings.add("Fresh Mozzarella"); 9 | toppings.add("Parmesan"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzas/ClamPizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzas; 2 | 3 | public class ClamPizza extends Pizza { 4 | public ClamPizza() { 5 | name = "Clam Pizza"; 6 | dough = "Thin crust"; 7 | sauce = "White garlic sauce"; 8 | toppings.add("Clams"); 9 | toppings.add("Grated parmesan cheese"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzas/PepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzas; 2 | 3 | public class PepperoniPizza extends Pizza { 4 | public PepperoniPizza() { 5 | name = "Pepperoni Pizza"; 6 | dough = "Crust"; 7 | sauce = "Marinara sauce"; 8 | toppings.add("Sliced Pepperoni"); 9 | toppings.add("Sliced Onion"); 10 | toppings.add("Grated parmesan cheese"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzas/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzas; 2 | 3 | public class PizzaStore { 4 | SimplePizzaFactory factory; 5 | 6 | public PizzaStore(SimplePizzaFactory factory) { 7 | this.factory = factory; 8 | } 9 | 10 | public Pizza orderPizza(String type) { 11 | Pizza pizza; 12 | 13 | pizza = factory.createPizza(type); 14 | 15 | pizza.prepare(); 16 | pizza.bake(); 17 | pizza.cut(); 18 | pizza.box(); 19 | 20 | return pizza; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /HeadFirst-java/factory/pizzas/VeggiePizza.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.factory.pizzas; 2 | 3 | public class VeggiePizza extends Pizza { 4 | public VeggiePizza() { 5 | name = "Veggie Pizza"; 6 | dough = "Crust"; 7 | sauce = "Marinara sauce"; 8 | toppings.add("Shredded mozzarella"); 9 | toppings.add("Grated parmesan"); 10 | toppings.add("Diced onion"); 11 | toppings.add("Sliced mushrooms"); 12 | toppings.add("Sliced red pepper"); 13 | toppings.add("Sliced black olives"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/flyweight/ConiferTree.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.flyweight; 2 | 3 | public class ConiferTree implements Tree { 4 | // Complex trunk, branch, needle graphic data 5 | public void display(int x, int y) { 6 | System.out.println("Conifer tree is located at " + x + ", " + y); 7 | } 8 | } -------------------------------------------------------------------------------- /HeadFirst-java/flyweight/DeciduousTree.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.flyweight; 2 | 3 | import java.time.*; 4 | 5 | public class DeciduousTree implements Tree { 6 | // complex trunk, branch, leaf graphic data 7 | public void display(int x, int y) { 8 | System.out.println("Deciduous tree is located at " + x + ", " + y); 9 | if (!this.isWithinRange(LocalDate.now())) { 10 | System.out.println("The tree currently has no leaves"); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /HeadFirst-java/flyweight/Tree.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.flyweight; 2 | 3 | import java.time.*; 4 | 5 | public interface Tree { 6 | public void display(int x, int y); 7 | public default boolean isWithinRange(LocalDate aDate) { 8 | Month month = aDate.getMonth(); 9 | return (month.getValue() > 2) && (month.getValue() < 11); 10 | } 11 | } -------------------------------------------------------------------------------- /HeadFirst-java/flyweight/TreeFactory.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.flyweight; 2 | 3 | public class TreeFactory { 4 | Tree d, c = null; 5 | public TreeFactory() { 6 | this.d = new DeciduousTree(); 7 | this.c = new ConiferTree(); 8 | } 9 | public Tree getTree(String type) throws Exception { 10 | if (type.equals("deciduous")) { 11 | return this.d; 12 | } else if (type.equals("conifer")) { 13 | return this.c; 14 | } else { 15 | throw new Exception("Invalid kind of tree"); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /HeadFirst-java/iterator/dinermerger/Iterator.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.dinermerger; 2 | 3 | public interface Iterator { 4 | boolean hasNext(); 5 | MenuItem next(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/dinermerger/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.dinermerger; 2 | 3 | public interface Menu { 4 | public Iterator createIterator(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/dinermergercafe/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.dinermergercafe; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/dinermergeri/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.dinermergeri; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/implicit/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.implicit; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/implicit/MenuTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.implicit; 2 | 3 | public class MenuTestDrive { 4 | public static void main(String args[]) { 5 | PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); 6 | DinerMenu dinerMenu = new DinerMenu(); 7 | Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); 8 | // Use implicit iteration 9 | waitress.printMenu(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/iterator/transition/Menu.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterator.transition; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/iterenum/EnumerationIteratorTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class EnumerationIteratorTestDrive { 6 | public static void main (String args[]) { 7 | Vector v = new Vector(Arrays.asList(args)); 8 | Iterator iterator = new EnumerationIterator(v.elements()); 9 | while (iterator.hasNext()) { 10 | System.out.println(iterator.next()); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/iterenum/IteratorEnumeration.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class IteratorEnumeration implements Enumeration { 6 | Iterator iterator; 7 | 8 | public IteratorEnumeration(Iterator iterator) { 9 | this.iterator = iterator; 10 | } 11 | 12 | public boolean hasMoreElements() { 13 | return iterator.hasNext(); 14 | } 15 | 16 | public Object nextElement() { 17 | return iterator.next(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /HeadFirst-java/iterenum/IteratorEnumerationTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.iterenum; 2 | 3 | import java.util.*; 4 | 5 | public class IteratorEnumerationTestDrive { 6 | public static void main (String args[]) { 7 | ArrayList l = new ArrayList(Arrays.asList(args)); 8 | Enumeration enumeration = new IteratorEnumeration(l.iterator()); 9 | while (enumeration.hasMoreElements()) { 10 | System.out.println(enumeration.nextElement()); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/simple/Example.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.simple; 2 | 3 | public class Example { 4 | 5 | public static void main(String[] args) { 6 | SimpleSubject simpleSubject = new SimpleSubject(); 7 | 8 | SimpleObserver simpleObserver = new SimpleObserver(simpleSubject); 9 | 10 | simpleSubject.setValue(80); 11 | 12 | simpleSubject.removeObserver(simpleObserver); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/simple/Observer.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.simple; 2 | 3 | public interface Observer { 4 | public void update(int value); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/simple/Subject.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.simple; 2 | 3 | public interface Subject { 4 | public void registerObserver(Observer o); 5 | public void removeObserver(Observer o); 6 | public void notifyObservers(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/simpleobservable/Example.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.simpleobservable; 2 | 3 | public class Example { 4 | 5 | public static void main(String[] args) { 6 | SimpleSubject simpleSubject = new SimpleSubject(); 7 | 8 | SimpleObserver simpleObserver = new SimpleObserver(simpleSubject); 9 | 10 | simpleSubject.setValue(80); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/simpleobservable/SimpleSubject.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.simpleobservable; 2 | 3 | import java.util.Observable; 4 | 5 | public class SimpleSubject extends Observable { 6 | private int value = 0; 7 | 8 | public SimpleSubject() { } 9 | 10 | public void setValue(int value) { 11 | this.value = value; 12 | setChanged(); 13 | notifyObservers(value); 14 | } 15 | 16 | public int getValue() { 17 | return this.value; 18 | } 19 | } -------------------------------------------------------------------------------- /HeadFirst-java/observer/weather/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.weather; 2 | 3 | public interface DisplayElement { 4 | public void display(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/weather/Observer.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.weather; 2 | 3 | public interface Observer { 4 | public void update(float temp, float humidity, float pressure); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/weather/Subject.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.weather; 2 | 3 | public interface Subject { 4 | public void registerObserver(Observer o); 5 | public void removeObserver(Observer o); 6 | public void notifyObservers(); 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/observer/weatherobservable/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.observer.weatherobservable; 2 | 3 | public interface DisplayElement { 4 | public void display(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/prototype/Dragon.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.prototype; 2 | 3 | public class Dragon extends Monster { 4 | public Dragon(String name, boolean hasWings) { 5 | super(name); 6 | this.hasWings = hasWings; 7 | this.canBreatheFire = true; 8 | } 9 | // Each concrete monster could determine how best to clone itself 10 | public Monster copy() throws CloneNotSupportedException { 11 | return (Monster)this.clone(); 12 | } 13 | } -------------------------------------------------------------------------------- /HeadFirst-java/proxy/gumball/GumballMachineRemote.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.proxy.gumball; 2 | 3 | import java.rmi.*; 4 | 5 | public interface GumballMachineRemote extends Remote { 6 | public int getCount() throws RemoteException; 7 | public String getLocation() throws RemoteException; 8 | public State getState() throws RemoteException; 9 | } 10 | -------------------------------------------------------------------------------- /HeadFirst-java/proxy/gumball/README.md: -------------------------------------------------------------------------------- 1 | # Gumball Machine with Proxy Pattern 2 | 3 | To get code running on localhost: 4 | 5 | 1. Change directories to the same level with headfirst/ folder in bin/ 6 | 7 | 2. Run rmiregistry in background: 8 | rmiregistry & 9 | 10 | 3. Run: 11 | java headfirst/designpatterns/proxy/gumball/GumballMachineTestDrive localhost 300 12 | 13 | 4. In a different window, run: 14 | java headfirst/designpatterns/proxy/gumball/GumballMonitorTestDrive localhost 15 | 16 | 17 | -------------------------------------------------------------------------------- /HeadFirst-java/proxy/gumball/State.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.proxy.gumball; 2 | 3 | import java.io.*; 4 | 5 | public interface State extends Serializable { 6 | public void insertQuarter(); 7 | public void ejectQuarter(); 8 | public void turnCrank(); 9 | public void dispense(); 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-java/proxy/gumballmonitor/State.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.proxy.gumballmonitor; 2 | 3 | import java.io.*; 4 | 5 | public interface State extends Serializable { 6 | 7 | public void insertQuarter(); 8 | public void ejectQuarter(); 9 | public void turnCrank(); 10 | public void dispense(); 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/proxy/javaproxy/Person.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.proxy.javaproxy; 2 | 3 | public interface Person { 4 | 5 | String getName(); 6 | String getGender(); 7 | String getInterests(); 8 | int getGeekRating(); 9 | 10 | void setName(String name); 11 | void setGender(String gender); 12 | void setInterests(String interests); 13 | void setGeekRating(int rating); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/chocolate/ChocolateController.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.chocolate; 2 | 3 | public class ChocolateController { 4 | public static void main(String args[]) { 5 | ChocolateBoiler boiler = ChocolateBoiler.getInstance(); 6 | boiler.fill(); 7 | boiler.boil(); 8 | boiler.drain(); 9 | 10 | // will return the existing instance 11 | ChocolateBoiler boiler2 = ChocolateBoiler.getInstance(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/classic/SingletonClient.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.classic; 2 | 3 | public class SingletonClient { 4 | public static void main(String[] args) { 5 | Singleton singleton = Singleton.getInstance(); 6 | System.out.println(singleton.getDescription()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/dcl/SingletonClient.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.dcl; 2 | 3 | public class SingletonClient { 4 | public static void main(String[] args) { 5 | Singleton singleton = Singleton.getInstance(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/enumS/Singleton.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.enumS; 2 | 3 | public enum Singleton { 4 | UNIQUE_INSTANCE; 5 | 6 | // other useful fields here 7 | 8 | // other useful methods here 9 | public String getDescription() { 10 | return "I'm a thread safe Singleton!"; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/enumS/SingletonClient.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.enumS; 2 | 3 | public class SingletonClient { 4 | public static void main(String[] args) { 5 | Singleton singleton = Singleton.UNIQUE_INSTANCE; 6 | System.out.println(singleton.getDescription()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/stat/Singleton.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.stat; 2 | 3 | public class Singleton { 4 | private static Singleton uniqueInstance = new Singleton(); 5 | 6 | private Singleton() {} 7 | 8 | public static Singleton getInstance() { 9 | return uniqueInstance; 10 | } 11 | 12 | // other useful methods here 13 | public String getDescription() { 14 | return "I'm a statically initialized Singleton!"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/stat/SingletonClient.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.stat; 2 | 3 | public class SingletonClient { 4 | public static void main(String[] args) { 5 | Singleton singleton = Singleton.getInstance(); 6 | System.out.println(singleton.getDescription()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/subclass/CoolerSingleton.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.subclass; 2 | 3 | public class CoolerSingleton extends Singleton { 4 | // useful instance variables here 5 | protected static Singleton uniqueInstance; 6 | 7 | private CoolerSingleton() { 8 | super(); 9 | } 10 | 11 | // useful methods here 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/subclass/HotterSingleton.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.subclass; 2 | 3 | public class HotterSingleton extends Singleton { 4 | // useful instance variables here 5 | 6 | private HotterSingleton() { 7 | super(); 8 | } 9 | 10 | // useful methods here 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/subclass/Singleton.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.subclass; 2 | 3 | public class Singleton { 4 | protected static Singleton uniqueInstance; 5 | 6 | // other useful instance variables here 7 | 8 | protected Singleton() {} 9 | 10 | public static synchronized Singleton getInstance() { 11 | if (uniqueInstance == null) { 12 | uniqueInstance = new Singleton(); 13 | } 14 | return uniqueInstance; 15 | } 16 | 17 | // other useful methods here 18 | } 19 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/subclass/SingletonTestDrive.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.subclass; 2 | 3 | public class SingletonTestDrive { 4 | public static void main(String[] args) { 5 | Singleton foo = CoolerSingleton.getInstance(); 6 | Singleton bar = HotterSingleton.getInstance(); 7 | System.out.println(foo); 8 | System.out.println(bar); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-java/singleton/threadsafe/SingletonClient.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.singleton.threadsafe; 2 | 3 | public class SingletonClient { 4 | public static void main(String[] args) { 5 | Singleton singleton = Singleton.getInstance(); 6 | System.out.println(singleton.getDescription()); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /HeadFirst-java/state/gumballstate/State.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.state.gumballstate; 2 | 3 | public interface State { 4 | 5 | public void insertQuarter(); 6 | public void ejectQuarter(); 7 | public void turnCrank(); 8 | public void dispense(); 9 | 10 | public void refill(); 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/state/gumballstatewinner/State.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.state.gumballstatewinner; 2 | 3 | public interface State { 4 | 5 | public void insertQuarter(); 6 | public void ejectQuarter(); 7 | public void turnCrank(); 8 | public void dispense(); 9 | 10 | public void refill(); 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class DecoyDuck extends Duck { 4 | public DecoyDuck() { 5 | setFlyBehavior(new FlyNoWay()); 6 | setQuackBehavior(new MuteQuack()); 7 | } 8 | public void display() { 9 | System.out.println("I'm a duck Decoy"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/FakeQuack.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class FakeQuack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Qwak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/FlyBehavior.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public interface FlyBehavior { 4 | public void fly(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/FlyNoWay.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class FlyNoWay implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I can't fly"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/FlyRocketPowered.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class FlyRocketPowered implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I'm flying with a rocket"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/FlyWithWings.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class FlyWithWings implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I'm flying!!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class MallardDuck extends Duck { 4 | 5 | public MallardDuck() { 6 | 7 | quackBehavior = new Quack(); 8 | flyBehavior = new FlyWithWings(); 9 | 10 | } 11 | 12 | public void display() { 13 | System.out.println("I'm a real Mallard duck"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/MiniDuckSimulator1.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class MiniDuckSimulator1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Duck mallard = new MallardDuck(); 8 | mallard.performQuack(); 9 | mallard.performFly(); 10 | 11 | Duck model = new ModelDuck(); 12 | model.performFly(); 13 | model.setFlyBehavior(new FlyRocketPowered()); 14 | model.performFly(); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/ModelDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class ModelDuck extends Duck { 4 | public ModelDuck() { 5 | flyBehavior = new FlyNoWay(); 6 | quackBehavior = new Quack(); 7 | } 8 | 9 | public void display() { 10 | System.out.println("I'm a model duck"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/MuteQuack.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class MuteQuack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("<< Silence >>"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/Quack.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class Quack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/QuackBehavior.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public interface QuackBehavior { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/RedHeadDuck.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class RedHeadDuck extends Duck { 4 | 5 | public RedHeadDuck() { 6 | flyBehavior = new FlyWithWings(); 7 | quackBehavior = new Quack(); 8 | } 9 | 10 | public void display() { 11 | System.out.println("I'm a real Red Headed duck"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/Squeak.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy; 2 | 3 | public class Squeak implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Squeak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/BasicCameraApp.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | public class BasicCameraApp extends PhoneCameraApp { 4 | public void edit() { 5 | System.out.println("Basic editing features"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/CameraPlusApp.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | public class CameraPlusApp extends PhoneCameraApp { 4 | public void edit() { 5 | System.out.println("Extra snazzy photo editing features"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/Email.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | public class Email implements ShareStrategy { 4 | public void share() { 5 | System.out.println("I'm emailing the photo"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/ShareStrategy.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | @FunctionalInterface 4 | public interface ShareStrategy { 5 | public void share(); 6 | } 7 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/Social.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | public class Social implements ShareStrategy { 4 | public void share() { 5 | System.out.println("I'm posting the photo on social media"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/strategy/challenge/Txt.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.strategy.challenge; 2 | 3 | public class Txt implements ShareStrategy { 4 | public void share() { 5 | System.out.println("I'm txting the photo"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /HeadFirst-java/templatemethod/barista/Coffee.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.templatemethod.barista; 2 | 3 | public class Coffee extends CaffeineBeverage { 4 | public void brew() { 5 | System.out.println("Dripping Coffee through filter"); 6 | } 7 | public void addCondiments() { 8 | System.out.println("Adding Sugar and Milk"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-java/templatemethod/barista/Tea.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.templatemethod.barista; 2 | 3 | public class Tea extends CaffeineBeverage { 4 | public void brew() { 5 | System.out.println("Steeping the tea"); 6 | } 7 | public void addCondiments() { 8 | System.out.println("Adding Lemon"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /HeadFirst-java/templatemethod/simplebarista/Barista.java: -------------------------------------------------------------------------------- 1 | package headfirst.designpatterns.templatemethod.simplebarista; 2 | 3 | public class Barista { 4 | 5 | public static void main(String[] args) { 6 | Tea tea = new Tea(); 7 | Coffee coffee = new Coffee(); 8 | System.out.println("Making tea..."); 9 | tea.prepareRecipe(); 10 | System.out.println("Making coffee..."); 11 | coffee.prepareRecipe(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HeadFirst设计模式 2 | 此项目是假期期间阅读headfirst设计模式进行总结 3 | 包含官方的java代码,部分C++代码,书本的pdf 4 | ![image](https://user-images.githubusercontent.com/82313079/154040088-19883071-3e64-489b-b04d-07b61779efdc.png) 5 | 6 | --------------------------------------------------------------------------------