├── test └── main │ ├── resources │ └── text.txt │ └── java │ └── com │ └── shawn │ └── patterns │ ├── AdapterPatternTest.java │ ├── CommandPatternTest.java │ ├── TemplatePatternTest.java │ ├── StrategyPatternTest.java │ ├── IteratorPatternTest.java │ ├── StatePatternTest.java │ ├── DecoratorPatternTest.java │ ├── ObserverPatternTest.java │ ├── FactoryPatternTest.java │ └── CompositePatternTest.java ├── .gitignore ├── src └── main │ └── java │ └── com │ ├── command │ └── demo │ │ ├── remote │ │ ├── Command.java │ │ ├── NoCommand.java │ │ ├── LightOnCommand.java │ │ ├── LightOffCommand.java │ │ ├── StereoOffCommand.java │ │ ├── LivingroomLightOffCommand.java │ │ ├── LivingroomLightOnCommand.java │ │ ├── HottubOffCommand.java │ │ ├── CeilingFanOffCommand.java │ │ ├── CeilingFanOnCommand.java │ │ ├── GarageDoorUpCommand.java │ │ ├── GarageDoorDownCommand.java │ │ ├── HottubOnCommand.java │ │ ├── StereoOnWithCDCommand.java │ │ ├── Light.java │ │ ├── TV.java │ │ ├── GarageDoor.java │ │ ├── Stereo.java │ │ ├── CeilingFan.java │ │ ├── Hottub.java │ │ ├── RemoteControl.java │ │ └── RemoteLoader.java │ │ ├── undo │ │ ├── Command.java │ │ ├── NoCommand.java │ │ ├── LightOnCommand.java │ │ ├── LightOffCommand.java │ │ ├── DimmerLightOnCommand.java │ │ ├── DimmerLightOffCommand.java │ │ ├── Light.java │ │ ├── CeilingFanLowCommand.java │ │ ├── CeilingFanOffCommand.java │ │ ├── CeilingFanHighCommand.java │ │ ├── CeilingFanMediumCommand.java │ │ ├── CeilingFan.java │ │ ├── RemoteControlWithUndo.java │ │ └── RemoteLoader.java │ │ ├── party │ │ ├── Command.java │ │ ├── NoCommand.java │ │ ├── TVOffCommand.java │ │ ├── TVOnCommand.java │ │ ├── LightOnCommand.java │ │ ├── LightOffCommand.java │ │ ├── StereoOnCommand.java │ │ ├── LivingroomLightOnCommand.java │ │ ├── StereoOffCommand.java │ │ ├── LivingroomLightOffCommand.java │ │ ├── HottubOffCommand.java │ │ ├── HottubOnCommand.java │ │ ├── StereoOnWithCDCommand.java │ │ ├── TV.java │ │ ├── MacroCommand.java │ │ ├── CeilingFanOffCommand.java │ │ ├── CeilingFanHighCommand.java │ │ ├── Light.java │ │ ├── CeilingFanMediumCommand.java │ │ ├── Stereo.java │ │ ├── Hottub.java │ │ ├── CeilingFan.java │ │ ├── RemoteLoader.java │ │ └── RemoteControl.java │ │ └── simple │ │ ├── RemoteControle.java │ │ ├── Command.java │ │ ├── NoCommand.java │ │ ├── Door.java │ │ ├── StereoOnWithCDCOmmad.java │ │ ├── Light.java │ │ ├── LightOnCommand.java │ │ ├── LightOffCommand.java │ │ ├── GarageDoorOpenCommand.java │ │ ├── SimpleRemoteControl.java │ │ ├── Stereo.java │ │ └── RemoteControl.java │ ├── adapter │ └── demo │ │ ├── Duck.java │ │ ├── Turkey.java │ │ ├── TurkeyAdapter.java │ │ ├── MallardDuck.java │ │ └── WildTurkey.java │ ├── factory │ └── demo │ │ ├── pizzastoreWithIngredient │ │ ├── Clams.java │ │ ├── Dough.java │ │ ├── Sauce.java │ │ ├── Cheese.java │ │ ├── Veggies.java │ │ ├── Pepperoni.java │ │ ├── Onion.java │ │ ├── Garlic.java │ │ ├── Spinach.java │ │ ├── Eggplant.java │ │ ├── Mushroom.java │ │ ├── RedPepper.java │ │ ├── BlackOlives.java │ │ ├── MarinaraSauce.java │ │ ├── ThinCrustDough.java │ │ ├── ParmesanCheese.java │ │ ├── ReggianoCheese.java │ │ ├── MozzarellaCheese.java │ │ ├── SlicedPepperoni.java │ │ ├── FreshClams.java │ │ ├── FrozenClams.java │ │ ├── PlumTomatoSauce.java │ │ ├── ThickCrustDough.java │ │ ├── PizzaIngredientFactory.java │ │ ├── PizzaStore.java │ │ ├── CheesePizza.java │ │ ├── ClamPizza.java │ │ ├── VeggiePizza.java │ │ ├── PepperoniPizza.java │ │ ├── NYPizzaIngredientFactory.java │ │ ├── ChicagoPizzaIngredientFactory.java │ │ ├── ChicagoPizzaStore.java │ │ ├── NYPizzaStore.java │ │ └── Pizza.java │ │ └── pizzastore │ │ ├── NYStyleClamPizza.java │ │ ├── NYStyleVeggiePizza.java │ │ ├── NyStylePepperoniPizza.java │ │ ├── SimplePizzaFactory.java │ │ ├── ChicagoCheesePizza.java │ │ ├── ChicagoStylePizzaStore.java │ │ ├── PizzaStore.java │ │ ├── NYStyleCheesePizza.java │ │ ├── NYStylePizzaStore.java │ │ └── Pizza.java │ ├── observer │ └── demo │ │ ├── weatherStationV2 │ │ ├── DisplayElement.java │ │ ├── WeatherStation.java │ │ ├── CurrentConditionDisplay.java │ │ ├── ForeConditionDisplay.java │ │ └── WeatherData.java │ │ ├── weatherStationV1 │ │ ├── DisplayElement.java │ │ ├── Observer.java │ │ ├── Subject.java │ │ ├── CurrentConditionDisplay.java │ │ └── WeatherData.java │ │ └── weatherStationWithEventBus │ │ ├── DisplayElement.java │ │ ├── CurrentConditionDisplayBySubscribe.java │ │ └── WeatherDataPOJO.java │ ├── iterator │ └── demo │ │ ├── Menu.java │ │ ├── Iterator.java │ │ ├── DinerMenuIterator.java │ │ ├── PancakeHouseMenuIterator.java │ │ ├── MenuItem.java │ │ ├── PancakeHouseMenu.java │ │ └── DinnerMenu.java │ ├── strategy │ └── demo │ │ ├── FlyBehavior.java │ │ ├── QuackBehavior.java │ │ ├── Quack.java │ │ ├── FlyNoWay.java │ │ ├── MuteQuack.java │ │ ├── FlyWithWings.java │ │ ├── RedheadDuck.java │ │ ├── MallardDuck.java │ │ └── Duck.java │ ├── state │ └── demo │ │ ├── State.java │ │ ├── NoQuarterState.java │ │ ├── SoldOutState.java │ │ ├── SoldState.java │ │ ├── HasQuarterState.java │ │ ├── WinnerState.java │ │ └── GumballMachine.java │ ├── di │ └── demo │ │ ├── AgentFinder.java │ │ ├── Agent.java │ │ ├── HollywoodServiceWithDI.java │ │ ├── HollywoodSerivice.java │ │ ├── HollywoodServiceWithFactory.java │ │ ├── AgentFinderFactory.java │ │ ├── WebServiceAgentFinder.java │ │ ├── SpreadsheetAgentFinder.java │ │ └── HollywoodServiceWithJSR330.java │ ├── decorator │ └── demo │ │ ├── buzzCoffee │ │ ├── CondimentDecorator.java │ │ ├── HouseBlend.java │ │ ├── Espresso.java │ │ ├── DarkRoast.java │ │ ├── Milk.java │ │ ├── Beverage.java │ │ └── Mocha.java │ │ └── io │ │ └── LowerCaseInputStream.java │ ├── composite │ └── demo │ │ ├── menu │ │ ├── Waitress.java │ │ ├── MenuComponent.java │ │ ├── MenuItem.java │ │ ├── Menu.java │ │ └── MenuTestDrive.java │ │ └── menuiterator │ │ ├── NullIterator.java │ │ ├── Waitress.java │ │ ├── MenuComponent.java │ │ ├── CompositeIterator.java │ │ ├── MenuItem.java │ │ ├── Menu.java │ │ └── MenuTestDrive.java │ ├── singleton │ └── demo │ │ ├── Singleton.java │ │ └── ChocolateBoiler.java │ └── template │ └── demo │ ├── Coffee.java │ ├── Tea.java │ ├── CaffineBeverageWithHook.java │ └── CoffeWithHook.java └── pom.xml /test/main/resources/text.txt: -------------------------------------------------------------------------------- 1 | RedheadDuck duck = new RedheadDuck(); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | bin 3 | target 4 | .classpath 5 | .project 6 | .settings 7 | src/main/webapp/META-INF 8 | .idea 9 | *.iml 10 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/Command.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public interface Command { 4 | public void execute(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/adapter/demo/Duck.java: -------------------------------------------------------------------------------- 1 | package com.adapter.demo; 2 | 3 | public interface Duck { 4 | public String quack(); 5 | public String fly(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/adapter/demo/Turkey.java: -------------------------------------------------------------------------------- 1 | package com.adapter.demo; 2 | 3 | public interface Turkey { 4 | public String gobble(); 5 | public String fly(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/Command.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public interface Command { 4 | public void execute(); 5 | public void undo(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/Command.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public interface Command { 4 | public void execute(); 5 | public void undo(); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/NoCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Clams.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Clams { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Dough.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Dough { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Sauce.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Sauce { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV2/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV2; 2 | 3 | public interface DisplayElement { 4 | public void display(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Cheese.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Cheese { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Veggies.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Veggies { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/NoCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | public void undo() { } 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Pepperoni.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public interface Pepperoni { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/NoCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | public void undo() { } 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/RemoteControle.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:14 7 | */ 8 | public class RemoteControle { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/Menu.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | /** 4 | * @author Shawn Cao 5 | */ 6 | public interface Menu { 7 | 8 | public Iterator createIterator(); 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/Iterator.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | /** 4 | * @author Shawn Cao 5 | */ 6 | public interface Iterator { 7 | public boolean hasNext(); 8 | 9 | T next(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Onion.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class Onion implements Veggies { 4 | 5 | public String toString() { 6 | return "Onion"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Garlic.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class Garlic implements Veggies { 4 | 5 | public String toString() { 6 | return "Garlic"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Spinach.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class Spinach implements Veggies { 4 | 5 | public String toString() { 6 | return "Spinach"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/FlyBehavior.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:14 7 | */ 8 | public interface FlyBehavior { 9 | public String fly(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Eggplant.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class Eggplant implements Veggies { 4 | 5 | public String toString() { 6 | return "Eggplant"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Mushroom.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class Mushroom implements Veggies { 4 | 5 | public String toString() { 6 | return "Mushrooms"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/QuackBehavior.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:14 7 | */ 8 | public interface QuackBehavior { 9 | public String quack(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/NYStyleClamPizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:30 7 | */ 8 | public class NYStyleClamPizza extends Pizza { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/RedPepper.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class RedPepper implements Veggies { 4 | 5 | public String toString() { 6 | return "Red Pepper"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV1/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV1; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM5:26 7 | */ 8 | public interface DisplayElement { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/Command.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: AM10:43 7 | */ 8 | public interface Command { 9 | 10 | public String execute(); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/NYStyleVeggiePizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:30 7 | */ 8 | public class NYStyleVeggiePizza extends Pizza { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/BlackOlives.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class BlackOlives implements Veggies { 4 | 5 | public String toString() { 6 | return "Black Olives"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/MarinaraSauce.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class MarinaraSauce implements Sauce { 4 | public String toString() { 5 | return "Marinara Sauce"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ThinCrustDough.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ThinCrustDough implements Dough { 4 | public String toString() { 5 | return "Thin Crust Dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/NyStylePepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:30 7 | */ 8 | public class NyStylePepperoniPizza extends Pizza { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ParmesanCheese.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ParmesanCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Parmesan"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ReggianoCheese.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ReggianoCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Reggiano Cheese"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/MozzarellaCheese.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class MozzarellaCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Mozzarella"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/SlicedPepperoni.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class SlicedPepperoni implements Pepperoni { 4 | 5 | public String toString() { 6 | return "Sliced Pepperoni"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/FreshClams.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class FreshClams implements Clams { 4 | 5 | public String toString() { 6 | return "Fresh Clams from Long Island Sound"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/FrozenClams.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class FrozenClams implements Clams { 4 | 5 | public String toString() { 6 | return "Frozen Clams from Chesapeake Bay"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/PlumTomatoSauce.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class PlumTomatoSauce implements Sauce { 4 | public String toString() { 5 | return "Tomato sauce with plum tomatoes"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/State.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 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 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ThickCrustDough.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ThickCrustDough implements Dough { 4 | public String toString() { 5 | return "ThickCrust style extra thick crust dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/AgentFinder.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * User: Shawn cao 7 | * Date: 13-9-16 8 | * Time: PM9:48 9 | */ 10 | 11 | public interface AgentFinder { 12 | 13 | public List findAllAgents(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV1/Observer.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV1; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM5:16 7 | */ 8 | public interface Observer { 9 | public void update(float temp, float humidity, float pressure); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationWithEventBus/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationWithEventBus; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM5:26 7 | */ 8 | public interface DisplayElement { 9 | 10 | public String display(); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/Quack.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:32 7 | */ 8 | public class Quack implements QuackBehavior { 9 | @Override 10 | public String quack() { 11 | return "Quack"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/NoCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:16 7 | */ 8 | public class NoCommand implements Command { 9 | @Override 10 | public String execute() { 11 | return null; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/CondimentDecorator.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM5:03 7 | */ 8 | public abstract class CondimentDecorator extends Beverage { 9 | public abstract String getDescription(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/FlyNoWay.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:29 7 | */ 8 | public class FlyNoWay implements FlyBehavior { 9 | @Override 10 | public String fly() { 11 | return "I can't fly"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/MuteQuack.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:32 7 | */ 8 | public class MuteQuack implements QuackBehavior { 9 | @Override 10 | public String quack() { 11 | return "silence"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menu/Waitress.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/FlyWithWings.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:28 7 | */ 8 | public class FlyWithWings implements FlyBehavior { 9 | @Override 10 | public String fly() { 11 | return "I'm flying"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/SimplePizzaFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM2:56 7 | */ 8 | public class SimplePizzaFactory { 9 | public static Pizza createPizza(String type) { 10 | return null; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/StereoOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/Door.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM12:04 7 | */ 8 | public class Door { 9 | 10 | public String rollUp(){ 11 | 12 | System.out.println("door roll up"); 13 | return "roll up"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/TVOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/LivingroomLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/LivingroomLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/ChicagoCheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:44 7 | */ 8 | public class ChicagoCheesePizza extends Pizza { 9 | 10 | public ChicagoCheesePizza() { 11 | name = "Chicago cheese pizza"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/RedheadDuck.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:37 7 | */ 8 | public class RedheadDuck extends Duck { 9 | 10 | public RedheadDuck(){ 11 | setFlyBehavior(new FlyNoWay()); 12 | setQuackBehavior(new Quack()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/HottubOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/CeilingFanOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/CeilingFanOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/TVOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/GarageDoorUpCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/StereoOnWithCDCOmmad.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:10 7 | */ 8 | public class StereoOnWithCDCOmmad implements Command { 9 | 10 | Stereo stereo; 11 | @Override 12 | public String execute() { 13 | return null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV1/Subject.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV1; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM5:17 7 | */ 8 | public interface Subject { 9 | public void registerObserver(Observer o); 10 | public void removeObserver(Observer o); 11 | public void notifyObservers(); 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:25 7 | */ 8 | public class MallardDuck extends Duck{ 9 | 10 | public MallardDuck(){ 11 | super.setQuackBehavior(new MuteQuack()); 12 | super.setFlyBehavior(new FlyWithWings()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/GarageDoorDownCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/HottubOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/singleton/demo/Singleton.java: -------------------------------------------------------------------------------- 1 | package com.singleton.demo; 2 | 3 | public class Singleton { 4 | private static Singleton uniqueSingleton; 5 | private Singleton(){ 6 | 7 | } 8 | 9 | public static Singleton getInstance(){ 10 | if(uniqueSingleton == null){ 11 | uniqueSingleton = new Singleton(); 12 | } 13 | return uniqueSingleton; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/StereoOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/LivingroomLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/StereoOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/LivingroomLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/StereoOnWithCDCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/Light.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM5:07 7 | */ 8 | public class HouseBlend extends Beverage { 9 | 10 | public HouseBlend(){ 11 | description = "HouseBlend"; 12 | } 13 | @Override 14 | public double cost() { 15 | return .89; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/HottubOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/Espresso.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM5:06 7 | */ 8 | public class Espresso extends Beverage { 9 | 10 | public Espresso(){ 11 | super.description = "Espresso"; 12 | } 13 | 14 | @Override 15 | public double cost() { 16 | return 1.99; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/PizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 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 | -------------------------------------------------------------------------------- /src/main/java/com/adapter/demo/TurkeyAdapter.java: -------------------------------------------------------------------------------- 1 | package com.adapter.demo; 2 | 3 | public class TurkeyAdapter implements Duck{ 4 | 5 | Turkey turkey; 6 | public TurkeyAdapter(Turkey turkey){ 7 | this.turkey = turkey; 8 | } 9 | @Override 10 | public String quack() { 11 | return turkey.gobble(); 12 | } 13 | 14 | @Override 15 | public String fly() { 16 | return turkey.fly(); 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/HottubOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/Light.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: AM10:44 7 | */ 8 | public class Light { 9 | public String on() { 10 | System.out.println("lights on "); 11 | return "lights on"; 12 | } 13 | 14 | public String off() { 15 | System.out.println("lights off"); 16 | return "lights off"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/StereoOnWithCDCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: AM10:44 7 | */ 8 | public class LightOnCommand implements Command { 9 | Light light; 10 | 11 | public LightOnCommand(Light light) { 12 | this.light = light; 13 | } 14 | 15 | @Override 16 | public String execute() { 17 | return light.on(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/DarkRoast.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM4:37 7 | */ 8 | public class DarkRoast extends Beverage { 9 | Beverage beverage; 10 | 11 | public DarkRoast(Beverage beverage) { 12 | this.beverage = beverage; 13 | } 14 | 15 | @Override 16 | public double cost() { 17 | return 1.45; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/DimmerLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:08 7 | */ 8 | public class LightOffCommand implements Command { 9 | 10 | Light light; 11 | 12 | public LightOffCommand(Light light) { 13 | this.light = light; 14 | } 15 | 16 | @Override 17 | public String execute() { 18 | return light.off(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/adapter/demo/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package com.adapter.demo; 2 | 3 | public class MallardDuck implements Duck{ 4 | 5 | @Override 6 | public String quack() { 7 | String quack = "mallaradDuck quack"; 8 | System.out.println(quack); 9 | return quack; 10 | } 11 | 12 | @Override 13 | public String fly() { 14 | String fly = "mallarad duck fly"; 15 | System.out.println(fly); 16 | return fly; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/GarageDoorOpenCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM12:03 7 | */ 8 | public class GarageDoorOpenCommand implements Command { 9 | 10 | Door door; 11 | public GarageDoorOpenCommand(Door door){ 12 | this.door = door; 13 | } 14 | 15 | @Override 16 | public String execute() { 17 | 18 | return door.rollUp(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/ChicagoStylePizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:18 7 | */ 8 | public class ChicagoStylePizzaStore extends PizzaStore { 9 | @Override 10 | Pizza createPizza(PizzaType type) { 11 | if(type.equals(PizzaType.cheese)) 12 | return new ChicagoCheesePizza(); 13 | else 14 | return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/DimmerLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/adapter/demo/WildTurkey.java: -------------------------------------------------------------------------------- 1 | package com.adapter.demo; 2 | 3 | public class WildTurkey implements Turkey{ 4 | 5 | 6 | @Override 7 | public String fly() { 8 | String fly = "Flying with a long distance"; 9 | System.out.println(fly); 10 | return fly; 11 | } 12 | 13 | @Override 14 | public String gobble() { 15 | String gobble = " Gobble gobble"; 16 | System.out.println(gobble); 17 | return gobble; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/SimpleRemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: AM11:54 7 | */ 8 | public class SimpleRemoteControl { 9 | 10 | Command slot; 11 | 12 | public SimpleRemoteControl() { 13 | } 14 | 15 | public void setCommand(Command command){ 16 | slot = command; 17 | } 18 | 19 | public String buttonWasPressed(){ 20 | return slot.execute(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 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 | -------------------------------------------------------------------------------- /src/main/java/com/template/demo/Coffee.java: -------------------------------------------------------------------------------- 1 | package com.template.demo; 2 | 3 | public class Coffee extends CaffineBeverageWithHook{ 4 | 5 | @Override 6 | public String brew() { 7 | String brew = " Dripping coffee through filter"; 8 | System.out.println(brew); 9 | return brew; 10 | } 11 | @Override 12 | public String addCondiments() { 13 | String condiments = "sugar and milk"; 14 | System.out.println(condiments); 15 | return condiments; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/TV.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.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 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/Stereo.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:11 7 | */ 8 | public class Stereo { 9 | 10 | Integer volume; 11 | public String on(){ 12 | System.out.println("stereo on"); 13 | return "stereo on"; 14 | } 15 | 16 | public void setCD(){ 17 | System.out.println("set cd on stereo"); 18 | } 19 | 20 | public void setVolume(Integer volume){ 21 | this.volume = volume; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/TV.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 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(location + " TV is on"); 13 | } 14 | 15 | public void off() { 16 | System.out.println(location + " TV is off"); 17 | } 18 | 19 | public void setInputChannel() { 20 | this.channel = 3; 21 | System.out.println(location + " TV channel is set for DVD"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/Agent.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 13-9-16 6 | * Time: PM9:49 7 | */ 8 | public class Agent { 9 | 10 | private String type; 11 | 12 | @Override 13 | public String toString() { 14 | return "Agent{" + 15 | "type='" + type + '\'' + 16 | '}'; 17 | } 18 | 19 | public void setType(String type) { 20 | this.type = type; 21 | } 22 | 23 | public String getType() { 24 | 25 | return type; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/template/demo/Tea.java: -------------------------------------------------------------------------------- 1 | package com.template.demo; 2 | 3 | public class Tea extends CaffineBeverageWithHook{ 4 | @Override 5 | public String brew() { 6 | String brew = " Steeping the tea "; 7 | System.out.println(brew); 8 | return brew; 9 | } 10 | 11 | @Override 12 | public String addCondiments() { 13 | String condiments = "lemon"; 14 | System.out.println(condiments); 15 | return condiments; 16 | } 17 | 18 | @Override 19 | boolean customerWantsCondiments() { 20 | return false; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM2:53 7 | */ 8 | public abstract class PizzaStore { 9 | 10 | public enum PizzaType {cheese, veggie,clam, pepperoni} 11 | 12 | public Pizza orderPizza(PizzaType type) { 13 | 14 | Pizza pizza = createPizza(type); 15 | 16 | pizza.prepare(); 17 | pizza.bake(); 18 | pizza.box(); 19 | return pizza; 20 | } 21 | 22 | abstract Pizza createPizza(PizzaType type); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/CheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class CheesePizza extends Pizza { 4 | PizzaIngredientFactory ingredientFactory; 5 | 6 | public CheesePizza(PizzaIngredientFactory ingredientFactory) { 7 | this.ingredientFactory = ingredientFactory; 8 | } 9 | 10 | void prepare() { 11 | System.out.println("Preparing " + name); 12 | dough = ingredientFactory.createDough(); 13 | sauce = ingredientFactory.createSauce(); 14 | cheese = ingredientFactory.createCheese(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV2/WeatherStation.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV2; 2 | 3 | public class WeatherStation { 4 | public static void main(String[] args) { 5 | WeatherData weatherData = new WeatherData(); 6 | CurrentConditionDisplay currentConditionDisplay = new CurrentConditionDisplay(weatherData); 7 | ForeConditionDisplay foreConditionDisplay = new ForeConditionDisplay(weatherData); 8 | 9 | weatherData.setMeasurement(80, 89, 98f); 10 | currentConditionDisplay.display(); 11 | foreConditionDisplay.display(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/NullIterator.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.Iterator; 4 | 5 | public class NullIterator implements Iterator { 6 | 7 | public MenuComponent next() { 8 | return null; 9 | } 10 | 11 | public boolean hasNext() { 12 | return false; 13 | } 14 | 15 | /* 16 | * No longer needed as of Java 8 17 | * 18 | * (non-Javadoc) 19 | * @see java.util.Iterator#remove() 20 | * 21 | public void remove() { 22 | throw new UnsupportedOperationException(); 23 | } 24 | */ 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/Milk.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM5:11 7 | */ 8 | public class Milk extends CondimentDecorator { 9 | Beverage beverage; 10 | 11 | public Milk(Beverage beverage) { 12 | this.beverage = beverage; 13 | } 14 | 15 | @Override 16 | public double cost() { 17 | return 0.33 + beverage.cost(); 18 | } 19 | 20 | @Override 21 | public String getDescription() { 22 | return beverage.getDescription() + ", milk"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/NYStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:30 7 | */ 8 | public class NYStyleCheesePizza extends Pizza { 9 | 10 | public NYStyleCheesePizza() { 11 | name = "NY Style Sauce and Cheese Pizza"; 12 | dough = "Thin Crust Dough"; 13 | sause = "Marinara Sauce"; 14 | toppings.add("Grated Reggiamo Cheese"); 15 | } 16 | 17 | public void cut(){ 18 | System.out.println("Cutting the pizza into square"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ClamPizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ClamPizza extends Pizza { 4 | PizzaIngredientFactory ingredientFactory; 5 | 6 | public ClamPizza(PizzaIngredientFactory ingredientFactory) { 7 | this.ingredientFactory = ingredientFactory; 8 | } 9 | 10 | void prepare() { 11 | System.out.println("Preparing " + name); 12 | dough = ingredientFactory.createDough(); 13 | sauce = ingredientFactory.createSauce(); 14 | cheese = ingredientFactory.createCheese(); 15 | clam = ingredientFactory.createClam(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/VeggiePizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class VeggiePizza extends Pizza { 4 | PizzaIngredientFactory ingredientFactory; 5 | 6 | public VeggiePizza(PizzaIngredientFactory ingredientFactory) { 7 | this.ingredientFactory = ingredientFactory; 8 | } 9 | 10 | void prepare() { 11 | System.out.println("Preparing " + name); 12 | dough = ingredientFactory.createDough(); 13 | sauce = ingredientFactory.createSauce(); 14 | cheese = ingredientFactory.createCheese(); 15 | veggies = ingredientFactory.createVeggies(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/template/demo/CaffineBeverageWithHook.java: -------------------------------------------------------------------------------- 1 | package com.template.demo; 2 | 3 | public abstract class CaffineBeverageWithHook { 4 | 5 | final void prepareRecipe() { 6 | boilWater(); 7 | brew(); 8 | pourInCup(); 9 | if (customerWantsCondiments()) { 10 | addCondiments(); 11 | } 12 | } 13 | 14 | public abstract String brew(); 15 | 16 | public abstract String addCondiments(); 17 | 18 | void boilWater() { 19 | System.out.println("Boiling water"); 20 | } 21 | 22 | void pourInCup() { 23 | System.out.println("Pouring into cup"); 24 | } 25 | 26 | boolean customerWantsCondiments() { 27 | return true; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/Beverage.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM4:35 7 | */ 8 | public abstract class Beverage { 9 | 10 | String description = "Unknown Beverage"; 11 | 12 | public enum Size {tall, grande, venti} 13 | Size size = Size.tall; 14 | public abstract double cost(); 15 | 16 | 17 | public void setSize(Size size) { 18 | this.size = size; 19 | } 20 | 21 | public Size getSize() { 22 | return size; 23 | } 24 | 25 | public String getDescription(){ 26 | return description; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/MacroCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class MacroCommand implements Command { 4 | Command[] commands; 5 | 6 | public MacroCommand(Command[] commands) { 7 | this.commands = commands; 8 | } 9 | 10 | public void execute() { 11 | for (int i = 0; i < commands.length; i++) { 12 | commands[i].execute(); 13 | } 14 | } 15 | 16 | /** 17 | * NOTE: these commands have to be done backwards to ensure 18 | * proper undo functionality 19 | */ 20 | public void undo() { 21 | for (int i = commands.length -1; i >= 0; i--) { 22 | commands[i].undo(); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/DinerMenuIterator.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | /** 4 | * @author Shawn Cao 5 | */ 6 | public class DinerMenuIterator implements Iterator { 7 | int current = 0; 8 | 9 | MenuItem[] menuItems; 10 | int size = 0; 11 | public DinerMenuIterator(MenuItem[] menuItems) { 12 | this.menuItems = menuItems; 13 | size = menuItems.length; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return size>0; 19 | } 20 | 21 | @Override 22 | public MenuItem next() { 23 | size --; 24 | return menuItems[current++]; 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/PepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class PepperoniPizza extends Pizza { 4 | PizzaIngredientFactory ingredientFactory; 5 | 6 | public PepperoniPizza(PizzaIngredientFactory ingredientFactory) { 7 | this.ingredientFactory = ingredientFactory; 8 | } 9 | 10 | void prepare() { 11 | System.out.println("Preparing " + name); 12 | dough = ingredientFactory.createDough(); 13 | sauce = ingredientFactory.createSauce(); 14 | cheese = ingredientFactory.createCheese(); 15 | veggies = ingredientFactory.createVeggies(); 16 | pepperoni = ingredientFactory.createPepperoni(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/CeilingFanOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class CeilingFanOffCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanOffCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | public void execute() { 11 | prevSpeed = ceilingFan.getSpeed(); 12 | ceilingFan.off(); 13 | } 14 | public void undo() { 15 | switch (prevSpeed) { 16 | case CeilingFan.HIGH: ceilingFan.high(); break; 17 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 18 | case CeilingFan.LOW: ceilingFan.low(); break; 19 | default: ceilingFan.off(); break; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/Light.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class Light { 4 | String location; 5 | int level; 6 | 7 | public Light(String location) { 8 | this.location = location; 9 | } 10 | 11 | public void on() { 12 | level = 100; 13 | System.out.println("Light is on"); 14 | } 15 | 16 | public void off() { 17 | level = 0; 18 | System.out.println("Light is off"); 19 | } 20 | 21 | public void dim(int level) { 22 | this.level = level; 23 | if (level == 0) { 24 | off(); 25 | } 26 | else { 27 | System.out.println("Light is dimmed to " + level + "%"); 28 | } 29 | } 30 | 31 | public int getLevel() { 32 | return level; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/CeilingFanHighCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class CeilingFanHighCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanHighCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | public void execute() { 11 | prevSpeed = ceilingFan.getSpeed(); 12 | ceilingFan.high(); 13 | } 14 | public void undo() { 15 | switch (prevSpeed) { 16 | case CeilingFan.HIGH: ceilingFan.high(); break; 17 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 18 | case CeilingFan.LOW: ceilingFan.low(); break; 19 | default: ceilingFan.off(); break; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/Light.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class Light { 4 | String location; 5 | int level; 6 | 7 | public Light(String location) { 8 | this.location = location; 9 | } 10 | 11 | public void on() { 12 | level = 100; 13 | System.out.println("Light is on"); 14 | } 15 | 16 | public void off() { 17 | level = 0; 18 | System.out.println("Light is off"); 19 | } 20 | 21 | public void dim(int level) { 22 | this.level = level; 23 | if (level == 0) { 24 | off(); 25 | } 26 | else { 27 | System.out.println("Light is dimmed to " + level + "%"); 28 | } 29 | } 30 | 31 | public int getLevel() { 32 | return level; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/CeilingFanMediumCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class CeilingFanMediumCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanMediumCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | public void execute() { 11 | prevSpeed = ceilingFan.getSpeed(); 12 | ceilingFan.medium(); 13 | } 14 | public void undo() { 15 | switch (prevSpeed) { 16 | case CeilingFan.HIGH: ceilingFan.high(); break; 17 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 18 | case CeilingFan.LOW: ceilingFan.low(); break; 19 | default: ceilingFan.off(); break; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/GarageDoor.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class GarageDoor { 4 | String location; 5 | 6 | public GarageDoor(String location) { 7 | this.location = location; 8 | } 9 | 10 | public void up() { 11 | System.out.println(location + " garage Door is Up"); 12 | } 13 | 14 | public void down() { 15 | System.out.println(location + " garage Door is Down"); 16 | } 17 | 18 | public void stop() { 19 | System.out.println(location + " garage Door is Stopped"); 20 | } 21 | 22 | public void lightOn() { 23 | System.out.println(location + " garage light is on"); 24 | } 25 | 26 | public void lightOff() { 27 | System.out.println(location + " garage light is off"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/CeilingFanLowCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class CeilingFanLowCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanLowCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | 11 | public void execute() { 12 | prevSpeed = ceilingFan.getSpeed(); 13 | ceilingFan.low(); 14 | } 15 | 16 | public void undo() { 17 | if (prevSpeed == CeilingFan.HIGH) { 18 | ceilingFan.high(); 19 | } else if (prevSpeed == CeilingFan.MEDIUM) { 20 | ceilingFan.medium(); 21 | } else if (prevSpeed == CeilingFan.LOW) { 22 | ceilingFan.low(); 23 | } else if (prevSpeed == CeilingFan.OFF) { 24 | ceilingFan.off(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/CeilingFanOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class CeilingFanOffCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanOffCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | 11 | public void execute() { 12 | prevSpeed = ceilingFan.getSpeed(); 13 | ceilingFan.off(); 14 | } 15 | 16 | public void undo() { 17 | if (prevSpeed == CeilingFan.HIGH) { 18 | ceilingFan.high(); 19 | } else if (prevSpeed == CeilingFan.MEDIUM) { 20 | ceilingFan.medium(); 21 | } else if (prevSpeed == CeilingFan.LOW) { 22 | ceilingFan.low(); 23 | } else if (prevSpeed == CeilingFan.OFF) { 24 | ceilingFan.off(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/AdapterPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.adapter.demo.Duck; 4 | import com.adapter.demo.Turkey; 5 | import com.adapter.demo.TurkeyAdapter; 6 | import com.adapter.demo.WildTurkey; 7 | import org.junit.Test; 8 | 9 | import static org.junit.Assert.assertTrue; 10 | 11 | /** 12 | * User: Shawn cao 13 | * Date: 15/2/3 14 | * Time: PM3:16 15 | */ 16 | public class AdapterPatternTest { 17 | 18 | @Test 19 | public void test_turkey_adapt_to_duck() { 20 | Turkey wildTurkey = new WildTurkey(); 21 | Duck adapter = new TurkeyAdapter(wildTurkey); 22 | assertTrue(adapter.fly().toLowerCase().contains("fly")); 23 | assertTrue(adapter.quack().contains("gobble")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/CeilingFanHighCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class CeilingFanHighCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanHighCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | 11 | public void execute() { 12 | prevSpeed = ceilingFan.getSpeed(); 13 | ceilingFan.high(); 14 | } 15 | 16 | public void undo() { 17 | if (prevSpeed == CeilingFan.HIGH) { 18 | ceilingFan.high(); 19 | } else if (prevSpeed == CeilingFan.MEDIUM) { 20 | ceilingFan.medium(); 21 | } else if (prevSpeed == CeilingFan.LOW) { 22 | ceilingFan.low(); 23 | } else if (prevSpeed == CeilingFan.OFF) { 24 | ceilingFan.off(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/PancakeHouseMenuIterator.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * @author Shawn Cao 7 | */ 8 | public class PancakeHouseMenuIterator implements Iterator { 9 | 10 | ArrayList menuItems; 11 | int position = 0; 12 | int size = 0 ; 13 | public PancakeHouseMenuIterator(ArrayList menuItems) { 14 | this.menuItems = menuItems; 15 | size = menuItems.size(); 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | if(size > 0) return true; 21 | return false; 22 | } 23 | 24 | @Override 25 | public MenuItem next() { 26 | size --; 27 | return menuItems.get(position++); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/CeilingFanMediumCommand.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class CeilingFanMediumCommand implements Command { 4 | CeilingFan ceilingFan; 5 | int prevSpeed; 6 | 7 | public CeilingFanMediumCommand(CeilingFan ceilingFan) { 8 | this.ceilingFan = ceilingFan; 9 | } 10 | 11 | public void execute() { 12 | prevSpeed = ceilingFan.getSpeed(); 13 | ceilingFan.medium(); 14 | } 15 | 16 | public void undo() { 17 | if (prevSpeed == CeilingFan.HIGH) { 18 | ceilingFan.high(); 19 | } else if (prevSpeed == CeilingFan.MEDIUM) { 20 | ceilingFan.medium(); 21 | } else if (prevSpeed == CeilingFan.LOW) { 22 | ceilingFan.low(); 23 | } else if (prevSpeed == CeilingFan.OFF) { 24 | ceilingFan.off(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/NYStylePizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/30 6 | * Time: PM3:17 7 | */ 8 | public class NYStylePizzaStore extends PizzaStore { 9 | @Override 10 | Pizza createPizza(PizzaType type) { 11 | if(type.equals(PizzaType.cheese)){ 12 | return new NYStyleCheesePizza(); 13 | } else if (type.equals(PizzaType.veggie)){ 14 | return new NYStyleVeggiePizza(); 15 | } else if (type.equals(PizzaType.clam)){ 16 | return new NYStyleClamPizza(); 17 | } else if(type.equals(PizzaType.pepperoni)){ 18 | return new NyStylePepperoniPizza(); 19 | } else { 20 | return null; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/Waitress.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Waitress { 6 | MenuComponent allMenus; 7 | 8 | public Waitress(MenuComponent allMenus) { 9 | this.allMenus = allMenus; 10 | } 11 | 12 | public void printMenu() { 13 | allMenus.print(); 14 | } 15 | 16 | public void printVegetarianMenu() { 17 | Iterator iterator = allMenus.createIterator(); 18 | 19 | System.out.println("\nVEGETARIAN MENU\n----"); 20 | while (iterator.hasNext()) { 21 | MenuComponent menuComponent = iterator.next(); 22 | try { 23 | if (menuComponent.isVegetarian()) { 24 | menuComponent.print(); 25 | } 26 | } catch (UnsupportedOperationException e) {} 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/NYPizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class NYPizzaIngredientFactory implements PizzaIngredientFactory { 4 | 5 | public Dough createDough() { 6 | return new ThinCrustDough(); 7 | } 8 | 9 | public Sauce createSauce() { 10 | return new MarinaraSauce(); 11 | } 12 | 13 | public Cheese createCheese() { 14 | return new ReggianoCheese(); 15 | } 16 | 17 | public Veggies[] createVeggies() { 18 | Veggies veggies[] = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() }; 19 | return veggies; 20 | } 21 | 22 | public Pepperoni createPepperoni() { 23 | return new SlicedPepperoni(); 24 | } 25 | 26 | public Clams createClam() { 27 | return new FreshClams(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/CommandPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.command.demo.simple.*; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | /** 9 | * User: Shawn cao 10 | * Date: 15/2/2 11 | * Time: AM11:56 12 | */ 13 | public class CommandPatternTest { 14 | 15 | @Test 16 | public void test_remote_return_light_on(){ 17 | SimpleRemoteControl control = new SimpleRemoteControl(); 18 | LightOnCommand command = new LightOnCommand(new Light()); 19 | 20 | control.setCommand(command); 21 | assertEquals("lights on", control.buttonWasPressed()); 22 | 23 | control.setCommand(new GarageDoorOpenCommand(new Door())); 24 | 25 | assertEquals("roll up", control.buttonWasPressed()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationWithEventBus/CurrentConditionDisplayBySubscribe.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationWithEventBus; 2 | 3 | 4 | import com.google.common.eventbus.Subscribe; 5 | 6 | 7 | public class CurrentConditionDisplayBySubscribe implements DisplayElement{ 8 | 9 | private float temp; 10 | private float humidity; 11 | private float pressure; 12 | 13 | @Override 14 | public String display() { 15 | return "Current conditions:" + temp + "F degrees and " + humidity + "% humidity" + pressure; 16 | } 17 | 18 | @Subscribe 19 | public void onMessage(WeatherDataPOJO data) { 20 | System.out.println(data.toString()); 21 | this.temp = data.getTemperature() ; 22 | this.humidity = data.getHumidity(); 23 | this.pressure = data.getPressure(); 24 | display(); 25 | } 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/HollywoodServiceWithDI.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 13-9-16 10 | * Time: PM11:58 11 | */ 12 | public class HollywoodServiceWithDI { 13 | 14 | public static List getFriendlyAgents(AgentFinder finder){ 15 | List agents = finder.findAllAgents(); 16 | return filterAgents(agents,"java developer"); 17 | } 18 | 19 | public static List filterAgents(List agents,String agentType){ 20 | List filterAgents = Lists.newArrayList(); 21 | for (Agent agent:agents){ 22 | if(agent.getType().equals(agentType)){ 23 | filterAgents.add(agent); 24 | } 25 | } 26 | return filterAgents ; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/strategy/demo/Duck.java: -------------------------------------------------------------------------------- 1 | package com.strategy.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM12:14 7 | */ 8 | public abstract class Duck { 9 | 10 | QuackBehavior quackBehavior; 11 | FlyBehavior flyBehavior; 12 | 13 | public void setQuackBehavior(QuackBehavior behavior){ 14 | this.quackBehavior = behavior; 15 | 16 | } 17 | public void setFlyBehavior(FlyBehavior behavior){ 18 | this.flyBehavior = behavior; 19 | } 20 | public String performQuack(){ 21 | return this.quackBehavior.quack(); 22 | } 23 | 24 | public String performFly(){ 25 | return this.flyBehavior.fly(); 26 | } 27 | 28 | public String display(){ 29 | return this.getClass().getName(); 30 | } 31 | 32 | public String swim(){ 33 | return "swim"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ChicagoPizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ChicagoPizzaIngredientFactory 4 | implements PizzaIngredientFactory 5 | { 6 | 7 | public Dough createDough() { 8 | return new ThickCrustDough(); 9 | } 10 | 11 | public Sauce createSauce() { 12 | return new PlumTomatoSauce(); 13 | } 14 | 15 | public Cheese createCheese() { 16 | return new MozzarellaCheese(); 17 | } 18 | 19 | public Veggies[] createVeggies() { 20 | Veggies veggies[] = { new BlackOlives(), 21 | new Spinach(), 22 | new Eggplant() }; 23 | return veggies; 24 | } 25 | 26 | public Pepperoni createPepperoni() { 27 | return new SlicedPepperoni(); 28 | } 29 | 30 | public Clams createClam() { 31 | return new FrozenClams(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/buzzCoffee/Mocha.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.buzzCoffee; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/29 6 | * Time: PM5:08 7 | */ 8 | public class Mocha extends CondimentDecorator { 9 | 10 | Beverage beverage; 11 | 12 | public Mocha(Beverage beverage){ 13 | this.beverage = beverage; 14 | } 15 | 16 | @Override 17 | public double cost() { 18 | double cost = beverage.cost(); 19 | if(beverage.getSize().equals(Size.venti)) { 20 | return .40 + cost; 21 | }else if(beverage.getSize().equals(Size.grande)){ 22 | return .30 + cost; 23 | }else { 24 | return .20 + cost; 25 | } 26 | } 27 | 28 | @Override 29 | public String getDescription() { 30 | return beverage.getDescription() + ", mocha " + beverage.getSize().name(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/TemplatePatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.template.demo.CaffineBeverageWithHook; 4 | import com.template.demo.Coffee; 5 | import com.template.demo.Tea; 6 | import org.junit.Test; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | /** 11 | * @author Shawn Cao 12 | */ 13 | public class TemplatePatternTest { 14 | 15 | @Test 16 | public void testMakingTeaAndCoffee() { 17 | CaffineBeverageWithHook beverageWithHook = new Tea(); 18 | assertEquals(" Steeping the tea ",beverageWithHook.brew()); 19 | assertEquals("lemon",beverageWithHook.addCondiments()); 20 | 21 | beverageWithHook = new Coffee(); 22 | assertEquals("Dripping coffee through filter", beverageWithHook.brew().trim()); 23 | assertEquals("sugar and milk",beverageWithHook.addCondiments().trim()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV2/CurrentConditionDisplay.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV2; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | public class CurrentConditionDisplay implements Observer,DisplayElement{ 7 | 8 | Observable observable; 9 | private float temp; 10 | private float humidity; 11 | 12 | public CurrentConditionDisplay(Observable observable) { 13 | this.observable = observable; 14 | observable.addObserver(this); 15 | } 16 | @Override 17 | public void display() { 18 | System.out.println("temp: " + temp + " humidity: " + humidity); 19 | } 20 | 21 | @Override 22 | public void update(Observable o, Object arg) { 23 | if(o instanceof WeatherData){ 24 | WeatherData data = (WeatherData)o; 25 | this.temp = data.getTemp(); 26 | this.humidity = data.getHumidity(); 27 | display(); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/NoQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | public class NoQuarterState implements State { 4 | GumballMachine gumballMachine; 5 | 6 | public NoQuarterState(GumballMachine gumballMachine) { 7 | this.gumballMachine = gumballMachine; 8 | } 9 | 10 | public void insertQuarter() { 11 | System.out.println("You inserted a quarter"); 12 | gumballMachine.setState(gumballMachine.getHasQuarterState()); 13 | } 14 | 15 | public void ejectQuarter() { 16 | System.out.println("You haven't inserted a quarter"); 17 | } 18 | 19 | public void turnCrank() { 20 | System.out.println("You turned, but there's no quarter"); 21 | } 22 | 23 | public void dispense() { 24 | System.out.println("You need to pay first"); 25 | } 26 | 27 | public void refill() { } 28 | 29 | public String toString() { 30 | return "waiting for quarter"; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV1/CurrentConditionDisplay.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV1; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/1/26 6 | * Time: PM5:15 7 | */ 8 | public class CurrentConditionDisplay implements Observer,DisplayElement { 9 | 10 | private float temperature; 11 | private float humidity; 12 | private float pressure; 13 | 14 | 15 | public CurrentConditionDisplay(Subject weatherData){ 16 | weatherData.registerObserver(this); 17 | } 18 | 19 | public String display() { 20 | return "Current conditions:" + temperature + "F degrees and " + humidity + "% humidity" + pressure; 21 | } 22 | 23 | @Override 24 | public void update(float temp, float humidity, float pressure) { 25 | this.temperature = temp; 26 | this.humidity = humidity; 27 | this.pressure = pressure; 28 | display(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menu/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menu; 2 | 3 | public abstract class MenuComponent { 4 | 5 | public void add(MenuComponent menuComponent) { 6 | throw new UnsupportedOperationException(); 7 | } 8 | public void remove(MenuComponent menuComponent) { 9 | throw new UnsupportedOperationException(); 10 | } 11 | public MenuComponent getChild(int i) { 12 | throw new UnsupportedOperationException(); 13 | } 14 | 15 | public String getName() { 16 | throw new UnsupportedOperationException(); 17 | } 18 | public String getDescription() { 19 | throw new UnsupportedOperationException(); 20 | } 21 | public double getPrice() { 22 | throw new UnsupportedOperationException(); 23 | } 24 | public boolean isVegetarian() { 25 | throw new UnsupportedOperationException(); 26 | } 27 | 28 | public void print() { 29 | throw new UnsupportedOperationException(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/HollywoodSerivice.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 13-9-16 10 | * Time: PM11:32 11 | */ 12 | public class HollywoodSerivice { 13 | 14 | public static List getFriendAgents(){ 15 | AgentFinder finder = new SpreadsheetAgentFinder(); 16 | List agents = finder.findAllAgents(); 17 | List filterAgents = filterAgents(agents,"java developer"); 18 | return filterAgents; 19 | } 20 | 21 | public static List filterAgents(List agents,String agentType){ 22 | List filterAgents = Lists.newArrayList(); 23 | for (Agent agent:agents){ 24 | if(agent.getType().equals(agentType)){ 25 | filterAgents.add(agent); 26 | } 27 | } 28 | return filterAgents; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/SoldOutState.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | public class SoldOutState implements State { 4 | GumballMachine gumballMachine; 5 | 6 | public SoldOutState(GumballMachine gumballMachine) { 7 | this.gumballMachine = gumballMachine; 8 | } 9 | 10 | public void insertQuarter() { 11 | System.out.println("You can't insert a quarter, the machine is sold out"); 12 | } 13 | 14 | public void ejectQuarter() { 15 | System.out.println("You can't eject, you haven't inserted a quarter yet"); 16 | } 17 | 18 | public void turnCrank() { 19 | System.out.println("You turned, but there are no gumballs"); 20 | } 21 | 22 | public void dispense() { 23 | System.out.println("No gumball dispensed"); 24 | } 25 | 26 | public void refill() { 27 | gumballMachine.setState(gumballMachine.getNoQuarterState()); 28 | } 29 | 30 | public String toString() { 31 | return "sold out"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/HollywoodServiceWithFactory.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 13-9-16 10 | * Time: PM11:36 11 | */ 12 | public class HollywoodServiceWithFactory { 13 | public List getFriendlyAgents(String agentType){ 14 | AgentFinder agentFinder =AgentFinderFactory.getInstance().agentFinder(agentType); 15 | List agents = filterAgents(agentFinder.findAllAgents(),"java developer"); 16 | return agents; 17 | } 18 | 19 | public static List filterAgents(List agents,String agentType){ 20 | List filterAgents = Lists.newArrayList(); 21 | for (Agent agent:agents){ 22 | if(agent.getType().equals(agentType)){ 23 | filterAgents.add(agent); 24 | } 25 | } 26 | return filterAgents ; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/Stereo.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class Stereo { 4 | String location; 5 | 6 | public Stereo(String location) { 7 | this.location = location; 8 | } 9 | 10 | public void on() { 11 | System.out.println(location + " stereo is on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(location + " stereo is off"); 16 | } 17 | 18 | public void setCD() { 19 | System.out.println(location + " stereo is set for CD input"); 20 | } 21 | 22 | public void setDVD() { 23 | System.out.println(location + " stereo is set for DVD input"); 24 | } 25 | 26 | public void setRadio() { 27 | System.out.println(location + " stereo is set for Radio"); 28 | } 29 | 30 | public void setVolume(int volume) { 31 | // code to set the volume 32 | // valid range: 1-11 (after all 11 is better than 10, right?) 33 | System.out.println(location + " Stereo volume set to " + volume); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/Stereo.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class Stereo { 4 | String location; 5 | 6 | public Stereo(String location) { 7 | this.location = location; 8 | } 9 | 10 | public void on() { 11 | System.out.println(location + " stereo is on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(location + " stereo is off"); 16 | } 17 | 18 | public void setCD() { 19 | System.out.println(location + " stereo is set for CD input"); 20 | } 21 | 22 | public void setDVD() { 23 | System.out.println(location + " stereo is set for DVD input"); 24 | } 25 | 26 | public void setRadio() { 27 | System.out.println(location + " stereo is set for Radio"); 28 | } 29 | 30 | public void setVolume(int volume) { 31 | // code to set the volume 32 | // valid range: 1-11 (after all 11 is better than 10, right?) 33 | System.out.println(location + " Stereo volume set to " + volume); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/StrategyPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.strategy.demo.MallardDuck; 4 | import com.strategy.demo.RedheadDuck; 5 | import org.junit.Test; 6 | 7 | import static org.junit.Assert.assertEquals; 8 | 9 | /** 10 | * User: Shawn cao 11 | * Date: 15/1/26 12 | * Time: AM11:45 13 | */ 14 | public class StrategyPatternTest { 15 | 16 | @Test 17 | public void testDuckFly(){ 18 | MallardDuck mallard = new MallardDuck(); 19 | assertEquals("mallard duck can fly ", mallard.performFly(),"I'm flying"); 20 | assertEquals("mallard duck cannot quack ", mallard.performQuack(),"silence"); 21 | } 22 | 23 | @Test 24 | public void testDuckQuack(){ 25 | RedheadDuck duck = new RedheadDuck(); 26 | assertEquals("redhead duck can quack", duck.performQuack(),"Quack"); 27 | assertEquals("redhead duck cannot flying", duck.performFly(),"I can't fly"); 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/CeilingFan.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class CeilingFan { 4 | public static final int HIGH = 3; 5 | public static final int MEDIUM = 2; 6 | public static final int LOW = 1; 7 | public static final int OFF = 0; 8 | String location; 9 | int speed; 10 | 11 | public CeilingFan(String location) { 12 | this.location = location; 13 | speed = OFF; 14 | } 15 | 16 | public void high() { 17 | speed = HIGH; 18 | System.out.println(location + " ceiling fan is on high"); 19 | } 20 | 21 | public void medium() { 22 | speed = MEDIUM; 23 | System.out.println(location + " ceiling fan is on medium"); 24 | } 25 | 26 | public void low() { 27 | speed = LOW; 28 | System.out.println(location + " ceiling fan is on low"); 29 | } 30 | 31 | public void off() { 32 | speed = OFF; 33 | System.out.println(location + " ceiling fan is off"); 34 | } 35 | 36 | public int getSpeed() { 37 | return speed; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/Hottub.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class Hottub { 4 | boolean on; 5 | int temperature; 6 | 7 | public Hottub() { 8 | } 9 | 10 | public void on() { 11 | on = true; 12 | } 13 | 14 | public void off() { 15 | on = false; 16 | } 17 | 18 | public void circulate() { 19 | if (on) { 20 | System.out.println("Hottub is bubbling!"); 21 | } 22 | } 23 | 24 | public void jetsOn() { 25 | if (on) { 26 | System.out.println("Hottub jets are on"); 27 | } 28 | } 29 | 30 | public void jetsOff() { 31 | if (on) { 32 | System.out.println("Hottub jets are off"); 33 | } 34 | } 35 | 36 | public void setTemperature(int temperature) { 37 | if (temperature > this.temperature) { 38 | System.out.println("Hottub is heating to a steaming " + temperature + " degrees"); 39 | } 40 | else { 41 | System.out.println("Hottub is cooling to " + temperature + " degrees"); 42 | } 43 | this.temperature = temperature; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/AgentFinderFactory.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 13-9-16 6 | * Time: PM11:38 7 | */ 8 | public class AgentFinderFactory { 9 | private static AgentFinderFactory agentFinderFactory; 10 | 11 | public static AgentFinderFactory getInstance(){ 12 | if(agentFinderFactory == null){ 13 | agentFinderFactory = new AgentFinderFactory(); 14 | } 15 | return agentFinderFactory; 16 | } 17 | 18 | public AgentFinder agentFinder(String agentType){ 19 | AgentFinder agentFinder = null; 20 | switch (agentType){ 21 | case "webservice": 22 | agentFinder = new WebServiceAgentFinder(); 23 | break; 24 | case "spreadsheet": 25 | agentFinder = new SpreadsheetAgentFinder(); 26 | break; 27 | default: 28 | agentFinder = new WebServiceAgentFinder(); 29 | break; 30 | } 31 | return agentFinder; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/simple/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.simple; 2 | 3 | /** 4 | * User: Shawn cao 5 | * Date: 15/2/2 6 | * Time: PM2:14 7 | */ 8 | public class RemoteControl { 9 | 10 | Command[] onCommands; 11 | Command[] offCommands; 12 | 13 | public RemoteControl() { 14 | 15 | onCommands = new Command[2]; 16 | offCommands = new Command[2]; 17 | 18 | Command noCommand = new NoCommand(); 19 | for (int i = 0; i < 7; i++) { 20 | onCommands[i] = noCommand; 21 | offCommands[i] = noCommand; 22 | } 23 | } 24 | 25 | public void setCommand(int slot, Command onCommand, Command offCommand){ 26 | onCommands[slot] = onCommand; 27 | offCommands[slot] = offCommand; 28 | } 29 | 30 | public void onButtonWasPushed(int slot){ 31 | onCommands[slot].execute(); 32 | } 33 | 34 | public void offButtonWasPushed(int slot){ 35 | offCommands[slot].execute(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV2/ForeConditionDisplay.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV2; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | public class ForeConditionDisplay implements Observer,DisplayElement{ 7 | 8 | Observable observable; 9 | private float temp; 10 | private float humidity; 11 | private float pressure; 12 | 13 | public ForeConditionDisplay(Observable observable) { 14 | this.observable = observable; 15 | observable.addObserver(this); 16 | } 17 | @Override 18 | public void update(Observable o, Object arg) { 19 | if(o instanceof WeatherData){ 20 | this.temp = ((WeatherData) o).getTemp(); 21 | this.humidity=((WeatherData) o).getHumidity(); 22 | this.pressure = ((WeatherData) o).getPressure(); 23 | } 24 | } 25 | 26 | @Override 27 | public void display() { 28 | System.out.println(" forcast the weather: " + "temp-->" + temp + " humidity--> " + humidity 29 | + " pressure-->" + pressure); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/ChicagoPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class ChicagoPizzaStore extends PizzaStore { 4 | 5 | protected Pizza createPizza(String item) { 6 | Pizza pizza = null; 7 | PizzaIngredientFactory ingredientFactory = 8 | new ChicagoPizzaIngredientFactory(); 9 | 10 | if (item.equals("cheese")) { 11 | 12 | pizza = new CheesePizza(ingredientFactory); 13 | pizza.setName("Chicago Style Cheese Pizza"); 14 | 15 | } else if (item.equals("veggie")) { 16 | 17 | pizza = new VeggiePizza(ingredientFactory); 18 | pizza.setName("Chicago Style Veggie Pizza"); 19 | 20 | } else if (item.equals("clam")) { 21 | 22 | pizza = new ClamPizza(ingredientFactory); 23 | pizza.setName("Chicago Style Clam Pizza"); 24 | 25 | } else if (item.equals("pepperoni")) { 26 | 27 | pizza = new PepperoniPizza(ingredientFactory); 28 | pizza.setName("Chicago Style Pepperoni Pizza"); 29 | 30 | } 31 | return pizza; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/NYPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public class NYPizzaStore extends PizzaStore { 4 | 5 | protected Pizza createPizza(String item) { 6 | Pizza pizza = null; 7 | PizzaIngredientFactory ingredientFactory = 8 | new NYPizzaIngredientFactory(); 9 | 10 | if (item.equals("cheese")) { 11 | 12 | pizza = new CheesePizza(ingredientFactory); 13 | pizza.setName("New York Style Cheese Pizza"); 14 | 15 | } else if (item.equals("veggie")) { 16 | 17 | pizza = new VeggiePizza(ingredientFactory); 18 | pizza.setName("New York Style Veggie Pizza"); 19 | 20 | } else if (item.equals("clam")) { 21 | 22 | pizza = new ClamPizza(ingredientFactory); 23 | pizza.setName("New York Style Clam Pizza"); 24 | 25 | } else if (item.equals("pepperoni")) { 26 | 27 | pizza = new PepperoniPizza(ingredientFactory); 28 | pizza.setName("New York Style Pepperoni Pizza"); 29 | 30 | } 31 | return pizza; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.*; 4 | 5 | public abstract class MenuComponent { 6 | 7 | public void add(MenuComponent menuComponent) { 8 | throw new UnsupportedOperationException(); 9 | } 10 | public void remove(MenuComponent menuComponent) { 11 | throw new UnsupportedOperationException(); 12 | } 13 | public MenuComponent getChild(int i) { 14 | throw new UnsupportedOperationException(); 15 | } 16 | 17 | public String getName() { 18 | throw new UnsupportedOperationException(); 19 | } 20 | public String getDescription() { 21 | throw new UnsupportedOperationException(); 22 | } 23 | public double getPrice() { 24 | throw new UnsupportedOperationException(); 25 | } 26 | public boolean isVegetarian() { 27 | throw new UnsupportedOperationException(); 28 | } 29 | 30 | public abstract Iterator createIterator(); 31 | 32 | public void print() { 33 | throw new UnsupportedOperationException(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/WebServiceAgentFinder.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 13-9-16 10 | * Time: PM11:29 11 | */ 12 | public class WebServiceAgentFinder implements AgentFinder { 13 | private String path; 14 | private String type; 15 | @Override 16 | public List findAllAgents() { 17 | return Lists.newArrayList(); 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "WebServiceAgentFinder{" + 23 | "path='" + path + '\'' + 24 | ", type='" + type + '\'' + 25 | '}'; 26 | } 27 | 28 | public void setPath(String path) { 29 | this.path = path; 30 | } 31 | 32 | public void setType(String type) { 33 | this.type = type; 34 | } 35 | 36 | public String getType() { 37 | 38 | return type; 39 | } 40 | 41 | public String getPath() { 42 | 43 | return path; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/SpreadsheetAgentFinder.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 13-9-16 10 | * Time: PM11:29 11 | */ 12 | public class SpreadsheetAgentFinder implements AgentFinder { 13 | 14 | private String path; 15 | private String type; 16 | 17 | @Override 18 | public String toString() { 19 | return "SpreadsheetAgentFinder{" + 20 | "path='" + path + '\'' + 21 | ", type='" + type + '\'' + 22 | '}'; 23 | } 24 | 25 | public String getPath() { 26 | return path; 27 | } 28 | 29 | public void setPath(String path) { 30 | this.path = path; 31 | } 32 | 33 | public String getType() { 34 | return type; 35 | } 36 | 37 | public void setType(String type) { 38 | this.type = type; 39 | } 40 | 41 | @Override 42 | public List findAllAgents() { 43 | return Lists.newLinkedList(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/di/demo/HollywoodServiceWithJSR330.java: -------------------------------------------------------------------------------- 1 | package com.di.demo; 2 | 3 | import com.google.common.collect.Lists; 4 | import com.google.inject.Inject; 5 | import com.google.inject.Singleton; 6 | //import javax.inject.Inject; 7 | import java.util.List; 8 | 9 | /** 10 | * User: Shawn cao 11 | * Date: 13-9-17 12 | * Time: AM12:03 13 | */ 14 | public class HollywoodServiceWithJSR330 { 15 | 16 | 17 | @Inject 18 | @Singleton 19 | public static List getFriendlyAgents(AgentFinder finder){ 20 | List agents = finder.findAllAgents(); 21 | return filterAgents(agents,"java developer"); 22 | } 23 | 24 | public static List filterAgents(List agents,String agentType){ 25 | List filterAgents = Lists.newArrayList(); 26 | for (Agent agent:agents){ 27 | if(agent.getType().equals(agentType)){ 28 | filterAgents.add(agent); 29 | } 30 | } 31 | return filterAgents ; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menu/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menu; 2 | 3 | public class MenuItem extends MenuComponent { 4 | String name; 5 | String description; 6 | boolean vegetarian; 7 | double price; 8 | 9 | public MenuItem(String name, 10 | String description, 11 | boolean vegetarian, 12 | double price) 13 | { 14 | this.name = name; 15 | this.description = description; 16 | this.vegetarian = vegetarian; 17 | this.price = price; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public String getDescription() { 25 | return description; 26 | } 27 | 28 | public double getPrice() { 29 | return price; 30 | } 31 | 32 | public boolean isVegetarian() { 33 | return vegetarian; 34 | } 35 | 36 | public void print() { 37 | System.out.print(" " + getName()); 38 | if (isVegetarian()) { 39 | System.out.print("(v)"); 40 | } 41 | System.out.println(", " + getPrice()); 42 | System.out.println(" -- " + getDescription()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/CeilingFan.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class CeilingFan { 4 | String location = ""; 5 | int level; 6 | public static final int HIGH = 2; 7 | public static final int MEDIUM = 1; 8 | public static final int LOW = 0; 9 | 10 | public CeilingFan(String location) { 11 | this.location = location; 12 | } 13 | 14 | public void high() { 15 | // turns the ceiling fan on to high 16 | level = HIGH; 17 | System.out.println(location + " ceiling fan is on high"); 18 | 19 | } 20 | 21 | public void medium() { 22 | // turns the ceiling fan on to medium 23 | level = MEDIUM; 24 | System.out.println(location + " ceiling fan is on medium"); 25 | } 26 | 27 | public void low() { 28 | // turns the ceiling fan on to low 29 | level = LOW; 30 | System.out.println(location + " ceiling fan is on low"); 31 | } 32 | 33 | public void off() { 34 | // turns the ceiling fan off 35 | level = 0; 36 | System.out.println(location + " ceiling fan is off"); 37 | } 38 | 39 | public int getSpeed() { 40 | return level; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/SoldState.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | public class SoldState implements State { 4 | GumballMachine gumballMachine; 5 | 6 | public SoldState(GumballMachine gumballMachine) { 7 | this.gumballMachine = gumballMachine; 8 | } 9 | 10 | public void insertQuarter() { 11 | System.out.println("Please wait, we're already giving you a gumball"); 12 | } 13 | 14 | public void ejectQuarter() { 15 | System.out.println("Sorry, you already turned the crank"); 16 | } 17 | 18 | public void turnCrank() { 19 | System.out.println("Turning twice doesn't get you another gumball!"); 20 | } 21 | 22 | public void dispense() { 23 | gumballMachine.releaseBall(); 24 | if (gumballMachine.getCount() > 0) { 25 | gumballMachine.setState(gumballMachine.getNoQuarterState()); 26 | } else { 27 | System.out.println("Oops, out of gumballs!"); 28 | gumballMachine.setState(gumballMachine.getSoldOutState()); 29 | } 30 | } 31 | 32 | public void refill() { } 33 | 34 | public String toString() { 35 | return "dispensing a gumball"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV2/WeatherData.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV2; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | public class WeatherData extends Observable { 7 | private float temp; 8 | private float humidity; 9 | private float pressure; 10 | 11 | public void measurementChanged(){ 12 | setChanged(); 13 | notifyObservers(); 14 | } 15 | 16 | public void setMeasurement(float temp, float humidity, float pressure){ 17 | this.temp = temp; 18 | this.humidity = humidity; 19 | this.pressure = pressure; 20 | measurementChanged(); 21 | } 22 | 23 | public float getTemp() { 24 | return temp; 25 | } 26 | 27 | public void setTemp(float temp) { 28 | this.temp = temp; 29 | } 30 | 31 | public float getHumidity() { 32 | return humidity; 33 | } 34 | 35 | public void setHumidity(float humidity) { 36 | this.humidity = humidity; 37 | } 38 | 39 | public float getPressure() { 40 | return pressure; 41 | } 42 | 43 | public void setPressure(float pressure) { 44 | this.pressure = pressure; 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/CeilingFan.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class CeilingFan { 4 | public static final int HIGH = 3; 5 | public static final int MEDIUM = 2; 6 | public static final int LOW = 1; 7 | public static final int OFF = 0; 8 | String location; 9 | int speed; 10 | 11 | public CeilingFan(String location) { 12 | this.location = location; 13 | } 14 | 15 | public void high() { 16 | // turns the ceiling fan on to high 17 | speed = HIGH; 18 | System.out.println(location + " ceiling fan is on high"); 19 | } 20 | 21 | public void medium() { 22 | // turns the ceiling fan on to medium 23 | speed = MEDIUM; 24 | System.out.println(location + " ceiling fan is on medium"); 25 | } 26 | 27 | public void low() { 28 | // turns the ceiling fan on to low 29 | speed = LOW; 30 | System.out.println(location + " ceiling fan is on low"); 31 | } 32 | 33 | public void off() { 34 | // turns the ceiling fan off 35 | speed = OFF; 36 | System.out.println(location + " ceiling fan is off"); 37 | } 38 | 39 | public int getSpeed() { 40 | return speed; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/Hottub.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class Hottub { 4 | boolean on; 5 | int temperature; 6 | 7 | public Hottub() { 8 | } 9 | 10 | public void on() { 11 | on = true; 12 | } 13 | 14 | public void off() { 15 | on = false; 16 | } 17 | 18 | public void bubblesOn() { 19 | if (on) { 20 | System.out.println("Hottub is bubbling!"); 21 | } 22 | } 23 | 24 | public void bubblesOff() { 25 | if (on) { 26 | System.out.println("Hottub is not bubbling"); 27 | } 28 | } 29 | 30 | public void jetsOn() { 31 | if (on) { 32 | System.out.println("Hottub jets are on"); 33 | } 34 | } 35 | 36 | public void jetsOff() { 37 | if (on) { 38 | System.out.println("Hottub jets are off"); 39 | } 40 | } 41 | 42 | public void setTemperature(int temperature) { 43 | this.temperature = temperature; 44 | } 45 | 46 | public void heat() { 47 | temperature = 105; 48 | System.out.println("Hottub is heating to a steaming 105 degrees"); 49 | } 50 | 51 | public void cool() { 52 | temperature = 98; 53 | System.out.println("Hottub is cooling to 98 degrees"); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastore/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastore; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 15/1/30 10 | * Time: PM2:57 11 | */ 12 | public abstract class Pizza { 13 | String name; 14 | String dough; 15 | String sause; 16 | List toppings = Lists.newArrayList(); 17 | 18 | public void prepare() { 19 | System.out.println("Prepare: " + name); 20 | System.out.println("Tossing dough.. "); 21 | System.out.println("Adding sauce.. "); 22 | System.out.println("Adding toppings: "); 23 | for (int i = 0; i < toppings.size(); i++) { 24 | System.out.println(" " + toppings.get(i)); 25 | } 26 | } 27 | 28 | public void bake() { 29 | System.out.println("Bake for 25 minutes at 350"); 30 | } 31 | 32 | public void cut(){ 33 | System.out.println("Cutting the pizza into diagonal slices"); 34 | } 35 | public void box() { 36 | System.out.println("Place pizza in official PizzaStore box"); 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/IteratorPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.iterator.demo.*; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author Shawn Cao 8 | */ 9 | public class IteratorPatternTest { 10 | 11 | @Test 12 | public void testDinnerAndPanHousePrintWithIterator(){ 13 | 14 | Waitress waitress = new Waitress(new DinnerMenu(),new PancakeHouseMenu()); 15 | waitress.printAll(); 16 | } 17 | 18 | private static class Waitress{ 19 | Menu dinerMenu; 20 | Menu pancakeMenu; 21 | 22 | public Waitress(Menu dinerMenu, Menu pancakeMenu) { 23 | this.dinerMenu = dinerMenu; 24 | this.pancakeMenu = pancakeMenu; 25 | } 26 | 27 | public void printAll(){ 28 | printMenu(pancakeMenu.createIterator()); 29 | System.out.println("----------------"); 30 | printMenu(dinerMenu.createIterator()); 31 | } 32 | 33 | private void printMenu(Iterator iterator){ 34 | while (iterator.hasNext()){ 35 | MenuItem menuItem = iterator.next(); 36 | System.out.println(menuItem.toString()); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/HasQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | import java.util.Random; 4 | 5 | public class HasQuarterState implements State { 6 | Random randomWinner = new Random(System.currentTimeMillis()); 7 | GumballMachine gumballMachine; 8 | 9 | public HasQuarterState(GumballMachine gumballMachine) { 10 | this.gumballMachine = gumballMachine; 11 | } 12 | 13 | public void insertQuarter() { 14 | System.out.println("You can't insert another quarter"); 15 | } 16 | 17 | public void ejectQuarter() { 18 | System.out.println("Quarter returned"); 19 | gumballMachine.setState(gumballMachine.getNoQuarterState()); 20 | } 21 | 22 | public void turnCrank() { 23 | System.out.println("You turned..."); 24 | int winner = randomWinner.nextInt(10); 25 | if ((winner == 0) && (gumballMachine.getCount() > 1)) { 26 | gumballMachine.setState(gumballMachine.getWinnerState()); 27 | } else { 28 | gumballMachine.setState(gumballMachine.getSoldState()); 29 | } 30 | } 31 | 32 | public void dispense() { 33 | System.out.println("No gumball dispensed"); 34 | } 35 | 36 | public void refill() { } 37 | 38 | public String toString() { 39 | return "waiting for turn of crank"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/CompositeIterator.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.*; 4 | 5 | public class CompositeIterator implements Iterator { 6 | Stack> stack = new Stack>(); 7 | 8 | public CompositeIterator(Iterator iterator) { 9 | stack.push(iterator); 10 | } 11 | 12 | public MenuComponent next() { 13 | if (hasNext()) { 14 | Iterator iterator = stack.peek(); 15 | MenuComponent component = iterator.next(); 16 | stack.push(component.createIterator()); 17 | return component; 18 | } else { 19 | return null; 20 | } 21 | } 22 | 23 | public boolean hasNext() { 24 | if (stack.empty()) { 25 | return false; 26 | } else { 27 | Iterator iterator = stack.peek(); 28 | if (!iterator.hasNext()) { 29 | stack.pop(); 30 | return hasNext(); 31 | } else { 32 | return true; 33 | } 34 | } 35 | } 36 | 37 | /* 38 | * No longer needed as of Java 8 39 | * 40 | * (non-Javadoc) 41 | * @see java.util.Iterator#remove() 42 | * 43 | public void remove() { 44 | throw new UnsupportedOperationException(); 45 | } 46 | */ 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.Iterator; 4 | 5 | public class MenuItem extends MenuComponent { 6 | 7 | String name; 8 | String description; 9 | boolean vegetarian; 10 | double price; 11 | 12 | public MenuItem(String name, 13 | String description, 14 | boolean vegetarian, 15 | double price) 16 | { 17 | this.name = name; 18 | this.description = description; 19 | this.vegetarian = vegetarian; 20 | this.price = price; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public String getDescription() { 28 | return description; 29 | } 30 | 31 | public double getPrice() { 32 | return price; 33 | } 34 | 35 | public boolean isVegetarian() { 36 | return vegetarian; 37 | } 38 | 39 | public Iterator createIterator() { 40 | return new NullIterator(); 41 | } 42 | 43 | public void print() { 44 | System.out.print(" " + getName()); 45 | if (isVegetarian()) { 46 | System.out.print("(v)"); 47 | } 48 | System.out.println(", " + getPrice()); 49 | System.out.println(" -- " + getDescription()); 50 | } 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | // 4 | // This is the invoker 5 | // 6 | public class RemoteControl { 7 | Command[] onCommands; 8 | Command[] offCommands; 9 | 10 | public RemoteControl() { 11 | onCommands = new Command[7]; 12 | offCommands = new Command[7]; 13 | 14 | Command noCommand = new NoCommand(); 15 | for (int i = 0; i < 7; i++) { 16 | onCommands[i] = noCommand; 17 | offCommands[i] = noCommand; 18 | } 19 | } 20 | 21 | public void setCommand(int slot, Command onCommand, Command offCommand) { 22 | onCommands[slot] = onCommand; 23 | offCommands[slot] = offCommand; 24 | } 25 | 26 | public void onButtonWasPushed(int slot) { 27 | onCommands[slot].execute(); 28 | } 29 | 30 | public void offButtonWasPushed(int slot) { 31 | offCommands[slot].execute(); 32 | } 33 | 34 | public String toString() { 35 | StringBuffer stringBuff = new StringBuffer(); 36 | stringBuff.append("\n------ Remote Control -------\n"); 37 | for (int i = 0; i < onCommands.length; i++) { 38 | stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() 39 | + " " + offCommands[i].getClass().getName() + "\n"); 40 | } 41 | return stringBuff.toString(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/decorator/demo/io/LowerCaseInputStream.java: -------------------------------------------------------------------------------- 1 | package com.decorator.demo.io; 2 | 3 | import java.io.FilterInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 15/1/30 10 | * Time: AM11:15 11 | */ 12 | public class LowerCaseInputStream extends FilterInputStream { 13 | /** 14 | * Creates a FilterInputStream 15 | * by assigning the argument in 16 | * to the field this.in so as 17 | * to remember it for later use. 18 | * 19 | * @param in the underlying input stream, or null if 20 | * this instance is to be created without an underlying stream. 21 | */ 22 | public LowerCaseInputStream(InputStream in) { 23 | super(in); 24 | } 25 | 26 | @Override 27 | public int read() throws IOException { 28 | int c = super.read(); 29 | return ( c == -1 ? c : Character.toLowerCase((char)c)); 30 | } 31 | 32 | @Override 33 | public int read(byte[] b, int off, int len) throws IOException { 34 | int result = super.read(b,off,len); 35 | for (int i = off; i < off + result ; i++) { 36 | b[i] = (byte)Character.toLowerCase((char)b[i]); 37 | } 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menu/Menu.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menu; 2 | 3 | import java.util.Iterator; 4 | import java.util.ArrayList; 5 | 6 | public class Menu extends MenuComponent { 7 | ArrayList menuComponents = new ArrayList(); 8 | String name; 9 | String description; 10 | 11 | public Menu(String name, String description) { 12 | this.name = name; 13 | this.description = description; 14 | } 15 | 16 | public void add(MenuComponent menuComponent) { 17 | menuComponents.add(menuComponent); 18 | } 19 | 20 | public void remove(MenuComponent menuComponent) { 21 | menuComponents.remove(menuComponent); 22 | } 23 | 24 | public MenuComponent getChild(int i) { 25 | return (MenuComponent)menuComponents.get(i); 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public String getDescription() { 33 | return description; 34 | } 35 | 36 | public void print() { 37 | System.out.print("\n" + getName()); 38 | System.out.println(", " + getDescription()); 39 | System.out.println("---------------------"); 40 | 41 | Iterator iterator = menuComponents.iterator(); 42 | while (iterator.hasNext()) { 43 | MenuComponent menuComponent = 44 | (MenuComponent)iterator.next(); 45 | menuComponent.print(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/template/demo/CoffeWithHook.java: -------------------------------------------------------------------------------- 1 | package com.template.demo; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStreamReader; 6 | 7 | public class CoffeWithHook extends CaffineBeverageWithHook{ 8 | 9 | @Override 10 | public String brew() { 11 | String brew = " Dripping coffee thru filter"; 12 | System.out.println(brew); 13 | return brew; 14 | } 15 | 16 | @Override 17 | public String addCondiments() { 18 | String condiments = " Adding sugar and milk"; 19 | System.out.println(condiments); 20 | return condiments; 21 | } 22 | 23 | public boolean customerWantsCondiments(){ 24 | String answer = getUserInput(); 25 | if(answer.toLowerCase().equals("yy")) 26 | return true; 27 | return false; 28 | } 29 | 30 | private String getUserInput(){ 31 | String answer = null; 32 | 33 | System.out.println("Would u like milk and sugar in ur coffee(Y/N) /n "); 34 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 35 | try{ 36 | answer = br.readLine(); 37 | }catch(IOException e){ 38 | System.out.println(" err reading"); 39 | } 40 | if(answer==null){ 41 | return "no"; 42 | } 43 | return answer; 44 | } 45 | 46 | public static void main(String[] args) { 47 | CaffineBeverageWithHook coffee = new CoffeWithHook(); 48 | coffee.prepareRecipe(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | import com.google.common.base.Objects; 4 | import com.google.common.base.Objects.ToStringHelper; 5 | 6 | public class MenuItem { 7 | String name; 8 | String description; 9 | boolean vegetarian; 10 | double price; 11 | 12 | public MenuItem(String name, String description, boolean vegetarian, 13 | double price) { 14 | this.name = name; 15 | this.description = description; 16 | this.vegetarian = vegetarian; 17 | this.price = price; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getDescription() { 29 | return description; 30 | } 31 | 32 | public void setDescription(String description) { 33 | this.description = description; 34 | } 35 | 36 | public boolean isVegetarian() { 37 | return vegetarian; 38 | } 39 | 40 | public void setVegetarian(boolean vegetarian) { 41 | this.vegetarian = vegetarian; 42 | } 43 | 44 | public double getPrice() { 45 | return price; 46 | } 47 | 48 | public void setPrice(double price) { 49 | this.price = price; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return Objects.toStringHelper(this).add("name", name) 55 | .add("description ", description).add("vegetarian", vegetarian) 56 | .add("price", price).toString(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/WinnerState.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | public class WinnerState implements State { 4 | GumballMachine gumballMachine; 5 | 6 | public WinnerState(GumballMachine gumballMachine) { 7 | this.gumballMachine = gumballMachine; 8 | } 9 | 10 | public void insertQuarter() { 11 | System.out.println("Please wait, we're already giving you a Gumball"); 12 | } 13 | 14 | public void ejectQuarter() { 15 | System.out.println("Please wait, we're already giving you a Gumball"); 16 | } 17 | 18 | public void turnCrank() { 19 | System.out.println("Turning again doesn't get you another gumball!"); 20 | } 21 | 22 | public void dispense() { 23 | gumballMachine.releaseBall(); 24 | if (gumballMachine.getCount() == 0) { 25 | gumballMachine.setState(gumballMachine.getSoldOutState()); 26 | } else { 27 | gumballMachine.releaseBall(); 28 | System.out.println("YOU'RE A WINNER! You got two gumballs for your quarter"); 29 | if (gumballMachine.getCount() > 0) { 30 | gumballMachine.setState(gumballMachine.getNoQuarterState()); 31 | } else { 32 | System.out.println("Oops, out of gumballs!"); 33 | gumballMachine.setState(gumballMachine.getSoldOutState()); 34 | } 35 | } 36 | } 37 | 38 | public void refill() { } 39 | 40 | public String toString() { 41 | return "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/RemoteLoader.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | public class RemoteLoader { 4 | 5 | public static void main(String[] args) { 6 | 7 | RemoteControl remoteControl = new RemoteControl(); 8 | 9 | Light light = new Light("Living Room"); 10 | TV tv = new TV("Living Room"); 11 | Stereo stereo = new Stereo("Living Room"); 12 | Hottub hottub = new Hottub(); 13 | 14 | LightOnCommand lightOn = new LightOnCommand(light); 15 | StereoOnCommand stereoOn = new StereoOnCommand(stereo); 16 | TVOnCommand tvOn = new TVOnCommand(tv); 17 | HottubOnCommand hottubOn = new HottubOnCommand(hottub); 18 | LightOffCommand lightOff = new LightOffCommand(light); 19 | StereoOffCommand stereoOff = new StereoOffCommand(stereo); 20 | TVOffCommand tvOff = new TVOffCommand(tv); 21 | HottubOffCommand hottubOff = new HottubOffCommand(hottub); 22 | 23 | Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn}; 24 | Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff}; 25 | 26 | MacroCommand partyOnMacro = new MacroCommand(partyOn); 27 | MacroCommand partyOffMacro = new MacroCommand(partyOff); 28 | 29 | remoteControl.setCommand(0, partyOnMacro, partyOffMacro); 30 | 31 | System.out.println(remoteControl); 32 | System.out.println("--- Pushing Macro On---"); 33 | remoteControl.onButtonWasPushed(0); 34 | System.out.println("--- Pushing Macro Off---"); 35 | remoteControl.offButtonWasPushed(0); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationV1/WeatherData.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationV1; 2 | 3 | import com.google.common.collect.Lists; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * User: Shawn cao 9 | * Date: 15/1/26 10 | * Time: PM5:27 11 | */ 12 | public class WeatherData implements Subject { 13 | 14 | private List observers; 15 | private float temperature; 16 | private float humidity; 17 | private float pressure; 18 | 19 | public WeatherData(){ 20 | observers = Lists.newArrayList(); 21 | } 22 | 23 | @Override 24 | public void registerObserver(Observer o) { 25 | observers.add(o); 26 | } 27 | 28 | @Override 29 | public void removeObserver(Observer o) { 30 | int i = observers.indexOf(o); 31 | if( i> 0) 32 | observers.remove(i); 33 | } 34 | 35 | @Override 36 | public void notifyObservers() { 37 | for (int i = 0; i < observers.size(); i++) { 38 | Observer observer = (Observer)observers.get(i); 39 | observer.update(temperature,humidity,pressure); 40 | } 41 | } 42 | 43 | public void measurementsChanged(){ 44 | notifyObservers(); 45 | } 46 | 47 | public void setMeasurements(float temperature, float humidity, float pressure) { 48 | this.temperature = temperature; 49 | this.humidity = humidity; 50 | this.pressure = pressure; 51 | measurementsChanged(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/observer/demo/weatherStationWithEventBus/WeatherDataPOJO.java: -------------------------------------------------------------------------------- 1 | package com.observer.demo.weatherStationWithEventBus; 2 | 3 | import com.google.common.base.Objects; 4 | 5 | /** 6 | * User: Shawn cao 7 | * Date: 15/1/26 8 | * Time: PM10:30 9 | */ 10 | public class WeatherDataPOJO { 11 | 12 | private float temperature; 13 | private float pressure; 14 | private float humidity; 15 | 16 | public WeatherDataPOJO(float temperature, float humidity, float pressure) { 17 | this.temperature = temperature; 18 | this.humidity = humidity; 19 | this.pressure = pressure; 20 | } 21 | 22 | public float getTemperature() { 23 | return temperature; 24 | } 25 | 26 | public void setTemperature(float temperature) { 27 | this.temperature = temperature; 28 | } 29 | 30 | public float getPressure() { 31 | return pressure; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return Objects.toStringHelper(this) 37 | .add("temperature", temperature) 38 | .add("pressure", pressure) 39 | .add("humidity", humidity) 40 | .toString(); 41 | } 42 | 43 | public void setPressure(float pressure) { 44 | this.pressure = pressure; 45 | } 46 | 47 | public float getHumidity() { 48 | return humidity; 49 | } 50 | 51 | public void setHumidity(float humidity) { 52 | this.humidity = humidity; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/PancakeHouseMenu.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class PancakeHouseMenu implements Menu{ 6 | ArrayList menuItems; 7 | 8 | public PancakeHouseMenu() { 9 | menuItems = new ArrayList(); 10 | addItem("K&B's Pancake Breakfast", 11 | "Pancakes with scrambled eggs, and toast", 12 | true, 13 | 2.99); 14 | 15 | addItem("Regular Pancake Breakfast", 16 | "Pancakes with fried eggs, sausage", 17 | false, 18 | 2.99); 19 | 20 | addItem("Blueberry Pancakes", 21 | "Pancakes made with fresh blueberries", 22 | true, 23 | 3.49); 24 | 25 | addItem("Waffles", 26 | "Waffles, with your choice of blueberries or strawberries", 27 | true, 28 | 3.59); 29 | } 30 | 31 | public void addItem(String name, String description, boolean vegetarian, double price){ 32 | MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 33 | menuItems.add(menuItem); 34 | } 35 | 36 | public ArrayList getMenuItems(){ 37 | return menuItems; 38 | } 39 | 40 | public Iterator createIterator() { 41 | return new PancakeHouseMenuIterator(menuItems); 42 | // To test Alternating menu items, comment out above line, 43 | // and uncomment the line below. 44 | //return new AlternatingDinerMenuIterator(menuItems); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/party/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.party; 2 | 3 | // 4 | // This is the invoker 5 | // 6 | public class RemoteControl { 7 | Command[] onCommands; 8 | Command[] offCommands; 9 | Command undoCommand; 10 | 11 | public RemoteControl() { 12 | onCommands = new Command[7]; 13 | offCommands = new Command[7]; 14 | 15 | Command noCommand = new NoCommand(); 16 | for(int i=0;i<7;i++) { 17 | onCommands[i] = noCommand; 18 | offCommands[i] = noCommand; 19 | } 20 | undoCommand = noCommand; 21 | } 22 | 23 | public void setCommand(int slot, Command onCommand, Command offCommand) { 24 | onCommands[slot] = onCommand; 25 | offCommands[slot] = offCommand; 26 | } 27 | 28 | public void onButtonWasPushed(int slot) { 29 | onCommands[slot].execute(); 30 | undoCommand = onCommands[slot]; 31 | } 32 | 33 | public void offButtonWasPushed(int slot) { 34 | offCommands[slot].execute(); 35 | undoCommand = offCommands[slot]; 36 | } 37 | 38 | public void undoButtonWasPushed() { 39 | undoCommand.undo(); 40 | } 41 | 42 | public String toString() { 43 | StringBuffer stringBuff = new StringBuffer(); 44 | stringBuff.append("\n------ Remote Control -------\n"); 45 | for (int i = 0; i < onCommands.length; i++) { 46 | stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() 47 | + " " + offCommands[i].getClass().getName() + "\n"); 48 | } 49 | stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n"); 50 | return stringBuff.toString(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/RemoteControlWithUndo.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | // 4 | // This is the invoker 5 | // 6 | public class RemoteControlWithUndo { 7 | Command[] onCommands; 8 | Command[] offCommands; 9 | Command undoCommand; 10 | 11 | public RemoteControlWithUndo() { 12 | onCommands = new Command[7]; 13 | offCommands = new Command[7]; 14 | 15 | Command noCommand = new NoCommand(); 16 | for(int i=0;i<7;i++) { 17 | onCommands[i] = noCommand; 18 | offCommands[i] = noCommand; 19 | } 20 | undoCommand = noCommand; 21 | } 22 | 23 | public void setCommand(int slot, Command onCommand, Command offCommand) { 24 | onCommands[slot] = onCommand; 25 | offCommands[slot] = offCommand; 26 | } 27 | 28 | public void onButtonWasPushed(int slot) { 29 | onCommands[slot].execute(); 30 | undoCommand = onCommands[slot]; 31 | } 32 | 33 | public void offButtonWasPushed(int slot) { 34 | offCommands[slot].execute(); 35 | undoCommand = offCommands[slot]; 36 | } 37 | 38 | public void undoButtonWasPushed() { 39 | undoCommand.undo(); 40 | } 41 | 42 | public String toString() { 43 | StringBuffer stringBuff = new StringBuffer(); 44 | stringBuff.append("\n------ Remote Control -------\n"); 45 | for (int i = 0; i < onCommands.length; i++) { 46 | stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() 47 | + " " + offCommands[i].getClass().getName() + "\n"); 48 | } 49 | stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n"); 50 | return stringBuff.toString(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/Menu.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | import java.util.Iterator; 4 | import java.util.ArrayList; 5 | 6 | public class Menu extends MenuComponent { 7 | Iterator iterator = null; 8 | ArrayList menuComponents = new ArrayList(); 9 | String name; 10 | String description; 11 | 12 | public Menu(String name, String description) { 13 | this.name = name; 14 | this.description = description; 15 | } 16 | 17 | public void add(MenuComponent menuComponent) { 18 | menuComponents.add(menuComponent); 19 | } 20 | 21 | public void remove(MenuComponent menuComponent) { 22 | menuComponents.remove(menuComponent); 23 | } 24 | 25 | public MenuComponent getChild(int i) { 26 | return menuComponents.get(i); 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public String getDescription() { 34 | return description; 35 | } 36 | 37 | 38 | public Iterator createIterator() { 39 | if (iterator == null) { 40 | iterator = new CompositeIterator(menuComponents.iterator()); 41 | } 42 | return iterator; 43 | } 44 | 45 | 46 | public void print() { 47 | System.out.print("\n" + getName()); 48 | System.out.println(", " + getDescription()); 49 | System.out.println("---------------------"); 50 | 51 | Iterator iterator = menuComponents.iterator(); 52 | while (iterator.hasNext()) { 53 | MenuComponent menuComponent = iterator.next(); 54 | menuComponent.print(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/singleton/demo/ChocolateBoiler.java: -------------------------------------------------------------------------------- 1 | package com.singleton.demo; 2 | 3 | public class ChocolateBoiler { 4 | private boolean empty; 5 | private boolean boiled; 6 | //private static ChocolateBoiler uniqueChocolateBoiler; 7 | 8 | //the volatile ensures that multiple threads handle the unique instance correctly 9 | //when it is being initialized to the singleton instance 10 | private static volatile ChocolateBoiler uniqueChocolateBoiler; 11 | 12 | 13 | private ChocolateBoiler() { 14 | empty = false; 15 | boiled = false; 16 | } 17 | 18 | /*public static ChocolateBoiler getInstance(){ 19 | if(uniqueChocolateBoiler == null){ 20 | uniqueChocolateBoiler = new ChocolateBoiler(); 21 | } 22 | return uniqueChocolateBoiler; 23 | }*/ 24 | 25 | 26 | //double-checked locking 27 | public static ChocolateBoiler getInstance(){ 28 | if(uniqueChocolateBoiler == null){ 29 | synchronized (ChocolateBoiler.class) { 30 | if(uniqueChocolateBoiler == null){ 31 | uniqueChocolateBoiler = new ChocolateBoiler(); 32 | } 33 | } 34 | } 35 | return uniqueChocolateBoiler; 36 | } 37 | 38 | 39 | public void fill(){ 40 | if(isEmpty()){ 41 | empty = false; 42 | boiled = false; 43 | } 44 | } 45 | 46 | public void drained(){ 47 | if(!isEmpty() && isBoiled()){ 48 | empty = true; 49 | } 50 | } 51 | 52 | public void boid(){ 53 | if(!isEmpty() && !isBoiled()){ 54 | boiled = true; 55 | } 56 | } 57 | 58 | public boolean isEmpty(){ 59 | return empty; 60 | } 61 | public boolean isBoiled(){ 62 | return boiled; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/StatePatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.state.demo.GumballMachine; 4 | import org.junit.Test; 5 | 6 | /** 7 | * @author Shawn Cao 8 | */ 9 | public class StatePatternTest { 10 | 11 | @Test 12 | public void testGumballMachineState(){ 13 | GumballMachine gumballMachine = 14 | new GumballMachine(10); 15 | 16 | System.out.println(gumballMachine); 17 | 18 | gumballMachine.insertQuarter(); 19 | gumballMachine.turnCrank(); 20 | gumballMachine.insertQuarter(); 21 | gumballMachine.turnCrank(); 22 | 23 | System.out.println(gumballMachine); 24 | 25 | gumballMachine.insertQuarter(); 26 | gumballMachine.turnCrank(); 27 | gumballMachine.insertQuarter(); 28 | gumballMachine.turnCrank(); 29 | 30 | System.out.println(gumballMachine); 31 | 32 | gumballMachine.insertQuarter(); 33 | gumballMachine.turnCrank(); 34 | gumballMachine.insertQuarter(); 35 | gumballMachine.turnCrank(); 36 | 37 | System.out.println(gumballMachine); 38 | 39 | gumballMachine.insertQuarter(); 40 | gumballMachine.turnCrank(); 41 | gumballMachine.insertQuarter(); 42 | gumballMachine.turnCrank(); 43 | 44 | System.out.println(gumballMachine); 45 | 46 | gumballMachine.insertQuarter(); 47 | gumballMachine.turnCrank(); 48 | gumballMachine.insertQuarter(); 49 | gumballMachine.turnCrank(); 50 | 51 | System.out.println(gumballMachine); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/factory/demo/pizzastoreWithIngredient/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.factory.demo.pizzastoreWithIngredient; 2 | 3 | public abstract class Pizza { 4 | String name; 5 | 6 | Dough dough; 7 | Sauce sauce; 8 | Veggies veggies[]; 9 | Cheese cheese; 10 | Pepperoni pepperoni; 11 | Clams clam; 12 | 13 | abstract void prepare(); 14 | 15 | void bake() { 16 | System.out.println("Bake for 25 minutes at 350"); 17 | } 18 | 19 | void cut() { 20 | System.out.println("Cutting the pizza into diagonal slices"); 21 | } 22 | 23 | void box() { 24 | System.out.println("Place pizza in official PizzaStore box"); 25 | } 26 | 27 | void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | String getName() { 32 | return name; 33 | } 34 | 35 | public String toString() { 36 | StringBuffer result = new StringBuffer(); 37 | result.append("---- " + name + " ----\n"); 38 | if (dough != null) { 39 | result.append(dough); 40 | result.append("\n"); 41 | } 42 | if (sauce != null) { 43 | result.append(sauce); 44 | result.append("\n"); 45 | } 46 | if (cheese != null) { 47 | result.append(cheese); 48 | result.append("\n"); 49 | } 50 | if (veggies != null) { 51 | for (int i = 0; i < veggies.length; i++) { 52 | result.append(veggies[i]); 53 | if (i < veggies.length-1) { 54 | result.append(", "); 55 | } 56 | } 57 | result.append("\n"); 58 | } 59 | if (clam != null) { 60 | result.append(clam); 61 | result.append("\n"); 62 | } 63 | if (pepperoni != null) { 64 | result.append(pepperoni); 65 | result.append("\n"); 66 | } 67 | return result.toString(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/undo/RemoteLoader.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.undo; 2 | 3 | public class RemoteLoader { 4 | 5 | public static void main(String[] args) { 6 | RemoteControlWithUndo remoteControl = new RemoteControlWithUndo(); 7 | 8 | Light livingRoomLight = new Light("Living Room"); 9 | 10 | LightOnCommand livingRoomLightOn = 11 | new LightOnCommand(livingRoomLight); 12 | LightOffCommand livingRoomLightOff = 13 | new LightOffCommand(livingRoomLight); 14 | 15 | remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); 16 | 17 | remoteControl.onButtonWasPushed(0); 18 | remoteControl.offButtonWasPushed(0); 19 | System.out.println(remoteControl); 20 | remoteControl.undoButtonWasPushed(); 21 | remoteControl.offButtonWasPushed(0); 22 | remoteControl.onButtonWasPushed(0); 23 | System.out.println(remoteControl); 24 | remoteControl.undoButtonWasPushed(); 25 | 26 | CeilingFan ceilingFan = new CeilingFan("Living Room"); 27 | 28 | CeilingFanMediumCommand ceilingFanMedium = 29 | new CeilingFanMediumCommand(ceilingFan); 30 | CeilingFanHighCommand ceilingFanHigh = 31 | new CeilingFanHighCommand(ceilingFan); 32 | CeilingFanOffCommand ceilingFanOff = 33 | new CeilingFanOffCommand(ceilingFan); 34 | 35 | remoteControl.setCommand(0, ceilingFanMedium, ceilingFanOff); 36 | remoteControl.setCommand(1, ceilingFanHigh, ceilingFanOff); 37 | 38 | remoteControl.onButtonWasPushed(0); 39 | remoteControl.offButtonWasPushed(0); 40 | System.out.println(remoteControl); 41 | remoteControl.undoButtonWasPushed(); 42 | 43 | remoteControl.onButtonWasPushed(1); 44 | System.out.println(remoteControl); 45 | remoteControl.undoButtonWasPushed(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.shawn 5 | desingPatternDemos 6 | 0.0.1-SNAPSHOT 7 | 8 | UTF-8 9 | 10 | 11 | 12 | com.google.guava 13 | guava 14 | 15.0 15 | 16 | 17 | javax.javaee 18 | javaee 19 | 6.0-alpha-1 20 | 21 | 22 | com.google.inject 23 | guice 24 | 3.0 25 | 26 | 27 | junit 28 | junit 29 | 4.10 30 | test 31 | 32 | 33 | org.mockito 34 | mockito-all 35 | 1.9.5 36 | test 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 3.0 45 | 46 | 1.8 47 | 1.8 48 | 1.8 49 | 1.8 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/DecoratorPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.decorator.demo.buzzCoffee.*; 4 | import com.decorator.demo.io.LowerCaseInputStream; 5 | import org.junit.Test; 6 | 7 | import java.io.*; 8 | 9 | import static junit.framework.Assert.assertEquals; 10 | 11 | /** 12 | * User: Shawn cao 13 | * Date: 15/1/29 14 | * Time: PM5:15 15 | */ 16 | public class DecoratorPatternTest { 17 | 18 | @Test 19 | public void test_coffee_without_condiment(){ 20 | Beverage beverage = new HouseBlend(); 21 | assertEquals(beverage.cost(), .89); 22 | } 23 | 24 | @Test 25 | public void test_coffee_with_condiments(){ 26 | 27 | Beverage beverage = new Mocha(new Milk(new Espresso())); 28 | assertEquals(beverage.cost(), .20 + 0.33 + 1.99); 29 | assertEquals(beverage.getDescription(),"Espresso, milk, mocha tall"); 30 | } 31 | 32 | @Test 33 | public void test_coffee_with_diff_size(){ 34 | Beverage beverage = new Espresso(); 35 | beverage = new Milk(beverage) ; 36 | beverage.setSize(Beverage.Size.grande); 37 | beverage = new Mocha(beverage); 38 | assertEquals(beverage.cost(), .30 + .33 + 1.99,0.01); 39 | assertEquals(beverage.getDescription(),"Espresso, milk, mocha grande"); 40 | } 41 | 42 | 43 | @Test 44 | public void test_lowercase_inputStream(){ 45 | int c; 46 | try { 47 | StringBuilder sb = new StringBuilder(); 48 | InputStream in = new LowerCaseInputStream(new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream("text.txt"))); 49 | while ((c = in.read()) > 0){ 50 | sb.append((char)c); 51 | } 52 | 53 | assertEquals("redheadDuck duck = new redheadDuck();".toLowerCase().trim(),sb.toString()); 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/iterator/demo/DinnerMenu.java: -------------------------------------------------------------------------------- 1 | package com.iterator.demo; 2 | 3 | /** 4 | * @author Shawn Cao 5 | */ 6 | public class DinnerMenu implements Menu{ 7 | static final int MAX_ITEMS = 6; 8 | int numberOfItems = 0; 9 | MenuItem[] menuItems; 10 | 11 | public DinnerMenu() { 12 | menuItems = new MenuItem[MAX_ITEMS]; 13 | 14 | addItem("Vegetarian BLT", 15 | "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99); 16 | addItem("BLT", 17 | "Bacon with lettuce & tomato on whole wheat", false, 2.99); 18 | addItem("Soup of the day", 19 | "Soup of the day, with a side of potato salad", false, 3.29); 20 | addItem("Hotdog", 21 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 22 | false, 3.05); 23 | addItem("Steamed Veggies and Brown Rice", 24 | "Steamed vegetables over brown rice", true, 3.99); 25 | addItem("Pasta", 26 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 27 | true, 3.89); 28 | } 29 | 30 | public void addItem(String name, String description, 31 | boolean vegetarian, double price) 32 | { 33 | MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 34 | if (numberOfItems >= MAX_ITEMS) { 35 | System.err.println("Sorry, menu is full! Can't add item to menu"); 36 | } else { 37 | menuItems[numberOfItems] = menuItem; 38 | numberOfItems = numberOfItems + 1; 39 | } 40 | } 41 | 42 | public MenuItem[] getMenuItems() { 43 | return menuItems; 44 | } 45 | 46 | public Iterator createIterator() { 47 | return new DinerMenuIterator(menuItems); 48 | // To test Alternating menu items, comment out above line, 49 | // and uncomment the line below. 50 | //return new AlternatingDinerMenuIterator(menuItems); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/ObserverPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.google.common.eventbus.EventBus; 4 | import com.observer.demo.weatherStationV1.CurrentConditionDisplay; 5 | import com.observer.demo.weatherStationV1.WeatherData; 6 | import com.observer.demo.weatherStationWithEventBus.CurrentConditionDisplayBySubscribe; 7 | import com.observer.demo.weatherStationWithEventBus.WeatherDataPOJO; 8 | import org.junit.Test; 9 | 10 | import static org.junit.Assert.assertEquals; 11 | 12 | /** 13 | * User: Shawn cao 14 | * Date: 15/1/26 15 | * Time: PM5:12 16 | */ 17 | public class ObserverPatternTest { 18 | 19 | private float t1=10,t2=20,h1=75,h2=80,p1=30.4f,p2=44.2f; 20 | @Test 21 | public void testChanged(){ 22 | WeatherData data = new WeatherData(); 23 | 24 | CurrentConditionDisplay current = new CurrentConditionDisplay(data); 25 | data.setMeasurements(t1,h1,p1); 26 | 27 | String display = current.display(); 28 | assertEquals("current ", display, "Current conditions:" + t1 + "F degrees and " + h1 + "% humidity"+p1); 29 | 30 | 31 | data.setMeasurements(t2,h2,p2); 32 | display = current.display(); 33 | assertEquals("current ", display, "Current conditions:" + t2 + "F degrees and " + h2 + "% humidity"+p2); 34 | 35 | } 36 | 37 | @Test 38 | public void testChangedByEventBus(){ 39 | 40 | CurrentConditionDisplayBySubscribe current = new CurrentConditionDisplayBySubscribe(); 41 | EventBus eventBus = new EventBus(); 42 | eventBus.register(current); 43 | eventBus.post(new WeatherDataPOJO(t1,h1,p1)); 44 | String display = current.display(); 45 | assertEquals("current ", display, "Current conditions:" + t1 + "F degrees and " + h1 + "% humidity"+p1); 46 | 47 | eventBus.post(new WeatherDataPOJO(t2,h2,p2)); 48 | display = current.display(); 49 | assertEquals("current ", display, "Current conditions:" + t2 + "F degrees and " + h2 + "% humidity"+p2); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/command/demo/remote/RemoteLoader.java: -------------------------------------------------------------------------------- 1 | package com.command.demo.remote; 2 | 3 | public class RemoteLoader { 4 | 5 | public static void main(String[] args) { 6 | RemoteControl remoteControl = new RemoteControl(); 7 | 8 | Light livingRoomLight = new Light("Living Room"); 9 | Light kitchenLight = new Light("Kitchen"); 10 | CeilingFan ceilingFan= new CeilingFan("Living Room"); 11 | GarageDoor garageDoor = new GarageDoor(""); 12 | Stereo stereo = new Stereo("Living Room"); 13 | 14 | LightOnCommand livingRoomLightOn = 15 | new LightOnCommand(livingRoomLight); 16 | LightOffCommand livingRoomLightOff = 17 | new LightOffCommand(livingRoomLight); 18 | LightOnCommand kitchenLightOn = 19 | new LightOnCommand(kitchenLight); 20 | LightOffCommand kitchenLightOff = 21 | new LightOffCommand(kitchenLight); 22 | 23 | CeilingFanOnCommand ceilingFanOn = 24 | new CeilingFanOnCommand(ceilingFan); 25 | CeilingFanOffCommand ceilingFanOff = 26 | new CeilingFanOffCommand(ceilingFan); 27 | 28 | GarageDoorUpCommand garageDoorUp = 29 | new GarageDoorUpCommand(garageDoor); 30 | GarageDoorDownCommand garageDoorDown = 31 | new GarageDoorDownCommand(garageDoor); 32 | 33 | StereoOnWithCDCommand stereoOnWithCD = 34 | new StereoOnWithCDCommand(stereo); 35 | StereoOffCommand stereoOff = 36 | new StereoOffCommand(stereo); 37 | 38 | remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); 39 | remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); 40 | remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); 41 | remoteControl.setCommand(3, stereoOnWithCD, stereoOff); 42 | 43 | System.out.println(remoteControl); 44 | 45 | remoteControl.onButtonWasPushed(0); 46 | remoteControl.offButtonWasPushed(0); 47 | remoteControl.onButtonWasPushed(1); 48 | remoteControl.offButtonWasPushed(1); 49 | remoteControl.onButtonWasPushed(2); 50 | remoteControl.offButtonWasPushed(2); 51 | remoteControl.onButtonWasPushed(3); 52 | remoteControl.offButtonWasPushed(3); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/FactoryPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.factory.demo.pizzastore.ChicagoStylePizzaStore; 4 | import com.factory.demo.pizzastore.NYStylePizzaStore; 5 | import com.factory.demo.pizzastore.Pizza; 6 | import com.factory.demo.pizzastore.PizzaStore; 7 | import com.factory.demo.pizzastoreWithIngredient.ChicagoPizzaStore; 8 | import com.factory.demo.pizzastoreWithIngredient.NYPizzaStore; 9 | import org.junit.Test; 10 | 11 | import static junit.framework.Assert.assertEquals; 12 | 13 | /** 14 | * User: Shawn cao 15 | * Date: 15/1/30 16 | * Time: PM2:53 17 | */ 18 | public class FactoryPatternTest { 19 | 20 | 21 | @Test 22 | public void test_store_making_diff_flavor_pizza(){ 23 | 24 | Pizza pizza = new NYStylePizzaStore().orderPizza(PizzaStore.PizzaType.cheese); 25 | assertEquals("NY Style Sauce and Cheese Pizza",pizza.getName()); 26 | 27 | pizza = new ChicagoStylePizzaStore().orderPizza(PizzaStore.PizzaType.cheese); 28 | assertEquals("Chicago cheese pizza", pizza.getName()); 29 | } 30 | 31 | @Test 32 | public void test_store_with_diff_ingredients(){ 33 | com.factory.demo.pizzastoreWithIngredient.PizzaStore nyStore = new NYPizzaStore(); 34 | com.factory.demo.pizzastoreWithIngredient.PizzaStore chicagoStore = new ChicagoPizzaStore(); 35 | 36 | com.factory.demo.pizzastoreWithIngredient.Pizza pizza = nyStore.orderPizza("cheese"); 37 | System.out.println("Ethan ordered a " + pizza + "\n"); 38 | 39 | pizza = chicagoStore.orderPizza("cheese"); 40 | System.out.println("Joel ordered a " + pizza + "\n"); 41 | 42 | pizza = nyStore.orderPizza("clam"); 43 | System.out.println("Ethan ordered a " + pizza + "\n"); 44 | 45 | pizza = chicagoStore.orderPizza("clam"); 46 | System.out.println("Joel ordered a " + pizza + "\n"); 47 | 48 | pizza = nyStore.orderPizza("pepperoni"); 49 | System.out.println("Ethan ordered a " + pizza + "\n"); 50 | 51 | pizza = chicagoStore.orderPizza("pepperoni"); 52 | System.out.println("Joel ordered a " + pizza + "\n"); 53 | 54 | pizza = nyStore.orderPizza("veggie"); 55 | System.out.println("Ethan ordered a " + pizza + "\n"); 56 | 57 | pizza = chicagoStore.orderPizza("veggie"); 58 | System.out.println("Joel ordered a " + pizza + "\n"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/state/demo/GumballMachine.java: -------------------------------------------------------------------------------- 1 | package com.state.demo; 2 | 3 | public class GumballMachine { 4 | 5 | State soldOutState; 6 | State noQuarterState; 7 | State hasQuarterState; 8 | State soldState; 9 | State winnerState; 10 | 11 | State state = soldOutState; 12 | int count = 0; 13 | 14 | public GumballMachine(int numberGumballs) { 15 | soldOutState = new SoldOutState(this); 16 | noQuarterState = new NoQuarterState(this); 17 | hasQuarterState = new HasQuarterState(this); 18 | soldState = new SoldState(this); 19 | winnerState = new WinnerState(this); 20 | 21 | this.count = numberGumballs; 22 | if (numberGumballs > 0) { 23 | state = noQuarterState; 24 | } 25 | } 26 | 27 | public void insertQuarter() { 28 | state.insertQuarter(); 29 | } 30 | 31 | public void ejectQuarter() { 32 | state.ejectQuarter(); 33 | } 34 | 35 | public void turnCrank() { 36 | state.turnCrank(); 37 | state.dispense(); 38 | } 39 | 40 | void setState(State state) { 41 | this.state = state; 42 | } 43 | 44 | void releaseBall() { 45 | System.out.println("A gumball comes rolling out the slot..."); 46 | if (count != 0) { 47 | count = count - 1; 48 | } 49 | } 50 | 51 | int getCount() { 52 | return count; 53 | } 54 | 55 | void refill(int count) { 56 | this.count += count; 57 | System.out.println("The gumball machine was just refilled; it's new count is: " + this.count); 58 | state.refill(); 59 | } 60 | 61 | public State getState() { 62 | return state; 63 | } 64 | 65 | public State getSoldOutState() { 66 | return soldOutState; 67 | } 68 | 69 | public State getNoQuarterState() { 70 | return noQuarterState; 71 | } 72 | 73 | public State getHasQuarterState() { 74 | return hasQuarterState; 75 | } 76 | 77 | public State getSoldState() { 78 | return soldState; 79 | } 80 | 81 | public State getWinnerState() { 82 | return winnerState; 83 | } 84 | 85 | public String toString() { 86 | StringBuffer result = new StringBuffer(); 87 | result.append("\nMighty Gumball, Inc."); 88 | result.append("\nJava-enabled Standing Gumball Model #2004"); 89 | result.append("\nInventory: " + count + " gumball"); 90 | if (count != 1) { 91 | result.append("s"); 92 | } 93 | result.append("\n"); 94 | result.append("Machine is " + state + "\n"); 95 | return result.toString(); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menuiterator/MenuTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menuiterator; 2 | 3 | public class MenuTestDrive { 4 | public static void main(String args[]) { 5 | 6 | MenuComponent pancakeHouseMenu = 7 | new Menu("PANCAKE HOUSE MENU", "Breakfast"); 8 | MenuComponent dinerMenu = 9 | new Menu("DINER MENU", "Lunch"); 10 | MenuComponent cafeMenu = 11 | new Menu("CAFE MENU", "Dinner"); 12 | MenuComponent dessertMenu = 13 | new Menu("DESSERT MENU", "Dessert of course!"); 14 | 15 | MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); 16 | 17 | allMenus.add(pancakeHouseMenu); 18 | allMenus.add(dinerMenu); 19 | allMenus.add(cafeMenu); 20 | 21 | pancakeHouseMenu.add(new MenuItem( 22 | "K&B's Pancake Breakfast", 23 | "Pancakes with scrambled eggs, and toast", 24 | true, 25 | 2.99)); 26 | pancakeHouseMenu.add(new MenuItem( 27 | "Regular Pancake Breakfast", 28 | "Pancakes with fried eggs, sausage", 29 | false, 30 | 2.99)); 31 | pancakeHouseMenu.add(new MenuItem( 32 | "Blueberry Pancakes", 33 | "Pancakes made with fresh blueberries, and blueberry syrup", 34 | true, 35 | 3.49)); 36 | pancakeHouseMenu.add(new MenuItem( 37 | "Waffles", 38 | "Waffles, with your choice of blueberries or strawberries", 39 | true, 40 | 3.59)); 41 | 42 | dinerMenu.add(new MenuItem( 43 | "Vegetarian BLT", 44 | "(Fakin') Bacon with lettuce & tomato on whole wheat", 45 | true, 46 | 2.99)); 47 | dinerMenu.add(new MenuItem( 48 | "BLT", 49 | "Bacon with lettuce & tomato on whole wheat", 50 | false, 51 | 2.99)); 52 | dinerMenu.add(new MenuItem( 53 | "Soup of the day", 54 | "A bowl of the soup of the day, with a side of potato salad", 55 | false, 56 | 3.29)); 57 | dinerMenu.add(new MenuItem( 58 | "Hotdog", 59 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 60 | false, 61 | 3.05)); 62 | dinerMenu.add(new MenuItem( 63 | "Steamed Veggies and Brown Rice", 64 | "A medly of steamed vegetables over brown rice", 65 | true, 66 | 3.99)); 67 | 68 | dinerMenu.add(new MenuItem( 69 | "Pasta", 70 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 71 | true, 72 | 3.89)); 73 | 74 | dinerMenu.add(dessertMenu); 75 | 76 | dessertMenu.add(new MenuItem( 77 | "Apple Pie", 78 | "Apple pie with a flakey crust, topped with vanilla icecream", 79 | true, 80 | 1.59)); 81 | dessertMenu.add(new MenuItem( 82 | "Cheesecake", 83 | "Creamy New York cheesecake, with a chocolate graham crust", 84 | true, 85 | 1.99)); 86 | dessertMenu.add(new MenuItem( 87 | "Sorbet", 88 | "A scoop of raspberry and a scoop of lime", 89 | true, 90 | 1.89)); 91 | 92 | cafeMenu.add(new MenuItem( 93 | "Veggie Burger and Air Fries", 94 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 95 | true, 96 | 3.99)); 97 | cafeMenu.add(new MenuItem( 98 | "Soup of the day", 99 | "A cup of the soup of the day, with a side salad", 100 | false, 101 | 3.69)); 102 | cafeMenu.add(new MenuItem( 103 | "Burrito", 104 | "A large burrito, with whole pinto beans, salsa, guacamole", 105 | true, 106 | 4.29)); 107 | 108 | Waitress waitress = new Waitress(allMenus); 109 | 110 | waitress.printVegetarianMenu(); 111 | 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/com/composite/demo/menu/MenuTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.composite.demo.menu; 2 | 3 | public class MenuTestDrive { 4 | public static void main(String args[]) { 5 | MenuComponent pancakeHouseMenu = 6 | new Menu("PANCAKE HOUSE MENU", "Breakfast"); 7 | MenuComponent dinerMenu = 8 | new Menu("DINER MENU", "Lunch"); 9 | MenuComponent cafeMenu = 10 | new Menu("CAFE MENU", "Dinner"); 11 | MenuComponent dessertMenu = 12 | new Menu("DESSERT MENU", "Dessert of course!"); 13 | MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee"); 14 | 15 | MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); 16 | 17 | allMenus.add(pancakeHouseMenu); 18 | allMenus.add(dinerMenu); 19 | allMenus.add(cafeMenu); 20 | 21 | pancakeHouseMenu.add(new MenuItem( 22 | "K&B's Pancake Breakfast", 23 | "Pancakes with scrambled eggs, and toast", 24 | true, 25 | 2.99)); 26 | pancakeHouseMenu.add(new MenuItem( 27 | "Regular Pancake Breakfast", 28 | "Pancakes with fried eggs, sausage", 29 | false, 30 | 2.99)); 31 | pancakeHouseMenu.add(new MenuItem( 32 | "Blueberry Pancakes", 33 | "Pancakes made with fresh blueberries, and blueberry syrup", 34 | true, 35 | 3.49)); 36 | pancakeHouseMenu.add(new MenuItem( 37 | "Waffles", 38 | "Waffles, with your choice of blueberries or strawberries", 39 | true, 40 | 3.59)); 41 | 42 | dinerMenu.add(new MenuItem( 43 | "Vegetarian BLT", 44 | "(Fakin') Bacon with lettuce & tomato on whole wheat", 45 | true, 46 | 2.99)); 47 | dinerMenu.add(new MenuItem( 48 | "BLT", 49 | "Bacon with lettuce & tomato on whole wheat", 50 | false, 51 | 2.99)); 52 | dinerMenu.add(new MenuItem( 53 | "Soup of the day", 54 | "A bowl of the soup of the day, with a side of potato salad", 55 | false, 56 | 3.29)); 57 | dinerMenu.add(new MenuItem( 58 | "Hotdog", 59 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 60 | false, 61 | 3.05)); 62 | dinerMenu.add(new MenuItem( 63 | "Steamed Veggies and Brown Rice", 64 | "Steamed vegetables over brown rice", 65 | true, 66 | 3.99)); 67 | 68 | dinerMenu.add(new MenuItem( 69 | "Pasta", 70 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 71 | true, 72 | 3.89)); 73 | 74 | dinerMenu.add(dessertMenu); 75 | 76 | dessertMenu.add(new MenuItem( 77 | "Apple Pie", 78 | "Apple pie with a flakey crust, topped with vanilla icecream", 79 | true, 80 | 1.59)); 81 | 82 | dessertMenu.add(new MenuItem( 83 | "Cheesecake", 84 | "Creamy New York cheesecake, with a chocolate graham crust", 85 | true, 86 | 1.99)); 87 | dessertMenu.add(new MenuItem( 88 | "Sorbet", 89 | "A scoop of raspberry and a scoop of lime", 90 | true, 91 | 1.89)); 92 | 93 | cafeMenu.add(new MenuItem( 94 | "Veggie Burger and Air Fries", 95 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 96 | true, 97 | 3.99)); 98 | cafeMenu.add(new MenuItem( 99 | "Soup of the day", 100 | "A cup of the soup of the day, with a side salad", 101 | false, 102 | 3.69)); 103 | cafeMenu.add(new MenuItem( 104 | "Burrito", 105 | "A large burrito, with whole pinto beans, salsa, guacamole", 106 | true, 107 | 4.29)); 108 | 109 | cafeMenu.add(coffeeMenu); 110 | 111 | coffeeMenu.add(new MenuItem( 112 | "Coffee Cake", 113 | "Crumbly cake topped with cinnamon and walnuts", 114 | true, 115 | 1.59)); 116 | coffeeMenu.add(new MenuItem( 117 | "Bagel", 118 | "Flavors include sesame, poppyseed, cinnamon raisin, pumpkin", 119 | false, 120 | 0.69)); 121 | coffeeMenu.add(new MenuItem( 122 | "Biscotti", 123 | "Three almond or hazelnut biscotti cookies", 124 | true, 125 | 0.89)); 126 | 127 | Waitress waitress = new Waitress(allMenus); 128 | 129 | waitress.printMenu(); 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /test/main/java/com/shawn/patterns/CompositePatternTest.java: -------------------------------------------------------------------------------- 1 | package com.shawn.patterns; 2 | 3 | import com.composite.demo.menuiterator.Menu; 4 | import com.composite.demo.menuiterator.MenuComponent; 5 | import com.composite.demo.menuiterator.MenuItem; 6 | import com.composite.demo.menuiterator.Waitress; 7 | import org.junit.Before; 8 | import org.junit.Test; 9 | 10 | /** 11 | * @author Shawn Cao 12 | */ 13 | public class CompositePatternTest { 14 | 15 | MenuComponent allMenus; 16 | 17 | @Before 18 | public void setUp(){ 19 | MenuComponent pancakeHouseMenu = 20 | new Menu("PANCAKE HOUSE MENU", "Breakfast"); 21 | MenuComponent dinerMenu = 22 | new Menu("DINER MENU", "Lunch"); 23 | MenuComponent cafeMenu = 24 | new Menu("CAFE MENU", "Dinner"); 25 | MenuComponent dessertMenu = 26 | new Menu("DESSERT MENU", "Dessert of course!"); 27 | 28 | MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); 29 | 30 | allMenus.add(pancakeHouseMenu); 31 | allMenus.add(dinerMenu); 32 | allMenus.add(cafeMenu); 33 | 34 | pancakeHouseMenu.add(new MenuItem( 35 | "K&B's Pancake Breakfast", 36 | "Pancakes with scrambled eggs, and toast", 37 | true, 38 | 2.99)); 39 | pancakeHouseMenu.add(new MenuItem( 40 | "Regular Pancake Breakfast", 41 | "Pancakes with fried eggs, sausage", 42 | false, 43 | 2.99)); 44 | pancakeHouseMenu.add(new MenuItem( 45 | "Blueberry Pancakes", 46 | "Pancakes made with fresh blueberries, and blueberry syrup", 47 | true, 48 | 3.49)); 49 | pancakeHouseMenu.add(new MenuItem( 50 | "Waffles", 51 | "Waffles, with your choice of blueberries or strawberries", 52 | true, 53 | 3.59)); 54 | 55 | dinerMenu.add(new MenuItem( 56 | "Vegetarian BLT", 57 | "(Fakin') Bacon with lettuce & tomato on whole wheat", 58 | true, 59 | 2.99)); 60 | dinerMenu.add(new MenuItem( 61 | "BLT", 62 | "Bacon with lettuce & tomato on whole wheat", 63 | false, 64 | 2.99)); 65 | dinerMenu.add(new MenuItem( 66 | "Soup of the day", 67 | "A bowl of the soup of the day, with a side of potato salad", 68 | false, 69 | 3.29)); 70 | dinerMenu.add(new MenuItem( 71 | "Hotdog", 72 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 73 | false, 74 | 3.05)); 75 | dinerMenu.add(new MenuItem( 76 | "Steamed Veggies and Brown Rice", 77 | "A medly of steamed vegetables over brown rice", 78 | true, 79 | 3.99)); 80 | 81 | dinerMenu.add(new MenuItem( 82 | "Pasta", 83 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 84 | true, 85 | 3.89)); 86 | 87 | dinerMenu.add(dessertMenu); 88 | 89 | dessertMenu.add(new MenuItem( 90 | "Apple Pie", 91 | "Apple pie with a flakey crust, topped with vanilla icecream", 92 | true, 93 | 1.59)); 94 | dessertMenu.add(new MenuItem( 95 | "Cheesecake", 96 | "Creamy New York cheesecake, with a chocolate graham crust", 97 | true, 98 | 1.99)); 99 | dessertMenu.add(new MenuItem( 100 | "Sorbet", 101 | "A scoop of raspberry and a scoop of lime", 102 | true, 103 | 1.89)); 104 | 105 | cafeMenu.add(new MenuItem( 106 | "Veggie Burger and Air Fries", 107 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 108 | true, 109 | 3.99)); 110 | cafeMenu.add(new MenuItem( 111 | "Soup of the day", 112 | "A cup of the soup of the day, with a side salad", 113 | false, 114 | 3.69)); 115 | cafeMenu.add(new MenuItem( 116 | "Burrito", 117 | "A large burrito, with whole pinto beans, salsa, guacamole", 118 | true, 119 | 4.29)); 120 | this.allMenus = allMenus; 121 | } 122 | 123 | @Test 124 | public void testComposite(){ 125 | Waitress waitress = new Waitress(allMenus); 126 | 127 | waitress.printVegetarianMenu(); 128 | } 129 | 130 | } 131 | --------------------------------------------------------------------------------