├── Strategy ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── QuackBehavior.java │ │ │ ├── FlyBehavior.java │ │ │ ├── Quack.java │ │ │ ├── Squeak.java │ │ │ ├── FakeQuack.java │ │ │ ├── FlyNoWay.java │ │ │ ├── FlyWithWings.java │ │ │ ├── MuteQuack.java │ │ │ ├── FlyRocketPowered.java │ │ │ ├── DecoyDuck.java │ │ │ ├── ModelDuck.java │ │ │ ├── RubberDuck.java │ │ │ ├── RedHeadDuck.java │ │ │ ├── MallardDuck.java │ │ │ └── Duck.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ ├── MiniDuckSimulator1.java │ │ └── MiniDuckSimulator.java ├── Strategy.iml └── pom.xml ├── Adapter ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Duck.java │ │ │ ├── Turkey.java │ │ │ ├── MallardDuck.java │ │ │ ├── WildTurkey.java │ │ │ ├── TurkeyAdapter.java │ │ │ └── DuckAdapter.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ ├── TurkeyTest.java │ │ └── DuckTest.java ├── Adapter.iml └── pom.xml ├── Factory ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── AbstractFactory │ │ │ ├── Cheese.java │ │ │ ├── Clams.java │ │ │ ├── Dough.java │ │ │ ├── Sauce.java │ │ │ ├── Veggies.java │ │ │ ├── Pepperoni.java │ │ │ ├── Onion.java │ │ │ ├── Garlic.java │ │ │ ├── Spinach.java │ │ │ ├── Eggplant.java │ │ │ ├── Mushroom.java │ │ │ ├── RedPepper.java │ │ │ ├── BlackOlives.java │ │ │ ├── MarinaraSauce.java │ │ │ ├── ThinCrustDough.java │ │ │ ├── ReggianoCheese.java │ │ │ ├── ParmesanCheese.java │ │ │ ├── FreshClams.java │ │ │ ├── FrozenClams.java │ │ │ ├── MozzarellaCheese.java │ │ │ ├── SlicedPepperoni.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 │ │ │ └── FactoryMethod │ │ │ ├── NYStyleCheesePizza.java │ │ │ ├── NYStyleClamPizza.java │ │ │ ├── PizzaStore.java │ │ │ ├── NYStyleVeggiePizza.java │ │ │ ├── ChicagoStyleCheesePizza.java │ │ │ ├── ChicagoStyleClamPizza.java │ │ │ ├── NYStylePepperoniPizza.java │ │ │ ├── NYPizzaStore.java │ │ │ ├── ChicagoStyleVeggiePizza.java │ │ │ ├── ChicagoStylePepperoniPizza.java │ │ │ ├── ChicagoPizzaStore.java │ │ │ ├── DependentPizzaStore.java │ │ │ └── Pizza.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ ├── AbstractFactory │ │ └── PizzaTestDrive.java │ │ └── FactoryMethod │ │ └── PizzaTestDrive.java ├── Factory.iml └── pom.xml ├── Command ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Command │ │ │ ├── Command.java │ │ │ ├── NoCommand.java │ │ │ ├── TVOffCommand.java │ │ │ ├── TVOnCommand.java │ │ │ ├── LightOnCommand.java │ │ │ ├── LightOffCommand.java │ │ │ ├── StereoOnCommand.java │ │ │ ├── LivingroomLightOffCommand.java │ │ │ ├── LivingroomLightOnCommand.java │ │ │ ├── StereoOffCommand.java │ │ │ ├── HottubOffCommand.java │ │ │ ├── HottubOnCommand.java │ │ │ ├── StereoOnWithCDCommand.java │ │ │ ├── MacroCommand.java │ │ │ ├── CeilingFanOffCommand.java │ │ │ ├── CeilingFanHighCommand.java │ │ │ └── CeilingFanMediumCommand.java │ │ │ ├── Receiver │ │ │ ├── TV.java │ │ │ ├── Light.java │ │ │ ├── Stereo.java │ │ │ ├── Hottub.java │ │ │ └── CeilingFan.java │ │ │ └── Invoker │ │ │ └── RemoteControl.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── RemoteLoader.java ├── Command.iml └── pom.xml ├── Iterator ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Menu.java │ │ │ ├── MenuItem.java │ │ │ ├── AlternatingDinerMenuIterator.java │ │ │ ├── DinerMenuIterator.java │ │ │ ├── CafeMenu.java │ │ │ ├── PancakeHouseMenu.java │ │ │ ├── DinerMenu.java │ │ │ └── Waitress.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── MenuTestDrive.java ├── Iterator.iml └── pom.xml ├── Observer ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── DisplayElement.java │ │ │ ├── WeatherData.java │ │ │ └── CurrentConditionsDisplay.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── WeatherSationTest.java ├── Observer.iml └── pom.xml ├── .idea ├── vcs.xml ├── libraries │ └── Maven__junit_junit_3_8_1.xml ├── misc.xml ├── encodings.xml ├── compiler.xml ├── modules.xml └── uiDesigner.xml ├── State ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── State.java │ │ │ ├── SoldOutState.java │ │ │ ├── NoQuarterState.java │ │ │ ├── SoldState.java │ │ │ ├── HasQuarterState.java │ │ │ ├── WinnerState.java │ │ │ └── GumballMachine.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── GumballMachineTestDrive.java ├── State.iml └── pom.xml ├── Singleton ├── src │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── DCLTest.java │ │ │ └── ChocolateController.java │ └── main │ │ └── java │ │ └── com │ │ └── crow │ │ ├── DCL.java │ │ └── ChocolateBoiler.java ├── Singleton.iml └── pom.xml ├── Template ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Tea.java │ │ │ ├── Coffee.java │ │ │ ├── CaffeineBeverage.java │ │ │ ├── CaffeineBeverageWithHook.java │ │ │ ├── TeaWithHook.java │ │ │ └── CoffeeWithHook.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── BeverageTestDrive.java ├── Template.iml └── pom.xml ├── Proxy ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── remoteproxy │ │ │ ├── State.java │ │ │ ├── GumballMachineRemote.java │ │ │ ├── GumballMonitor.java │ │ │ ├── GumballMachineTestDrive.java │ │ │ ├── SoldOutState.java │ │ │ ├── NoQuarterState.java │ │ │ ├── SoldState.java │ │ │ ├── HasQuarterState.java │ │ │ ├── WinnerState.java │ │ │ └── GumballMachine.java │ │ │ └── dynamicproxy │ │ │ ├── PersonBean.java │ │ │ ├── OwnerInvocationHandler.java │ │ │ ├── NonOwnerInvocationHandler.java │ │ │ └── PersonBeanImpl.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ ├── remoteproxy │ │ └── GumballMonitorTestDrive.java │ │ └── dynamicproxy │ │ └── MatchMakingTestDrive.java ├── Proxy.iml └── pom.xml ├── Decorator ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Decorator │ │ │ ├── CondimentDecorator.java │ │ │ ├── Soy.java │ │ │ ├── Whip.java │ │ │ └── Mocha.java │ │ │ ├── Beverages │ │ │ ├── Espresso.java │ │ │ ├── HouseBlend.java │ │ │ └── Beverage.java │ │ │ └── JavaIO │ │ │ └── LowerCaseInputStream.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ ├── InputTest.java │ │ └── CoffeeTest.java ├── Decorator.iml └── pom.xml ├── Composite ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── NullIterator.java │ │ │ ├── Waitress.java │ │ │ ├── MenuComponent.java │ │ │ ├── CompositeIterator.java │ │ │ ├── MenuItem.java │ │ │ └── Menu.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── MenuTestDrive.java ├── Composite.iml └── pom.xml ├── Facade ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── crow │ │ │ ├── Screen.java │ │ │ ├── PopcornPopper.java │ │ │ ├── TheaterLights.java │ │ │ ├── Projector.java │ │ │ ├── Tuner.java │ │ │ ├── Amplifier.java │ │ │ ├── CdPlayer.java │ │ │ ├── DvdPlayer.java │ │ │ └── HomeTheaterFacade.java │ └── test │ │ └── java │ │ └── com │ │ └── crow │ │ └── HomeTheaterTestDrive.java ├── Facade.iml └── pom.xml ├── DesignPattern.iml ├── README.md └── pom.xml /Strategy/src/main/java/com/crow/QuackBehavior.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public interface QuackBehavior { 4 | public void quack(); 5 | } 6 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/Duck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public interface Duck { 4 | public void quack(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/Turkey.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public interface Turkey { 4 | public void gobble(); 5 | public void fly(); 6 | } 7 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Cheese.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Cheese { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Clams.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Clams { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Dough.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Dough { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Sauce.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Sauce { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Veggies.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Veggies { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/Command.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | public interface Command { 4 | public void execute(); 5 | public void undo(); 6 | } 7 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Pepperoni.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public interface Pepperoni { 4 | public String toString(); 5 | } 6 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/Menu.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | 5 | public interface Menu { 6 | public Iterator createIterator(); 7 | } 8 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/FlyBehavior.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public interface FlyBehavior {//定义算法族,分别封装起来,可以互相替换,让算法的变化独立于使用算法的客户 4 | public void fly(); 5 | } 6 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/Quack.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Quack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/Squeak.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Squeak implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Squeak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/NoCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | public class NoCommand implements Command { 4 | public void execute() { } 5 | public void undo() { } 6 | } 7 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/FakeQuack.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class FakeQuack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("Qwak"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/FlyNoWay.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class FlyNoWay implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I can't fly"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Observer/src/main/java/com/crow/DisplayElement.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/9. 5 | */ 6 | public interface DisplayElement { 7 | public void display(); 8 | } 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/FlyWithWings.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class FlyWithWings implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I'm flying!!"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/MuteQuack.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MuteQuack implements QuackBehavior { 4 | public void quack() { 5 | System.out.println("<< Silence >>"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Onion.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class Onion implements Veggies { 4 | 5 | public String toString() { 6 | return "Onion"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Garlic.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class Garlic implements Veggies { 4 | 5 | public String toString() { 6 | return "Garlic"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Spinach.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class Spinach implements Veggies { 4 | 5 | public String toString() { 6 | return "Spinach"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Eggplant.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class Eggplant implements Veggies { 4 | 5 | public String toString() { 6 | return "Eggplant"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Mushroom.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class Mushroom implements Veggies { 4 | 5 | public String toString() { 6 | return "Mushrooms"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/RedPepper.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class RedPepper implements Veggies { 4 | 5 | public String toString() { 6 | return "Red Pepper"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/FlyRocketPowered.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class FlyRocketPowered implements FlyBehavior { 4 | public void fly() { 5 | System.out.println("I'm flying with a rocket"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/BlackOlives.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class BlackOlives implements Veggies { 4 | 5 | public String toString() { 6 | return "Black Olives"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/MarinaraSauce.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class MarinaraSauce implements Sauce { 4 | public String toString() { 5 | return "Marinara Sauce"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ThinCrustDough.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class ThinCrustDough implements Dough { 4 | public String toString() { 5 | return "Thin Crust Dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ReggianoCheese.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class ReggianoCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Reggiano Cheese"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/State.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public interface State { 4 | 5 | public void insertQuarter(); 6 | public void ejectQuarter(); 7 | public void turnCrank(); 8 | public void dispense(); 9 | } 10 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ParmesanCheese.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class ParmesanCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Parmesan"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/FreshClams.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class FreshClams implements Clams { 4 | 5 | public String toString() { 6 | return "Fresh Clams from Long Island Sound"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/FrozenClams.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class FrozenClams implements Clams { 4 | 5 | public String toString() { 6 | return "Frozen Clams from Chesapeake Bay"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/MozzarellaCheese.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class MozzarellaCheese implements Cheese { 4 | 5 | public String toString() { 6 | return "Shredded Mozzarella"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/SlicedPepperoni.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class SlicedPepperoni implements Pepperoni { 4 | 5 | public String toString() { 6 | return "Sliced Pepperoni"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/PlumTomatoSauce.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class PlumTomatoSauce implements Sauce { 4 | public String toString() { 5 | return "Tomato sauce with plum tomatoes"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ThickCrustDough.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class ThickCrustDough implements Dough { 4 | public String toString() { 5 | return "ThickCrust style extra thick crust dough"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Singleton/src/test/java/com/crow/DCLTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/11. 5 | */ 6 | public class DCLTest { 7 | public static void main(String[] args) { 8 | DCL dcl = DCL.getInstance(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MallardDuck implements Duck { 4 | public void quack() { 5 | System.out.println("Quack"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/Tea.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Tea extends CaffeineBeverage { 4 | public void brew() { 5 | System.out.println("Steeping the tea"); 6 | } 7 | public void addCondiments() { 8 | System.out.println("Adding Lemon"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/WildTurkey.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class WildTurkey implements Turkey { 4 | public void gobble() { 5 | System.out.println("Gobble gobble"); 6 | } 7 | 8 | public void fly() { 9 | System.out.println("I'm flying a short distance"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/State.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.io.*; 4 | 5 | public interface State extends Serializable { 6 | public void insertQuarter(); 7 | public void ejectQuarter(); 8 | public void turnCrank(); 9 | public void dispense(); 10 | } 11 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/DecoyDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class DecoyDuck extends Duck { 4 | public DecoyDuck() { 5 | setFlyBehavior(new FlyNoWay()); 6 | setQuackBehavior(new MuteQuack()); 7 | } 8 | public void display() { 9 | System.out.println("I'm a duck Decoy"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/ModelDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class ModelDuck extends Duck { 4 | public ModelDuck() { 5 | flyBehavior = new FlyNoWay(); 6 | quackBehavior = new Quack(); 7 | } 8 | 9 | public void display() { 10 | System.out.println("I'm a model duck"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/Coffee.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Coffee extends CaffeineBeverage { 4 | public void brew() { 5 | System.out.println("Dripping Coffee through filter"); 6 | } 7 | public void addCondiments() { 8 | System.out.println("Adding Sugar and Milk"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Decorator/CondimentDecorator.java: -------------------------------------------------------------------------------- 1 | package com.crow.Decorator; 2 | 3 | import com.crow.Beverages.Beverage; 4 | 5 | /** 6 | * Created by CrowHawk on 17/7/10. 7 | */ 8 | public abstract class CondimentDecorator extends Beverage { 9 | public abstract String getDescription(); 10 | } 11 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/RubberDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class RubberDuck extends Duck { 4 | 5 | public RubberDuck() { 6 | flyBehavior = new FlyNoWay(); 7 | quackBehavior = new Squeak(); 8 | } 9 | 10 | public void display() { 11 | System.out.println("I'm a rubber duckie"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/RedHeadDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class RedHeadDuck extends Duck { 4 | 5 | public RedHeadDuck() { 6 | flyBehavior = new FlyWithWings(); 7 | quackBehavior = new Quack(); 8 | } 9 | 10 | public void display() { 11 | System.out.println("I'm a real Red Headed duck"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Beverages/Espresso.java: -------------------------------------------------------------------------------- 1 | package com.crow.Beverages; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/10. 5 | */ 6 | public class Espresso extends Beverage { 7 | 8 | public Espresso() { 9 | description = "Espresso"; 10 | } 11 | 12 | public double cost() { 13 | return 1.99; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/GumballMachineRemote.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.rmi.*; 4 | 5 | public interface GumballMachineRemote extends Remote { 6 | public int getCount() throws RemoteException; 7 | public String getLocation() throws RemoteException; 8 | public State getState() throws RemoteException; 9 | } 10 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Beverages/HouseBlend.java: -------------------------------------------------------------------------------- 1 | package com.crow.Beverages; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/10. 5 | */ 6 | public class HouseBlend extends Beverage { 7 | public HouseBlend() { 8 | description = "House Blend Coffee"; 9 | } 10 | 11 | public double cost() { 12 | return .89; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/MallardDuck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MallardDuck extends Duck { 4 | 5 | public MallardDuck() { 6 | 7 | quackBehavior = new Quack(); 8 | flyBehavior = new FlyWithWings(); 9 | 10 | 11 | } 12 | 13 | public void display() { 14 | System.out.println("I'm a real Mallard duck"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/TVOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.TV; 4 | 5 | public class TVOffCommand implements Command { 6 | TV tv; 7 | 8 | public TVOffCommand(TV tv) { 9 | this.tv= tv; 10 | } 11 | 12 | public void execute() { 13 | tv.off(); 14 | } 15 | 16 | public void undo() { 17 | tv.on(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Beverages/Beverage.java: -------------------------------------------------------------------------------- 1 | package com.crow.Beverages; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/10. 5 | */ 6 | public abstract class Beverage { 7 | public String description = "Unknown Beverage"; 8 | 9 | public String getDescription() { 10 | return description; 11 | } 12 | 13 | public abstract double cost(); 14 | } 15 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/NYStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class NYStyleCheesePizza extends Pizza { 4 | 5 | public NYStyleCheesePizza() { 6 | name = "NY Style Sauce and Cheese Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/PizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/NullIterator.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | 5 | public class NullIterator implements Iterator { 6 | 7 | public Object next() { 8 | return null; 9 | } 10 | 11 | public boolean hasNext() { 12 | return false; 13 | } 14 | 15 | public void remove() { 16 | throw new UnsupportedOperationException(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/TVOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.TV; 4 | 5 | public class TVOnCommand implements Command { 6 | TV tv; 7 | 8 | public TVOnCommand(TV tv) { 9 | this.tv= tv; 10 | } 11 | 12 | public void execute() { 13 | tv.on(); 14 | tv.setInputChannel(); 15 | } 16 | 17 | public void undo() { 18 | tv.off(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Adapter/src/test/java/com/crow/TurkeyTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class TurkeyTest { 4 | public static void main(String[] args) { 5 | MallardDuck duck = new MallardDuck(); 6 | Turkey duckAdapter = new DuckAdapter(duck); 7 | 8 | for(int i=0;i<10;i++) { 9 | System.out.println("The DuckAdapter says..."); 10 | duckAdapter.gobble(); 11 | duckAdapter.fly(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/LightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Light; 4 | 5 | public class LightOnCommand implements Command { 6 | Light light; 7 | 8 | public LightOnCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | public void execute() { 13 | light.on(); 14 | } 15 | 16 | public void undo() { 17 | light.off(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/LightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Light; 4 | 5 | public class LightOffCommand implements Command { 6 | Light light; 7 | 8 | public LightOffCommand(Light light) { 9 | this.light = light; 10 | } 11 | 12 | public void execute() { 13 | light.off(); 14 | } 15 | 16 | public void undo() { 17 | light.on(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/NYStyleClamPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class NYStyleClamPizza extends Pizza { 4 | 5 | public NYStyleClamPizza() { 6 | name = "NY Style Clam Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | toppings.add("Fresh Clams from Long Island Sound"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Singleton/src/test/java/com/crow/ChocolateController.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class ChocolateController { 4 | public static void main(String args[]) { 5 | ChocolateBoiler boiler = ChocolateBoiler.getInstance(); 6 | boiler.fill(); 7 | boiler.boil(); 8 | boiler.drain(); 9 | 10 | // will return the existing instance 11 | ChocolateBoiler boiler2 = ChocolateBoiler.getInstance(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/StereoOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Stereo; 4 | 5 | public class StereoOnCommand implements Command { 6 | Stereo stereo; 7 | 8 | public StereoOnCommand(Stereo stereo) { 9 | this.stereo = stereo; 10 | } 11 | 12 | public void execute() { 13 | stereo.on(); 14 | } 15 | 16 | public void undo() { 17 | stereo.off(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/dynamicproxy/PersonBean.java: -------------------------------------------------------------------------------- 1 | package com.crow.dynamicproxy; 2 | 3 | public interface PersonBean { 4 | 5 | String getName(); 6 | String getGender(); 7 | String getInterests(); 8 | int getHotOrNotRating(); 9 | 10 | void setName(String name); 11 | void setGender(String gender); 12 | void setInterests(String interests); 13 | void setHotOrNotRating(int rating); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/LivingroomLightOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Light; 4 | 5 | public class LivingroomLightOffCommand implements Command { 6 | Light light; 7 | 8 | public LivingroomLightOffCommand(Light light) { 9 | this.light = light; 10 | } 11 | public void execute() { 12 | light.off(); 13 | } 14 | public void undo() { 15 | light.on(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/LivingroomLightOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Light; 4 | 5 | public class LivingroomLightOnCommand implements Command { 6 | Light light; 7 | 8 | public LivingroomLightOnCommand(Light light) { 9 | this.light = light; 10 | } 11 | public void execute() { 12 | light.on(); 13 | } 14 | public void undo() { 15 | light.off(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/StereoOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Stereo; 4 | 5 | public class StereoOffCommand implements Command { 6 | Stereo stereo; 7 | 8 | public StereoOffCommand(Stereo stereo) { 9 | this.stereo = stereo; 10 | } 11 | 12 | public void execute() { 13 | stereo.off(); 14 | } 15 | 16 | public void undo() { 17 | stereo.on(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/TurkeyAdapter.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class TurkeyAdapter implements Duck {//让火鸡看起来像鸭子 4 | Turkey turkey; 5 | 6 | public TurkeyAdapter(Turkey turkey) { 7 | this.turkey = turkey; 8 | } 9 | 10 | public void quack() { 11 | turkey.gobble(); 12 | } 13 | 14 | public void fly() { 15 | for(int i=0; i < 5; i++) {//鸭子可以长时间地飞行,而火鸡飞行时间较短 16 | turkey.fly(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Strategy/src/test/java/com/crow/MiniDuckSimulator1.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MiniDuckSimulator1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Duck mallard = new MallardDuck(); 8 | mallard.performQuack(); 9 | mallard.performFly(); 10 | 11 | Duck model = new ModelDuck(); 12 | model.performFly(); 13 | model.setFlyBehavior(new FlyRocketPowered()); 14 | model.performFly(); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/HottubOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Hottub; 4 | 5 | public class HottubOffCommand implements Command { 6 | Hottub hottub; 7 | 8 | public HottubOffCommand(Hottub hottub) { 9 | this.hottub = hottub; 10 | } 11 | 12 | public void execute() { 13 | hottub.setTemperature(98); 14 | hottub.off(); 15 | } 16 | public void undo() { 17 | hottub.on(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public abstract class PizzaStore { 4 | 5 | abstract Pizza createPizza(String item); 6 | 7 | public Pizza orderPizza(String type) { 8 | Pizza pizza = createPizza(type); 9 | System.out.println("--- Making a " + pizza.getName() + " ---"); 10 | pizza.prepare(); 11 | pizza.bake(); 12 | pizza.cut(); 13 | pizza.box(); 14 | return pizza; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/HottubOnCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Hottub; 4 | 5 | public class HottubOnCommand implements Command { 6 | Hottub hottub; 7 | 8 | public HottubOnCommand(Hottub hottub) { 9 | this.hottub = hottub; 10 | } 11 | public void execute() { 12 | hottub.on(); 13 | hottub.setTemperature(104); 14 | hottub.circulate(); 15 | } 16 | public void undo() { 17 | hottub.off(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/PizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/StereoOnWithCDCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.Stereo; 4 | 5 | public class StereoOnWithCDCommand implements Command { 6 | Stereo stereo; 7 | 8 | public StereoOnWithCDCommand(Stereo stereo) { 9 | this.stereo = stereo; 10 | } 11 | 12 | public void execute() { 13 | stereo.on(); 14 | stereo.setCD(); 15 | stereo.setVolume(11); 16 | } 17 | 18 | public void undo() { 19 | stereo.off(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/NYStyleVeggiePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class NYStyleVeggiePizza extends Pizza { 4 | 5 | public NYStyleVeggiePizza() { 6 | name = "NY Style Veggie Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | toppings.add("Garlic"); 12 | toppings.add("Onion"); 13 | toppings.add("Mushrooms"); 14 | toppings.add("Red Pepper"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Adapter/src/main/java/com/crow/DuckAdapter.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | import java.util.Random; 3 | 4 | public class DuckAdapter implements Turkey {//让鸭子看起来像火鸡 5 | Duck duck; 6 | Random rand; 7 | 8 | public DuckAdapter(Duck duck) { 9 | this.duck = duck; 10 | rand = new Random(); 11 | } 12 | 13 | public void gobble() { 14 | duck.quack(); 15 | } 16 | 17 | public void fly() { 18 | if (rand.nextInt(5) == 0) {//鸭子可以长时间地飞行,而火鸡飞行时间较短 19 | duck.fly(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/CaffeineBeverage.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public abstract class CaffeineBeverage { 4 | 5 | final void prepareRecipe() { 6 | boilWater(); 7 | brew(); 8 | pourInCup(); 9 | addCondiments(); 10 | } 11 | 12 | abstract void brew(); 13 | 14 | abstract void addCondiments(); 15 | 16 | void boilWater() { 17 | System.out.println("Boiling water"); 18 | } 19 | 20 | void pourInCup() { 21 | System.out.println("Pouring into cup"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/Screen.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Screen { 4 | String description; 5 | 6 | public Screen(String description) { 7 | this.description = description; 8 | } 9 | 10 | public void up() { 11 | System.out.println(description + " going up"); 12 | } 13 | 14 | public void down() { 15 | System.out.println(description + " going down"); 16 | } 17 | 18 | 19 | public String toString() { 20 | return description; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/ChicagoStyleCheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class ChicagoStyleCheesePizza extends Pizza { 4 | 5 | public ChicagoStyleCheesePizza() { 6 | name = "Chicago Style Deep Dish Cheese Pizza"; 7 | dough = "Extra Thick Crust Dough"; 8 | sauce = "Plum Tomato Sauce"; 9 | 10 | toppings.add("Shredded Mozzarella Cheese"); 11 | } 12 | 13 | void cut() { 14 | System.out.println("Cutting the pizza into square slices"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.idea/libraries/Maven__junit_junit_3_8_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/ChicagoStyleClamPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class ChicagoStyleClamPizza extends Pizza { 4 | public ChicagoStyleClamPizza() { 5 | name = "Chicago Style Clam Pizza"; 6 | dough = "Extra Thick Crust Dough"; 7 | sauce = "Plum Tomato Sauce"; 8 | 9 | toppings.add("Shredded Mozzarella Cheese"); 10 | toppings.add("Frozen Clams from Chesapeake Bay"); 11 | } 12 | 13 | void cut() { 14 | System.out.println("Cutting the pizza into square slices"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Receiver/TV.java: -------------------------------------------------------------------------------- 1 | package com.crow.Receiver; 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 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/NYStylePepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class NYStylePepperoniPizza extends Pizza { 4 | 5 | public NYStylePepperoniPizza() { 6 | name = "NY Style Pepperoni Pizza"; 7 | dough = "Thin Crust Dough"; 8 | sauce = "Marinara Sauce"; 9 | 10 | toppings.add("Grated Reggiano Cheese"); 11 | toppings.add("Sliced Pepperoni"); 12 | toppings.add("Garlic"); 13 | toppings.add("Onion"); 14 | toppings.add("Mushrooms"); 15 | toppings.add("Red Pepper"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Singleton/src/main/java/com/crow/DCL.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | // 4 | // Danger! This implementation of Singleton not 5 | // guaranteed to work prior to Java 5 6 | // 7 | 8 | public class DCL { 9 | private volatile static DCL uniqueInstance; 10 | 11 | private DCL() {} 12 | 13 | public static DCL getInstance() { 14 | if (uniqueInstance == null) { 15 | synchronized (DCL.class) { 16 | if (uniqueInstance == null) { 17 | uniqueInstance = new DCL(); 18 | } 19 | } 20 | } 21 | return uniqueInstance; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/CheesePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/NYPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class NYPizzaStore extends PizzaStore { 4 | 5 | Pizza createPizza(String item) { 6 | if (item.equals("cheese")) { 7 | return new NYStyleCheesePizza(); 8 | } else if (item.equals("veggie")) { 9 | return new NYStyleVeggiePizza(); 10 | } else if (item.equals("clam")) { 11 | return new NYStyleClamPizza(); 12 | } else if (item.equals("pepperoni")) { 13 | return new NYStylePepperoniPizza(); 14 | } else return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Decorator/Soy.java: -------------------------------------------------------------------------------- 1 | package com.crow.Decorator; 2 | 3 | import com.crow.Beverages.Beverage; 4 | 5 | /** 6 | * Created by CrowHawk on 17/7/10. 7 | */ 8 | public class Soy extends CondimentDecorator { 9 | Beverage beverage; 10 | 11 | public Soy(Beverage beverage) { 12 | this.beverage = beverage; 13 | } 14 | 15 | public String getDescription() { 16 | return beverage.getDescription() + ",Soy"; 17 | } 18 | 19 | public double cost() { 20 | return .25 + beverage.cost(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Observer/src/test/java/com/crow/WeatherSationTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | /** 4 | * Created by CrowHawk on 17/7/9. 5 | */ 6 | public class WeatherSationTest { 7 | public static void main(String[] args) { 8 | WeatherData weatherData = new WeatherData(); 9 | CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); 10 | weatherData.setMeasurements(80, 65, 30.4f); 11 | weatherData.setMeasurements(82, 70, 29.2f); 12 | weatherData.setMeasurements(78, 90, 29.2f); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Decorator/Whip.java: -------------------------------------------------------------------------------- 1 | package com.crow.Decorator; 2 | 3 | import com.crow.Beverages.Beverage; 4 | 5 | /** 6 | * Created by CrowHawk on 17/7/10. 7 | */ 8 | public class Whip extends CondimentDecorator { 9 | Beverage beverage; 10 | 11 | public Whip(Beverage beverage) { 12 | this.beverage = beverage; 13 | } 14 | 15 | public String getDescription() { 16 | return beverage.getDescription() + ",Whip"; 17 | } 18 | 19 | public double cost() { 20 | return .18 + beverage.cost(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Observer/Observer.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Strategy/src/test/java/com/crow/MiniDuckSimulator.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MiniDuckSimulator { 4 | 5 | public static void main(String[] args) { 6 | 7 | MallardDuck mallard = new MallardDuck(); 8 | RubberDuck rubberDuckie = new RubberDuck(); 9 | DecoyDuck decoy = new DecoyDuck(); 10 | 11 | ModelDuck model = new ModelDuck(); 12 | 13 | mallard.performQuack(); 14 | rubberDuckie.performQuack(); 15 | decoy.performQuack(); 16 | 17 | model.performFly(); 18 | model.setFlyBehavior(new FlyRocketPowered()); 19 | model.performFly(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ClamPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/ChicagoStyleVeggiePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class ChicagoStyleVeggiePizza extends Pizza { 4 | public ChicagoStyleVeggiePizza() { 5 | name = "Chicago Deep Dish Veggie Pizza"; 6 | dough = "Extra Thick Crust Dough"; 7 | sauce = "Plum Tomato Sauce"; 8 | 9 | toppings.add("Shredded Mozzarella Cheese"); 10 | toppings.add("Black Olives"); 11 | toppings.add("Spinach"); 12 | toppings.add("Eggplant"); 13 | } 14 | 15 | void cut() { 16 | System.out.println("Cutting the pizza into square slices"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/VeggiePizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/PopcornPopper.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class PopcornPopper { 4 | String description; 5 | 6 | public PopcornPopper(String description) { 7 | this.description = description; 8 | } 9 | 10 | public void on() { 11 | System.out.println(description + " on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(description + " off"); 16 | } 17 | 18 | public void pop() { 19 | System.out.println(description + " popping popcorn!"); 20 | } 21 | 22 | 23 | public String toString() { 24 | return description; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Adapter/Adapter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/CaffeineBeverageWithHook.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public abstract class CaffeineBeverageWithHook { 4 | 5 | void prepareRecipe() { 6 | boilWater(); 7 | brew(); 8 | pourInCup(); 9 | if (customerWantsCondiments()) { 10 | addCondiments(); 11 | } 12 | } 13 | 14 | abstract void brew(); 15 | 16 | abstract void 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 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/TheaterLights.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class TheaterLights { 4 | String description; 5 | 6 | public TheaterLights(String description) { 7 | this.description = description; 8 | } 9 | 10 | public void on() { 11 | System.out.println(description + " on"); 12 | } 13 | 14 | public void off() { 15 | System.out.println(description + " off"); 16 | } 17 | 18 | public void dim(int level) { 19 | System.out.println(description + " dimming to " + level + "%"); 20 | } 21 | 22 | public String toString() { 23 | return description; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Template/Template.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/Decorator/Mocha.java: -------------------------------------------------------------------------------- 1 | package com.crow.Decorator; 2 | 3 | import com.crow.Beverages.Beverage; 4 | 5 | /** 6 | * Created by CrowHawk on 17/7/10. 7 | */ 8 | public class Mocha extends CondimentDecorator {//Mocha是一个装饰者 9 | Beverage beverage;//用实例变量记录饮料,也就是被装饰者 10 | 11 | public Mocha(Beverage beverage) {//将饮料当作构造器参数,使被装饰者记录到实例变量中 12 | this.beverage = beverage; 13 | } 14 | 15 | public String getDescription() { 16 | return beverage.getDescription() + ",Mocha"; 17 | } 18 | 19 | public double cost() {//计算带Mocha饮料的价钱 20 | return .20 + beverage.cost(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/ChicagoStylePepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class ChicagoStylePepperoniPizza extends Pizza { 4 | public ChicagoStylePepperoniPizza() { 5 | name = "Chicago Style Pepperoni Pizza"; 6 | dough = "Extra Thick Crust Dough"; 7 | sauce = "Plum Tomato Sauce"; 8 | 9 | toppings.add("Shredded Mozzarella Cheese"); 10 | toppings.add("Black Olives"); 11 | toppings.add("Spinach"); 12 | toppings.add("Eggplant"); 13 | toppings.add("Sliced Pepperoni"); 14 | } 15 | 16 | void cut() { 17 | System.out.println("Cutting the pizza into square slices"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/MacroCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 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 proper undo functionality 18 | */ 19 | public void undo() { 20 | for (int i = commands.length -1; i >= 0; i--) { 21 | commands[i].undo(); 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/GumballMonitor.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.rmi.*; 4 | 5 | public class GumballMonitor { 6 | GumballMachineRemote machine; 7 | 8 | public GumballMonitor(GumballMachineRemote machine) { 9 | this.machine = machine; 10 | } 11 | 12 | public void report() { 13 | try { 14 | System.out.println("Gumball Machine: " + machine.getLocation()); 15 | System.out.println("Current inventory: " + machine.getCount() + " gumballs"); 16 | System.out.println("Current state: " + machine.getState()); 17 | } catch (RemoteException e) { 18 | e.printStackTrace(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/ChicagoPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class ChicagoPizzaStore extends PizzaStore { 4 | 5 | Pizza createPizza(String item) { 6 | if (item.equals("cheese")) { 7 | return new ChicagoStyleCheesePizza(); 8 | } else if (item.equals("veggie")) { 9 | return new ChicagoStyleVeggiePizza(); 10 | } else if (item.equals("clam")) { 11 | return new ChicagoStyleClamPizza(); 12 | } else if (item.equals("pepperoni")) { 13 | return new ChicagoStylePepperoniPizza(); 14 | } else return null; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Adapter/src/test/java/com/crow/DuckTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class DuckTest { 4 | public static void main(String[] args) { 5 | MallardDuck duck = new MallardDuck(); 6 | 7 | WildTurkey turkey = new WildTurkey(); 8 | Duck turkeyAdapter = new TurkeyAdapter(turkey); 9 | 10 | System.out.println("The Turkey says..."); 11 | turkey.gobble(); 12 | turkey.fly(); 13 | 14 | System.out.println("\nThe Duck says..."); 15 | testDuck(duck); 16 | 17 | System.out.println("\nThe TurkeyAdapter says..."); 18 | testDuck(turkeyAdapter); 19 | } 20 | 21 | static void testDuck(Duck duck) { 22 | duck.quack(); 23 | duck.fly(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/PepperoniPizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Strategy/src/main/java/com/crow/Duck.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public abstract class Duck { 4 | FlyBehavior flyBehavior; 5 | QuackBehavior quackBehavior; 6 | 7 | public Duck() { 8 | } 9 | 10 | public void setFlyBehavior (FlyBehavior fb) { 11 | flyBehavior = fb; 12 | } 13 | 14 | public void setQuackBehavior(QuackBehavior qb) { 15 | quackBehavior = qb; 16 | } 17 | 18 | abstract void display(); 19 | 20 | public void performFly() { 21 | flyBehavior.fly(); 22 | } 23 | 24 | public void performQuack() { 25 | quackBehavior.quack(); 26 | } 27 | 28 | public void swim() { 29 | System.out.println("All ducks float, even decoys!"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Template/src/test/java/com/crow/BeverageTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class BeverageTestDrive { 4 | public static void main(String[] args) { 5 | 6 | Tea tea = new Tea(); 7 | Coffee coffee = new Coffee(); 8 | 9 | System.out.println("\nMaking tea..."); 10 | tea.prepareRecipe(); 11 | 12 | System.out.println("\nMaking coffee..."); 13 | coffee.prepareRecipe(); 14 | 15 | 16 | TeaWithHook teaHook = new TeaWithHook(); 17 | CoffeeWithHook coffeeHook = new CoffeeWithHook(); 18 | 19 | System.out.println("\nMaking tea..."); 20 | teaHook.prepareRecipe(); 21 | 22 | System.out.println("\nMaking coffee..."); 23 | coffeeHook.prepareRecipe(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Receiver/Light.java: -------------------------------------------------------------------------------- 1 | package com.crow.Receiver; 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 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/GumballMachineTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | import java.rmi.*; 3 | 4 | public class GumballMachineTestDrive { 5 | 6 | public static void main(String[] args) { 7 | GumballMachineRemote gumballMachine = null; 8 | int count; 9 | 10 | if (args.length < 2) { 11 | System.out.println("GumballMachine "); 12 | System.exit(1); 13 | } 14 | 15 | try { 16 | count = Integer.parseInt(args[1]); 17 | 18 | gumballMachine = 19 | new GumballMachine(args[0], count); 20 | Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine); 21 | } catch (Exception e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/CeilingFanOffCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.CeilingFan; 4 | 5 | public class CeilingFanOffCommand implements Command { 6 | CeilingFan ceilingFan; 7 | int prevSpeed; 8 | 9 | public CeilingFanOffCommand(CeilingFan ceilingFan) { 10 | this.ceilingFan = ceilingFan; 11 | } 12 | public void execute() { 13 | prevSpeed = ceilingFan.getSpeed(); 14 | ceilingFan.off(); 15 | } 16 | public void undo() { 17 | switch (prevSpeed) { 18 | case CeilingFan.HIGH: ceilingFan.high(); break; 19 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 20 | case CeilingFan.LOW: ceilingFan.low(); break; 21 | default: ceilingFan.off(); break; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/CeilingFanHighCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.CeilingFan; 4 | 5 | public class CeilingFanHighCommand implements Command { 6 | CeilingFan ceilingFan; 7 | int prevSpeed; 8 | 9 | public CeilingFanHighCommand(CeilingFan ceilingFan) { 10 | this.ceilingFan = ceilingFan; 11 | } 12 | public void execute() { 13 | prevSpeed = ceilingFan.getSpeed(); 14 | ceilingFan.high(); 15 | } 16 | public void undo() { 17 | switch (prevSpeed) { 18 | case CeilingFan.HIGH: ceilingFan.high(); break; 19 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 20 | case CeilingFan.LOW: ceilingFan.low(); break; 21 | default: ceilingFan.off(); break; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DesignPattern.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Command/CeilingFanMediumCommand.java: -------------------------------------------------------------------------------- 1 | package com.crow.Command; 2 | 3 | import com.crow.Receiver.CeilingFan; 4 | 5 | public class CeilingFanMediumCommand implements Command { 6 | CeilingFan ceilingFan; 7 | int prevSpeed; 8 | 9 | public CeilingFanMediumCommand(CeilingFan ceilingFan) { 10 | this.ceilingFan = ceilingFan; 11 | } 12 | public void execute() { 13 | prevSpeed = ceilingFan.getSpeed(); 14 | ceilingFan.medium(); 15 | } 16 | public void undo() { 17 | switch (prevSpeed) { 18 | case CeilingFan.HIGH: ceilingFan.high(); break; 19 | case CeilingFan.MEDIUM: ceilingFan.medium(); break; 20 | case CeilingFan.LOW: ceilingFan.low(); break; 21 | default: ceilingFan.off(); break; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/Waitress.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 = 22 | (MenuComponent)iterator.next(); 23 | try { 24 | if (menuComponent.isVegetarian()) { 25 | menuComponent.print(); 26 | } 27 | } catch (UnsupportedOperationException e) {} 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/NYPizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MenuItem { 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 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/Projector.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Projector { 4 | String description; 5 | DvdPlayer dvdPlayer; 6 | 7 | public Projector(String description, DvdPlayer dvdPlayer) { 8 | this.description = description; 9 | this.dvdPlayer = dvdPlayer; 10 | } 11 | 12 | public void on() { 13 | System.out.println(description + " on"); 14 | } 15 | 16 | public void off() { 17 | System.out.println(description + " off"); 18 | } 19 | 20 | public void wideScreenMode() { 21 | System.out.println(description + " in widescreen mode (16x9 aspect ratio)"); 22 | } 23 | 24 | public void tvMode() { 25 | System.out.println(description + " in tv mode (4x3 aspect ratio)"); 26 | } 27 | 28 | public String toString() { 29 | return description; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/SoldOutState.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 String toString() { 27 | return "sold out"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Decorator/src/test/java/com/crow/InputTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import com.crow.JavaIO.LowerCaseInputStream; 4 | 5 | import java.io.BufferedInputStream; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | 10 | /** 11 | * Created by CrowHawk on 17/7/10. 12 | */ 13 | public class InputTest { 14 | public static void main(String[] args) throws IOException { 15 | int c; 16 | try { 17 | InputStream in = new LowerCaseInputStream(new BufferedInputStream(new FileInputStream("test.txt"))); 18 | 19 | while((c = in.read()) >= 0) { 20 | System.out.print((char)c); 21 | } 22 | 23 | in.close(); 24 | }catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ChicagoPizzaIngredientFactory.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/SoldOutState.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | public class SoldOutState implements State { 4 | transient 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 String toString() { 27 | return "sold out"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/NoQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 String toString() { 28 | return "waiting for quarter"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/NoQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | public class NoQuarterState implements State { 4 | transient 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 String toString() { 28 | return "waiting for quarter"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/AlternatingDinerMenuIterator.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | import java.util.Calendar; 5 | 6 | public class AlternatingDinerMenuIterator implements Iterator { 7 | MenuItem[] items; 8 | int position; 9 | 10 | public AlternatingDinerMenuIterator(MenuItem[] items) { 11 | this.items = items; 12 | Calendar rightNow = Calendar.getInstance(); 13 | position = rightNow.DAY_OF_WEEK % 2; 14 | } 15 | public Object next() { 16 | MenuItem menuItem = items[position]; 17 | position = position + 2; 18 | return menuItem; 19 | } 20 | public boolean hasNext() { 21 | if (position >= items.length || items[position] == null) { 22 | return false; 23 | } else { 24 | return true; 25 | } 26 | } 27 | public void remove() { 28 | throw new UnsupportedOperationException( 29 | "Alternating Diner Menu Iterator does not support remove()"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Command/Command.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Facade/Facade.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Facade/src/test/java/com/crow/HomeTheaterTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class HomeTheaterTestDrive { 4 | public static void main(String[] args) { 5 | Amplifier amp = new Amplifier("Top-O-Line Amplifier"); 6 | Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp); 7 | DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", amp); 8 | CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp); 9 | Projector projector = new Projector("Top-O-Line Projector", dvd); 10 | TheaterLights lights = new TheaterLights("Theater Ceiling Lights"); 11 | Screen screen = new Screen("Theater Screen"); 12 | PopcornPopper popper = new PopcornPopper("Popcorn Popper"); 13 | 14 | HomeTheaterFacade homeTheater = 15 | new HomeTheaterFacade(amp, tuner, dvd, cd, 16 | projector, screen, lights, popper); 17 | 18 | homeTheater.watchMovie("Raiders of the Lost Ark"); 19 | homeTheater.endMovie(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Factory/Factory.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Proxy/Proxy.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/dynamicproxy/OwnerInvocationHandler.java: -------------------------------------------------------------------------------- 1 | package com.crow.dynamicproxy; 2 | 3 | import java.lang.reflect.*; 4 | 5 | public class OwnerInvocationHandler implements InvocationHandler { 6 | PersonBean person; 7 | 8 | public OwnerInvocationHandler(PersonBean person) { 9 | this.person = person; 10 | } 11 | 12 | public Object invoke(Object proxy, Method method, Object[] args) 13 | throws IllegalAccessException { 14 | 15 | try { 16 | if (method.getName().startsWith("get")) { 17 | return method.invoke(person, args); 18 | } else if (method.getName().equals("setHotOrNotRating")) { 19 | throw new IllegalAccessException(); 20 | } else if (method.getName().startsWith("set")) { 21 | return method.invoke(person, args); 22 | } 23 | } catch (InvocationTargetException e) { 24 | e.printStackTrace(); 25 | } 26 | return null; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /State/State.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Composite/Composite.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Decorator/Decorator.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Iterator/Iterator.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Singleton/Singleton.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Strategy/Strategy.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/Tuner.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Tuner { 4 | String description; 5 | Amplifier amplifier; 6 | double frequency; 7 | 8 | public Tuner(String description, Amplifier amplifier) { 9 | this.description = description; 10 | } 11 | 12 | public void on() { 13 | System.out.println(description + " on"); 14 | } 15 | 16 | public void off() { 17 | System.out.println(description + " off"); 18 | } 19 | 20 | public void setFrequency(double frequency) { 21 | System.out.println(description + " setting frequency to " + frequency); 22 | this.frequency = frequency; 23 | } 24 | 25 | public void setAm() { 26 | System.out.println(description + " setting AM mode"); 27 | } 28 | 29 | public void setFm() { 30 | System.out.println(description + " setting FM mode"); 31 | } 32 | 33 | public String toString() { 34 | return description; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/dynamicproxy/NonOwnerInvocationHandler.java: -------------------------------------------------------------------------------- 1 | package com.crow.dynamicproxy; 2 | 3 | import java.lang.reflect.*; 4 | 5 | public class NonOwnerInvocationHandler implements InvocationHandler { 6 | PersonBean person; 7 | 8 | public NonOwnerInvocationHandler(PersonBean person) { 9 | this.person = person; 10 | } 11 | 12 | public Object invoke(Object proxy, Method method, Object[] args) 13 | throws IllegalAccessException { 14 | 15 | try { 16 | if (method.getName().startsWith("get")) { 17 | return method.invoke(person, args); 18 | } else if (method.getName().equals("setHotOrNotRating")) { 19 | return method.invoke(person, args); 20 | } else if (method.getName().startsWith("set")) { 21 | throw new IllegalAccessException(); 22 | } 23 | } catch (InvocationTargetException e) { 24 | e.printStackTrace(); 25 | } 26 | return null; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Decorator/src/main/java/com/crow/JavaIO/LowerCaseInputStream.java: -------------------------------------------------------------------------------- 1 | package com.crow.JavaIO; 2 | 3 | import java.io.FilterInputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | 7 | /** 8 | * Created by CrowHawk on 17/7/10. 9 | */ 10 | public class LowerCaseInputStream extends FilterInputStream {//JavaI/O设计时使用了装饰者模式,编写自己的JavaI/O装饰者,把输入流的所有大写字符换成小写 11 | public LowerCaseInputStream(InputStream in) { 12 | super(in); 13 | } 14 | 15 | public int read() throws IOException { 16 | int c = super.read(); 17 | return (c == -1 ? c : Character.toLowerCase((char)c)); 18 | } 19 | 20 | public int read(byte[] b, int offset, int len) throws IOException { 21 | int result = super.read(b, offset, len); 22 | for (int i = offset; i < offset + result; i++) { 23 | b[i] = (byte)Character.toLowerCase((char)b[i]); 24 | } 25 | return result; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Receiver/Stereo.java: -------------------------------------------------------------------------------- 1 | package com.crow.Receiver; 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 | -------------------------------------------------------------------------------- /Observer/src/main/java/com/crow/WeatherData.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Observable; 4 | 5 | /** 6 | * Created by CrowHawk on 17/7/9. 7 | */ 8 | public class WeatherData extends Observable { 9 | private float temperature; 10 | private float humidity; 11 | private float pressure; 12 | 13 | public WeatherData() {} 14 | 15 | public void measurementsChanged() { 16 | setChanged(); 17 | notifyObservers(); 18 | } 19 | 20 | public void setMeasurements (float temperature, float humidity, float pressure) { 21 | this.temperature = temperature; 22 | this.humidity = humidity; 23 | this.pressure = pressure; 24 | measurementsChanged(); 25 | } 26 | 27 | public float getTemperature() { 28 | return temperature; 29 | } 30 | 31 | public float getHumidity() { 32 | return humidity; 33 | } 34 | 35 | public float getPressure() { 36 | return pressure; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Iterator/src/test/java/com/crow/MenuTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class MenuTestDrive { 4 | public static void main(String args[]) { 5 | PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); 6 | DinerMenu dinerMenu = new DinerMenu(); 7 | CafeMenu cafeMenu = new CafeMenu(); 8 | 9 | Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu); 10 | 11 | waitress.printMenu(); 12 | waitress.printVegetarianMenu(); 13 | 14 | System.out.println("\nCustomer asks, is the Hotdog vegetarian?"); 15 | System.out.print("Waitress says: "); 16 | if (waitress.isItemVegetarian("Hotdog")) { 17 | System.out.println("Yes"); 18 | } else { 19 | System.out.println("No"); 20 | } 21 | System.out.println("\nCustomer asks, are the Waffles vegetarian?"); 22 | System.out.print("Waitress says: "); 23 | if (waitress.isItemVegetarian("Waffles")) { 24 | System.out.println("Yes"); 25 | } else { 26 | System.out.println("No"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/MenuComponent.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/dynamicproxy/PersonBeanImpl.java: -------------------------------------------------------------------------------- 1 | package com.crow.dynamicproxy; 2 | 3 | public class PersonBeanImpl implements PersonBean { 4 | String name; 5 | String gender; 6 | String interests; 7 | int rating; 8 | int ratingCount = 0; 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public String getGender() { 15 | return gender; 16 | } 17 | 18 | public String getInterests() { 19 | return interests; 20 | } 21 | 22 | public int getHotOrNotRating() { 23 | if (ratingCount == 0) return 0; 24 | return (rating/ratingCount); 25 | } 26 | 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public void setGender(String gender) { 33 | this.gender = gender; 34 | } 35 | 36 | public void setInterests(String interests) { 37 | this.interests = interests; 38 | } 39 | 40 | public void setHotOrNotRating(int rating) { 41 | this.rating += rating; 42 | ratingCount++; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Receiver/Hottub.java: -------------------------------------------------------------------------------- 1 | package com.crow.Receiver; 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 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/ChicagoPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/NYPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/DinerMenuIterator.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | 5 | public class DinerMenuIterator implements Iterator { 6 | MenuItem[] list; 7 | int position = 0; 8 | 9 | public DinerMenuIterator(MenuItem[] list) { 10 | this.list = list; 11 | } 12 | 13 | public Object next() { 14 | MenuItem menuItem = list[position]; 15 | position = position + 1; 16 | return menuItem; 17 | } 18 | 19 | public boolean hasNext() { 20 | if (position >= list.length || list[position] == null) { 21 | return false; 22 | } else { 23 | return true; 24 | } 25 | } 26 | 27 | public void remove() { 28 | if (position <= 0) { 29 | throw new IllegalStateException 30 | ("You can't remove an item until you've done at least one next()"); 31 | } 32 | if (list[position-1] != null) { 33 | for (int i = position-1; i < (list.length-1); i++) { 34 | list[i] = list[i+1]; 35 | } 36 | list[list.length-1] = null; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Proxy/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Proxy 11 | jar 12 | 13 | Proxy 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /State/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | State 11 | jar 12 | 13 | State 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Adapter/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Adapter 11 | jar 12 | 13 | Adapter 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Command/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Command 11 | jar 12 | 13 | Command 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Facade/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Facade 11 | jar 12 | 13 | Facade 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Factory/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Factory 11 | jar 12 | 13 | Factory 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Composite/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Composite 11 | jar 12 | 13 | Composite 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Decorator/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Decorator 11 | jar 12 | 13 | Decorator 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Iterator/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Iterator 11 | jar 12 | 13 | Iterator 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Observer/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Observer 11 | jar 12 | 13 | Observer 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Singleton/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Singleton 11 | jar 12 | 13 | Singleton 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Strategy/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Strategy 11 | jar 12 | 13 | Strategy 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Template/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | DesignPattern 5 | com.crow 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | Template 11 | jar 12 | 13 | Template 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | junit 23 | junit 24 | 3.8.1 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Decorator/src/test/java/com/crow/CoffeeTest.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import com.crow.Beverages.Beverage; 4 | import com.crow.Beverages.Espresso; 5 | import com.crow.Beverages.HouseBlend; 6 | import com.crow.Decorator.Mocha; 7 | import com.crow.Decorator.Soy; 8 | import com.crow.Decorator.Whip; 9 | 10 | /** 11 | * Created by CrowHawk on 17/7/10. 12 | */ 13 | public class CoffeeTest { 14 | public static void main(String args[]) {//计算不同饮料加不同调料的价格 15 | Beverage beverage = new Espresso(); 16 | beverage = new Mocha(beverage); 17 | beverage = new Mocha(beverage);//Espresso里加双份Mocha 18 | beverage = new Whip(beverage);//再加Whip 19 | System.out.println(beverage.getDescription() + " $" + beverage.cost()); 20 | 21 | Beverage beverage2 = new HouseBlend(); 22 | beverage2 = new Soy(beverage2); 23 | beverage2 = new Mocha(beverage2); 24 | beverage2 = new Whip(beverage2); 25 | System.out.println(beverage2.getDescription() + " $" + beverage2.cost()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/CafeMenu.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.*; 4 | 5 | public class CafeMenu implements Menu { 6 | Hashtable menuItems = new Hashtable(); 7 | 8 | public CafeMenu() { 9 | addItem("Veggie Burger and Air Fries", 10 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 11 | true, 3.99); 12 | addItem("Soup of the day", 13 | "A cup of the soup of the day, with a side salad", 14 | false, 3.69); 15 | addItem("Burrito", 16 | "A large burrito, with whole pinto beans, salsa, guacamole", 17 | true, 4.29); 18 | } 19 | 20 | public void addItem(String name, String description, 21 | boolean vegetarian, double price) 22 | { 23 | MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 24 | menuItems.put(menuItem.getName(), menuItem); 25 | } 26 | 27 | public Hashtable getItems() { 28 | return menuItems; 29 | } 30 | 31 | public Iterator createIterator() { 32 | return menuItems.values().iterator(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Observer/src/main/java/com/crow/CurrentConditionsDisplay.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Observable; 4 | import java.util.Observer; 5 | 6 | /** 7 | * Created by CrowHawk on 17/7/9. 8 | */ 9 | public class CurrentConditionsDisplay implements Observer,DisplayElement{ 10 | Observable observable; 11 | private float temperature; 12 | private float humidity; 13 | 14 | public CurrentConditionsDisplay(Observable observable) { 15 | this.observable = observable; 16 | observable.addObserver(this); 17 | } 18 | 19 | public void update(Observable obs, Object arg) { 20 | if(obs instanceof WeatherData) { 21 | WeatherData weatherData = (WeatherData)obs; 22 | this.temperature = weatherData.getTemperature(); 23 | this.humidity = weatherData.getHumidity(); 24 | display(); 25 | } 26 | } 27 | 28 | public void display() { 29 | System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/SoldState.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 String toString() { 33 | return "dispensing a gumball"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/TeaWithHook.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.io.*; 4 | 5 | public class TeaWithHook extends CaffeineBeverageWithHook { 6 | 7 | public void brew() { 8 | System.out.println("Steeping the tea"); 9 | } 10 | 11 | public void addCondiments() { 12 | System.out.println("Adding Lemon"); 13 | } 14 | 15 | public boolean customerWantsCondiments() { 16 | 17 | String answer = getUserInput(); 18 | 19 | if (answer.toLowerCase().startsWith("y")) { 20 | return true; 21 | } else { 22 | return false; 23 | } 24 | } 25 | 26 | private String getUserInput() { 27 | // get the user's response 28 | String answer = null; 29 | 30 | System.out.print("Would you like lemon with your tea (y/n)? "); 31 | 32 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 33 | try { 34 | answer = in.readLine(); 35 | } catch (IOException ioe) { 36 | System.err.println("IO error trying to read your answer"); 37 | } 38 | if (answer == null) { 39 | return "no"; 40 | } 41 | return answer; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Template/src/main/java/com/crow/CoffeeWithHook.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.io.*; 4 | 5 | public class CoffeeWithHook extends CaffeineBeverageWithHook { 6 | 7 | public void brew() { 8 | System.out.println("Dripping Coffee through filter"); 9 | } 10 | 11 | public void addCondiments() { 12 | System.out.println("Adding Sugar and Milk"); 13 | } 14 | 15 | public boolean customerWantsCondiments() { 16 | 17 | String answer = getUserInput(); 18 | 19 | if (answer.toLowerCase().startsWith("y")) { 20 | return true; 21 | } else { 22 | return false; 23 | } 24 | } 25 | 26 | private String getUserInput() { 27 | String answer = null; 28 | 29 | System.out.print("Would you like milk and sugar with your coffee (y/n)? "); 30 | 31 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 32 | try { 33 | answer = in.readLine(); 34 | } catch (IOException ioe) { 35 | System.err.println("IO error trying to read your answer"); 36 | } 37 | if (answer == null) { 38 | return "no"; 39 | } 40 | return answer; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/SoldState.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | public class SoldState implements State { 4 | transient 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 String toString() { 33 | return "dispensing a gumball"; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/CompositeIterator.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | 4 | import java.util.*; 5 | 6 | public class CompositeIterator implements Iterator { 7 | Stack stack = new Stack(); 8 | 9 | public CompositeIterator(Iterator iterator) { 10 | stack.push(iterator); 11 | } 12 | 13 | public Object next() { 14 | if(hasNext()) { 15 | Iterator iterator = (Iterator) stack.peek(); 16 | MenuComponent menuComponent = (MenuComponent) iterator.next(); 17 | if(menuComponent instanceof Menu) {//如果元素是一个菜单,则有了另一个需要被包含进遍历中的组合,把它也压入栈中 18 | stack.push(menuComponent.createIterator()); 19 | } 20 | return menuComponent; 21 | } 22 | else { 23 | return null; 24 | } 25 | } 26 | 27 | public boolean hasNext() { 28 | if(stack.isEmpty()) { 29 | return false; 30 | } 31 | else { 32 | if(((Iterator)stack.peek()).hasNext()) { 33 | return true; 34 | } 35 | else { 36 | stack.pop(); 37 | return hasNext(); 38 | } 39 | } 40 | } 41 | 42 | public void remove() { 43 | throw new UnsupportedOperationException(); 44 | } 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Receiver/CeilingFan.java: -------------------------------------------------------------------------------- 1 | package com.crow.Receiver; 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 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/HasQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.util.Random; 4 | 5 | public class HasQuarterState implements State { 6 | Random randomWinner = new Random(System.currentTimeMillis()); 7 | transient 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) { 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 String toString() { 37 | return "waiting for turn of crank"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/HasQuarterState.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 String toString() { 37 | return "waiting for turn of crank"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Proxy/src/test/java/com/crow/remoteproxy/GumballMonitorTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.rmi.*; 4 | 5 | public class GumballMonitorTestDrive { 6 | 7 | public static void main(String[] args) { 8 | String[] location = {"rmi://santafe.mightygumball.com/gumballmachine", 9 | "rmi://boulder.mightygumball.com/gumballmachine", 10 | "rmi://seattle.mightygumball.com/gumballmachine"}; 11 | 12 | if (args.length >= 0) 13 | { 14 | location = new String[1]; 15 | location[0] = "rmi://" + args[0] + "/gumballmachine"; 16 | } 17 | 18 | GumballMonitor[] monitor = new GumballMonitor[location.length]; 19 | 20 | 21 | 22 | for (int i=0;i < location.length; i++) { 23 | try { 24 | GumballMachineRemote machine = 25 | (GumballMachineRemote) Naming.lookup(location[i]); 26 | monitor[i] = new GumballMonitor(machine); 27 | System.out.println(monitor[i]); 28 | } catch (Exception e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | for(int i=0; i < monitor.length; i++) { 34 | monitor[i].report(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Factory/src/test/java/com/crow/AbstractFactory/PizzaTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 2 | 3 | public class PizzaTestDrive { 4 | 5 | public static void main(String[] args) { 6 | PizzaStore nyStore = new NYPizzaStore(); 7 | PizzaStore chicagoStore = new ChicagoPizzaStore(); 8 | 9 | Pizza pizza = nyStore.orderPizza("cheese"); 10 | System.out.println("Ethan ordered a " + pizza + "\n"); 11 | 12 | pizza = chicagoStore.orderPizza("cheese"); 13 | System.out.println("Joel ordered a " + pizza + "\n"); 14 | 15 | pizza = nyStore.orderPizza("clam"); 16 | System.out.println("Ethan ordered a " + pizza + "\n"); 17 | 18 | pizza = chicagoStore.orderPizza("clam"); 19 | System.out.println("Joel ordered a " + pizza + "\n"); 20 | 21 | pizza = nyStore.orderPizza("pepperoni"); 22 | System.out.println("Ethan ordered a " + pizza + "\n"); 23 | 24 | pizza = chicagoStore.orderPizza("pepperoni"); 25 | System.out.println("Joel ordered a " + pizza + "\n"); 26 | 27 | pizza = nyStore.orderPizza("veggie"); 28 | System.out.println("Ethan ordered a " + pizza + "\n"); 29 | 30 | pizza = chicagoStore.orderPizza("veggie"); 31 | System.out.println("Joel ordered a " + pizza + "\n"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/DependentPizzaStore.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class DependentPizzaStore { 4 | 5 | public Pizza createPizza(String style, String type) { 6 | Pizza pizza = null; 7 | if (style.equals("NY")) { 8 | if (type.equals("cheese")) { 9 | pizza = new NYStyleCheesePizza(); 10 | } else if (type.equals("veggie")) { 11 | pizza = new NYStyleVeggiePizza(); 12 | } else if (type.equals("clam")) { 13 | pizza = new NYStyleClamPizza(); 14 | } else if (type.equals("pepperoni")) { 15 | pizza = new NYStylePepperoniPizza(); 16 | } 17 | } else if (style.equals("Chicago")) { 18 | if (type.equals("cheese")) { 19 | pizza = new ChicagoStyleCheesePizza(); 20 | } else if (type.equals("veggie")) { 21 | pizza = new ChicagoStyleVeggiePizza(); 22 | } else if (type.equals("clam")) { 23 | pizza = new ChicagoStyleClamPizza(); 24 | } else if (type.equals("pepperoni")) { 25 | pizza = new ChicagoStylePepperoniPizza(); 26 | } 27 | } else { 28 | System.out.println("Error: invalid type of pizza"); 29 | return null; 30 | } 31 | pizza.prepare(); 32 | pizza.bake(); 33 | pizza.cut(); 34 | pizza.box(); 35 | return pizza; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Singleton/src/main/java/com/crow/ChocolateBoiler.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class ChocolateBoiler { 4 | private boolean empty; 5 | private boolean boiled; 6 | private static ChocolateBoiler uniqueInstance; 7 | 8 | private ChocolateBoiler() { 9 | empty = true; 10 | boiled = false; 11 | } 12 | 13 | public static ChocolateBoiler getInstance() { 14 | if (uniqueInstance == null) { 15 | System.out.println("Creating unique instance of Chocolate Boiler"); 16 | uniqueInstance = new ChocolateBoiler(); 17 | } 18 | System.out.println("Returning instance of Chocolate Boiler"); 19 | return uniqueInstance; 20 | } 21 | 22 | public void fill() { 23 | if (isEmpty()) { 24 | empty = false; 25 | boiled = false; 26 | // fill the boiler with a milk/chocolate mixture 27 | } 28 | } 29 | 30 | public void drain() { 31 | if (!isEmpty() && isBoiled()) { 32 | // drain the boiled milk and chocolate 33 | empty = true; 34 | } 35 | } 36 | 37 | public void boil() { 38 | if (!isEmpty() && !isBoiled()) { 39 | // bring the contents to a boil 40 | boiled = true; 41 | } 42 | } 43 | 44 | public boolean isEmpty() { 45 | return empty; 46 | } 47 | 48 | public boolean isBoiled() { 49 | return boiled; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/PancakeHouseMenu.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class PancakeHouseMenu implements Menu { 7 | ArrayList menuItems; 8 | 9 | public PancakeHouseMenu() { 10 | menuItems = new ArrayList(); 11 | 12 | addItem("K&B's Pancake Breakfast", 13 | "Pancakes with scrambled eggs, and toast", 14 | true, 15 | 2.99); 16 | 17 | addItem("Regular Pancake Breakfast", 18 | "Pancakes with fried eggs, sausage", 19 | false, 20 | 2.99); 21 | 22 | addItem("Blueberry Pancakes", 23 | "Pancakes made with fresh blueberries, and blueberry syrup", 24 | true, 25 | 3.49); 26 | 27 | addItem("Waffles", 28 | "Waffles, with your choice of blueberries or strawberries", 29 | true, 30 | 3.59); 31 | } 32 | 33 | public void addItem(String name, String description, 34 | boolean vegetarian, double price) 35 | { 36 | MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 37 | menuItems.add(menuItem); 38 | } 39 | 40 | public ArrayList getMenuItems() { 41 | return menuItems; 42 | } 43 | 44 | public Iterator createIterator() { 45 | return menuItems.iterator(); 46 | } 47 | 48 | // other menu methods here 49 | } 50 | -------------------------------------------------------------------------------- /Factory/src/test/java/com/crow/FactoryMethod/PizzaTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | public class PizzaTestDrive { 4 | 5 | public static void main(String[] args) { 6 | PizzaStore nyStore = new NYPizzaStore(); 7 | PizzaStore chicagoStore = new ChicagoPizzaStore(); 8 | 9 | Pizza pizza = nyStore.orderPizza("cheese"); 10 | System.out.println("Ethan ordered a " + pizza.getName() + "\n"); 11 | 12 | pizza = chicagoStore.orderPizza("cheese"); 13 | System.out.println("Joel ordered a " + pizza.getName() + "\n"); 14 | 15 | pizza = nyStore.orderPizza("clam"); 16 | System.out.println("Ethan ordered a " + pizza.getName() + "\n"); 17 | 18 | pizza = chicagoStore.orderPizza("clam"); 19 | System.out.println("Joel ordered a " + pizza.getName() + "\n"); 20 | 21 | pizza = nyStore.orderPizza("pepperoni"); 22 | System.out.println("Ethan ordered a " + pizza.getName() + "\n"); 23 | 24 | pizza = chicagoStore.orderPizza("pepperoni"); 25 | System.out.println("Joel ordered a " + pizza.getName() + "\n"); 26 | 27 | pizza = nyStore.orderPizza("veggie"); 28 | System.out.println("Ethan ordered a " + pizza.getName() + "\n"); 29 | 30 | pizza = chicagoStore.orderPizza("veggie"); 31 | System.out.println("Joel ordered a " + pizza.getName() + "\n"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 设计模式学习笔记 2 | 本仓库记录了我学习设计模式的历程,仓库中代码来自于《Head First Design Patterns》一书,每一个模式都附有相应的介绍博客,适合作为初学者入门学习的参考。 3 | ## 博客目录 4 | + [设计模式(0)——概述](https://crowhawk.github.io/2017/07/20/DesignPatterns0/) 5 | + [设计模式(1)——策略模式](https://crowhawk.github.io/2017/07/20/strategy_1/) 6 | + [设计模式(2)——观察者模式](https://crowhawk.github.io/2017/07/20/observer_2/) 7 | + [设计模式(3)——装饰者模式](https://crowhawk.github.io/2017/07/20/decorator_3/) 8 | + [设计模式(4)——工厂模式](https://crowhawk.github.io/2017/07/20/factory_4/) 9 | + [设计模式(5)——单例模式](https://crowhawk.github.io/2017/07/20/singleton_5/) 10 | + [设计模式(6)——命令模式](https://crowhawk.github.io/2017/07/20/command_6/) 11 | + [设计模式(7)——适配器模式](https://crowhawk.github.io/2017/07/20/adapter_7/) 12 | + [设计模式(8)——外观模式](https://crowhawk.github.io/2017/07/20/facade_8/) 13 | + [设计模式(9)——模板方法模式](https://crowhawk.github.io/2017/07/21/designpattern_9_template/) 14 | + [设计模式(10)——迭代器模式](https://crowhawk.github.io/2017/07/21/designpattern_10_iterator/) 15 | + [设计模式(11)——组合模式](https://crowhawk.github.io/2017/07/21/designpattern_11_composite/) 16 | + [设计模式(12)——状态模式](https://crowhawk.github.io/2017/07/21/designpattern_12_state/) 17 | ## 源码说明 18 | + 本仓库代码由Maven构建,下载以后可直接运行。 19 | + 每一个模块对应一个设计模式,由相应的模式名命名,其中代码的具体介绍可参考相应的博客。 20 | ## 联系作者 21 | [Personal Website:Crow Home](https://crowhawk.github.io/) 22 | -------------------------------------------------------------------------------- /State/src/test/java/com/crow/GumballMachineTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class GumballMachineTestDrive { 4 | 5 | public static void main(String[] args) { 6 | GumballMachine gumballMachine = 7 | new GumballMachine(10); 8 | 9 | System.out.println(gumballMachine); 10 | 11 | gumballMachine.insertQuarter(); 12 | gumballMachine.turnCrank(); 13 | gumballMachine.insertQuarter(); 14 | gumballMachine.turnCrank(); 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 | } 47 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/MenuItem.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | import java.util.ArrayList; 5 | 6 | public class MenuItem extends MenuComponent { 7 | 8 | String name; 9 | String description; 10 | boolean vegetarian; 11 | double price; 12 | 13 | public MenuItem(String name, 14 | String description, 15 | boolean vegetarian, 16 | double price) 17 | { 18 | this.name = name; 19 | this.description = description; 20 | this.vegetarian = vegetarian; 21 | this.price = price; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public String getDescription() { 29 | return description; 30 | } 31 | 32 | public double getPrice() { 33 | return price; 34 | } 35 | 36 | public boolean isVegetarian() { 37 | return vegetarian; 38 | } 39 | 40 | public Iterator createIterator() { 41 | return new NullIterator(); 42 | } 43 | 44 | public void print() { 45 | System.out.print(" " + getName()); 46 | if (isVegetarian()) { 47 | System.out.print("(v)"); 48 | } 49 | System.out.println(", " + getPrice()); 50 | System.out.println(" -- " + getDescription()); 51 | } 52 | //vv MenuItemCompositeV2Main 53 | } 54 | //^^ MenuItemCompositeV2Main 55 | //^^ MenuItemCompositeV2 56 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/FactoryMethod/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.FactoryMethod; 2 | 3 | import java.util.ArrayList; 4 | 5 | public abstract class Pizza { 6 | String name; 7 | String dough; 8 | String sauce; 9 | ArrayList toppings = new ArrayList(); 10 | 11 | void prepare() { 12 | System.out.println("Preparing " + name); 13 | System.out.println("Tossing dough..."); 14 | System.out.println("Adding sauce..."); 15 | System.out.println("Adding toppings: "); 16 | for (int i = 0; i < toppings.size(); i++) { 17 | System.out.println(" " + toppings.get(i)); 18 | } 19 | } 20 | 21 | void bake() { 22 | System.out.println("Bake for 25 minutes at 350"); 23 | } 24 | 25 | void cut() { 26 | System.out.println("Cutting the pizza into diagonal slices"); 27 | } 28 | 29 | void box() { 30 | System.out.println("Place pizza in official PizzaStore box"); 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public String toString() { 38 | StringBuffer display = new StringBuffer(); 39 | display.append("---- " + name + " ----\n"); 40 | display.append(dough + "\n"); 41 | display.append(sauce + "\n"); 42 | for (int i = 0; i < toppings.size(); i++) { 43 | display.append((String )toppings.get(i) + "\n"); 44 | } 45 | return display.toString(); 46 | } 47 | } 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/Amplifier.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class Amplifier { 4 | String description; 5 | Tuner tuner; 6 | DvdPlayer dvd; 7 | CdPlayer cd; 8 | 9 | public Amplifier(String description) { 10 | this.description = description; 11 | } 12 | 13 | public void on() { 14 | System.out.println(description + " on"); 15 | } 16 | 17 | public void off() { 18 | System.out.println(description + " off"); 19 | } 20 | 21 | public void setStereoSound() { 22 | System.out.println(description + " stereo mode on"); 23 | } 24 | 25 | public void setSurroundSound() { 26 | System.out.println(description + " surround sound on (5 speakers, 1 subwoofer)"); 27 | } 28 | 29 | public void setVolume(int level) { 30 | System.out.println(description + " setting volume to " + level); 31 | } 32 | 33 | public void setTuner(Tuner tuner) { 34 | System.out.println(description + " setting tuner to " + dvd); 35 | this.tuner = tuner; 36 | } 37 | 38 | public void setDvd(DvdPlayer dvd) { 39 | System.out.println(description + " setting DVD player to " + dvd); 40 | this.dvd = dvd; 41 | } 42 | 43 | public void setCd(CdPlayer cd) { 44 | System.out.println(description + " setting CD player to " + cd); 45 | this.cd = cd; 46 | } 47 | 48 | public String toString() { 49 | return description; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/WinnerState.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 | System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter"); 24 | gumballMachine.releaseBall(); 25 | if (gumballMachine.getCount() == 0) { 26 | gumballMachine.setState(gumballMachine.getSoldOutState()); 27 | } else { 28 | gumballMachine.releaseBall(); 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 String toString() { 39 | return "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/CdPlayer.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class CdPlayer { 4 | String description; 5 | int currentTrack; 6 | Amplifier amplifier; 7 | String title; 8 | 9 | public CdPlayer(String description, Amplifier amplifier) { 10 | this.description = description; 11 | this.amplifier = amplifier; 12 | } 13 | 14 | public void on() { 15 | System.out.println(description + " on"); 16 | } 17 | 18 | public void off() { 19 | System.out.println(description + " off"); 20 | } 21 | 22 | public void eject() { 23 | title = null; 24 | System.out.println(description + " eject"); 25 | } 26 | 27 | public void play(String title) { 28 | this.title = title; 29 | currentTrack = 0; 30 | System.out.println(description + " playing \"" + title + "\""); 31 | } 32 | 33 | public void play(int track) { 34 | if (title == null) { 35 | System.out.println(description + " can't play track " + currentTrack + 36 | ", no cd inserted"); 37 | } else { 38 | currentTrack = track; 39 | System.out.println(description + " playing track " + currentTrack); 40 | } 41 | } 42 | 43 | public void stop() { 44 | currentTrack = 0; 45 | System.out.println(description + " stopped"); 46 | } 47 | 48 | public void pause() { 49 | System.out.println(description + " paused \"" + title + "\""); 50 | } 51 | 52 | public String toString() { 53 | return description; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Composite/src/main/java/com/crow/Menu.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | import java.util.ArrayList; 5 | 6 | public class Menu extends MenuComponent { 7 | 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 (MenuComponent)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 | return new CompositeIterator(menuComponents.iterator()); 40 | } 41 | 42 | 43 | public void print() { 44 | System.out.print("\n" + getName()); 45 | System.out.println(", " + getDescription()); 46 | System.out.println("---------------------"); 47 | 48 | Iterator iterator = menuComponents.iterator(); 49 | while (iterator.hasNext()) { 50 | MenuComponent menuComponent = 51 | (MenuComponent)iterator.next(); 52 | menuComponent.print(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/WinnerState.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | public class WinnerState implements State { 4 | transient 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 | System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter"); 24 | gumballMachine.releaseBall(); 25 | if (gumballMachine.getCount() == 0) { 26 | gumballMachine.setState(gumballMachine.getSoldOutState()); 27 | } else { 28 | gumballMachine.releaseBall(); 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 String toString() { 39 | return "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.crow 6 | DesignPattern 7 | 1.0-SNAPSHOT 8 | 9 | Decorator 10 | Factory 11 | Singleton 12 | Command 13 | Adapter 14 | Adapter 15 | Adapter 16 | Facade 17 | Template 18 | Template 19 | Iterator 20 | Composite 21 | Strategy 22 | State 23 | Proxy 24 | RMI 25 | 26 | pom 27 | 28 | DesignPattern 29 | http://maven.apache.org 30 | 31 | 32 | UTF-8 33 | 34 | 35 | 36 | 37 | junit 38 | junit 39 | 3.8.1 40 | test 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Factory/src/main/java/com/crow/AbstractFactory/Pizza.java: -------------------------------------------------------------------------------- 1 | package com.crow.AbstractFactory; 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 | -------------------------------------------------------------------------------- /Command/src/main/java/com/crow/Invoker/RemoteControl.java: -------------------------------------------------------------------------------- 1 | package com.crow.Invoker; 2 | 3 | import com.crow.Command.Command; 4 | import com.crow.Command.NoCommand; 5 | 6 | // 7 | // This is the invoker 8 | // 9 | public class RemoteControl { 10 | Command[] onCommands; 11 | Command[] offCommands; 12 | Command undoCommand; 13 | 14 | public RemoteControl() { 15 | onCommands = new Command[7]; 16 | offCommands = new Command[7]; 17 | 18 | Command noCommand = new NoCommand(); 19 | for(int i=0;i<7;i++) { 20 | onCommands[i] = noCommand; 21 | offCommands[i] = noCommand; 22 | } 23 | undoCommand = noCommand; 24 | } 25 | 26 | public void setCommand(int slot, Command onCommand, Command offCommand) { 27 | onCommands[slot] = onCommand; 28 | offCommands[slot] = offCommand; 29 | } 30 | 31 | public void onButtonWasPushed(int slot) { 32 | onCommands[slot].execute(); 33 | undoCommand = onCommands[slot]; 34 | } 35 | 36 | public void offButtonWasPushed(int slot) { 37 | offCommands[slot].execute(); 38 | undoCommand = offCommands[slot]; 39 | } 40 | 41 | public void undoButtonWasPushed() { 42 | undoCommand.undo(); 43 | } 44 | 45 | public String toString() { 46 | StringBuffer stringBuff = new StringBuffer(); 47 | stringBuff.append("\n------ Remote Control -------\n"); 48 | for (int i = 0; i < onCommands.length; i++) { 49 | stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() 50 | + " " + offCommands[i].getClass().getName() + "\n"); 51 | } 52 | stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n"); 53 | return stringBuff.toString(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Command/src/test/java/com/crow/RemoteLoader.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import com.crow.Command.*; 4 | import com.crow.Invoker.RemoteControl; 5 | import com.crow.Receiver.Hottub; 6 | import com.crow.Receiver.Light; 7 | import com.crow.Receiver.Stereo; 8 | import com.crow.Receiver.TV; 9 | 10 | public class RemoteLoader { 11 | 12 | public static void main(String[] args) { 13 | 14 | RemoteControl remoteControl = new RemoteControl(); 15 | 16 | Light light = new Light("Living Room"); 17 | TV tv = new TV("Living Room"); 18 | Stereo stereo = new Stereo("Living Room"); 19 | Hottub hottub = new Hottub(); 20 | 21 | LightOnCommand lightOn = new LightOnCommand(light); 22 | StereoOnCommand stereoOn = new StereoOnCommand(stereo); 23 | TVOnCommand tvOn = new TVOnCommand(tv); 24 | HottubOnCommand hottubOn = new HottubOnCommand(hottub); 25 | LightOffCommand lightOff = new LightOffCommand(light); 26 | StereoOffCommand stereoOff = new StereoOffCommand(stereo); 27 | TVOffCommand tvOff = new TVOffCommand(tv); 28 | HottubOffCommand hottubOff = new HottubOffCommand(hottub); 29 | 30 | Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn}; 31 | Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff}; 32 | 33 | MacroCommand partyOnMacro = new MacroCommand(partyOn); 34 | MacroCommand partyOffMacro = new MacroCommand(partyOff); 35 | 36 | remoteControl.setCommand(0, partyOnMacro, partyOffMacro); 37 | 38 | System.out.println(remoteControl); 39 | System.out.println("--- Pushing Macro On---"); 40 | remoteControl.onButtonWasPushed(0); 41 | System.out.println("--- Pushing Macro Off---"); 42 | remoteControl.offButtonWasPushed(0); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/DvdPlayer.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class DvdPlayer { 4 | String description; 5 | int currentTrack; 6 | Amplifier amplifier; 7 | String movie; 8 | 9 | public DvdPlayer(String description, Amplifier amplifier) { 10 | this.description = description; 11 | this.amplifier = amplifier; 12 | } 13 | 14 | public void on() { 15 | System.out.println(description + " on"); 16 | } 17 | 18 | public void off() { 19 | System.out.println(description + " off"); 20 | } 21 | 22 | public void eject() { 23 | movie = null; 24 | System.out.println(description + " eject"); 25 | } 26 | 27 | public void play(String movie) { 28 | this.movie = movie; 29 | currentTrack = 0; 30 | System.out.println(description + " playing \"" + movie + "\""); 31 | } 32 | 33 | public void play(int track) { 34 | if (movie == null) { 35 | System.out.println(description + " can't play track " + track + " no dvd inserted"); 36 | } else { 37 | currentTrack = track; 38 | System.out.println(description + " playing track " + currentTrack + " of \"" + movie + "\""); 39 | } 40 | } 41 | 42 | public void stop() { 43 | currentTrack = 0; 44 | System.out.println(description + " stopped \"" + movie + "\""); 45 | } 46 | 47 | public void pause() { 48 | System.out.println(description + " paused \"" + movie + "\""); 49 | } 50 | 51 | public void setTwoChannelAudio() { 52 | System.out.println(description + " set two channel audio"); 53 | } 54 | 55 | public void setSurroundAudio() { 56 | System.out.println(description + " set surround audio"); 57 | } 58 | 59 | public String toString() { 60 | return description; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/DinerMenu.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | 5 | public class DinerMenu implements Menu { 6 | static final int MAX_ITEMS = 6; 7 | int numberOfItems = 0; 8 | MenuItem[] menuItems; 9 | 10 | public DinerMenu() { 11 | menuItems = new MenuItem[MAX_ITEMS]; 12 | 13 | addItem("Vegetarian BLT", 14 | "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99); 15 | addItem("BLT", 16 | "Bacon with lettuce & tomato on whole wheat", false, 2.99); 17 | addItem("Soup of the day", 18 | "Soup of the day, with a side of potato salad", false, 3.29); 19 | addItem("Hotdog", 20 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 21 | false, 3.05); 22 | addItem("Steamed Veggies and Brown Rice", 23 | "A medly of steamed vegetables over brown rice", true, 3.99); 24 | addItem("Pasta", 25 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 26 | true, 3.89); 27 | } 28 | 29 | public void addItem(String name, String description, 30 | boolean vegetarian, double price) 31 | { 32 | MenuItem menuItem = new MenuItem(name, description, vegetarian, price); 33 | if (numberOfItems >= MAX_ITEMS) { 34 | System.err.println("Sorry, menu is full! Can't add item to menu"); 35 | } else { 36 | menuItems[numberOfItems] = menuItem; 37 | numberOfItems = numberOfItems + 1; 38 | } 39 | } 40 | 41 | public MenuItem[] getMenuItems() { 42 | return menuItems; 43 | } 44 | 45 | public Iterator createIterator() { 46 | return new DinerMenuIterator(menuItems); 47 | //return new AlternatingDinerMenuIterator(menuItems); 48 | } 49 | 50 | // other menu methods here 51 | } 52 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Facade/src/main/java/com/crow/HomeTheaterFacade.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | public class HomeTheaterFacade { 4 | Amplifier amp; 5 | Tuner tuner; 6 | DvdPlayer dvd; 7 | CdPlayer cd; 8 | Projector projector; 9 | TheaterLights lights; 10 | Screen screen; 11 | PopcornPopper popper; 12 | 13 | public HomeTheaterFacade(Amplifier amp, 14 | Tuner tuner, 15 | DvdPlayer dvd, 16 | CdPlayer cd, 17 | Projector projector, 18 | Screen screen, 19 | TheaterLights lights, 20 | PopcornPopper popper) { 21 | 22 | this.amp = amp; 23 | this.tuner = tuner; 24 | this.dvd = dvd; 25 | this.cd = cd; 26 | this.projector = projector; 27 | this.screen = screen; 28 | this.lights = lights; 29 | this.popper = popper; 30 | } 31 | 32 | public void watchMovie(String movie) { 33 | System.out.println("Get ready to watch a movie..."); 34 | popper.on(); 35 | popper.pop(); 36 | lights.dim(10); 37 | screen.down(); 38 | projector.on(); 39 | projector.wideScreenMode(); 40 | amp.on(); 41 | amp.setDvd(dvd); 42 | amp.setSurroundSound(); 43 | amp.setVolume(5); 44 | dvd.on(); 45 | dvd.play(movie); 46 | } 47 | 48 | 49 | public void endMovie() { 50 | System.out.println("Shutting movie theater down..."); 51 | popper.off(); 52 | lights.on(); 53 | screen.up(); 54 | projector.off(); 55 | amp.off(); 56 | dvd.stop(); 57 | dvd.eject(); 58 | dvd.off(); 59 | } 60 | 61 | public void listenToCd(String cdTitle) { 62 | System.out.println("Get ready for an audiopile experence..."); 63 | lights.on(); 64 | amp.on(); 65 | amp.setVolume(5); 66 | amp.setCd(cd); 67 | amp.setStereoSound(); 68 | cd.on(); 69 | cd.play(cdTitle); 70 | } 71 | 72 | public void endCd() { 73 | System.out.println("Shutting down CD..."); 74 | amp.off(); 75 | amp.setCd(cd); 76 | cd.eject(); 77 | cd.off(); 78 | } 79 | 80 | public void listenToRadio(double frequency) { 81 | System.out.println("Tuning in the airwaves..."); 82 | tuner.on(); 83 | tuner.setFrequency(frequency); 84 | amp.on(); 85 | amp.setVolume(5); 86 | amp.setTuner(tuner); 87 | } 88 | 89 | public void endRadio() { 90 | System.out.println("Shutting down the tuner..."); 91 | tuner.off(); 92 | amp.off(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /State/src/main/java/com/crow/GumballMachine.java: -------------------------------------------------------------------------------- 1 | package com.crow; 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 | state = noQuarterState; 58 | } 59 | 60 | public State getState() { 61 | return state; 62 | } 63 | 64 | public State getSoldOutState() { 65 | return soldOutState; 66 | } 67 | 68 | public State getNoQuarterState() { 69 | return noQuarterState; 70 | } 71 | 72 | public State getHasQuarterState() { 73 | return hasQuarterState; 74 | } 75 | 76 | public State getSoldState() { 77 | return soldState; 78 | } 79 | 80 | public State getWinnerState() { 81 | return winnerState; 82 | } 83 | 84 | public String toString() { 85 | StringBuffer result = new StringBuffer(); 86 | result.append("\nMighty Gumball, Inc."); 87 | result.append("\nJava-enabled Standing Gumball Model #2004"); 88 | result.append("\nInventory: " + count + " gumball"); 89 | if (count != 1) { 90 | result.append("s"); 91 | } 92 | result.append("\n"); 93 | result.append("Machine is " + state + "\n"); 94 | return result.toString(); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Proxy/src/test/java/com/crow/dynamicproxy/MatchMakingTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow.dynamicproxy; 2 | 3 | import java.lang.reflect.*; 4 | import java.util.*; 5 | 6 | public class MatchMakingTestDrive { 7 | Hashtable datingDB = new Hashtable(); 8 | 9 | public static void main(String[] args) { 10 | MatchMakingTestDrive test = new MatchMakingTestDrive(); 11 | test.drive(); 12 | } 13 | 14 | public MatchMakingTestDrive() { 15 | initializeDatabase(); 16 | } 17 | 18 | public void drive() { 19 | PersonBean joe = getPersonFromDatabase("Joe Javabean"); 20 | PersonBean ownerProxy = getOwnerProxy(joe); 21 | System.out.println("Name is " + ownerProxy.getName()); 22 | ownerProxy.setInterests("bowling, Go"); 23 | System.out.println("Interests set from owner proxy"); 24 | try { 25 | ownerProxy.setHotOrNotRating(10); 26 | } catch (Exception e) { 27 | System.out.println("Can't set rating from owner proxy"); 28 | } 29 | System.out.println("Rating is " + ownerProxy.getHotOrNotRating()); 30 | 31 | PersonBean nonOwnerProxy = getNonOwnerProxy(joe); 32 | System.out.println("Name is " + nonOwnerProxy.getName()); 33 | try { 34 | nonOwnerProxy.setInterests("bowling, Go"); 35 | } catch (Exception e) { 36 | System.out.println("Can't set interests from non owner proxy"); 37 | } 38 | nonOwnerProxy.setHotOrNotRating(3); 39 | System.out.println("Rating set from non owner proxy"); 40 | System.out.println("Rating is " + nonOwnerProxy.getHotOrNotRating()); 41 | } 42 | 43 | PersonBean getOwnerProxy(PersonBean person) { 44 | 45 | return (PersonBean) Proxy.newProxyInstance( 46 | person.getClass().getClassLoader(), 47 | person.getClass().getInterfaces(), 48 | new OwnerInvocationHandler(person)); 49 | } 50 | 51 | PersonBean getNonOwnerProxy(PersonBean person) { 52 | 53 | return (PersonBean) Proxy.newProxyInstance( 54 | person.getClass().getClassLoader(), 55 | person.getClass().getInterfaces(), 56 | new NonOwnerInvocationHandler(person)); 57 | } 58 | 59 | PersonBean getPersonFromDatabase(String name) { 60 | return (PersonBean)datingDB.get(name); 61 | } 62 | 63 | void initializeDatabase() { 64 | PersonBean joe = new PersonBeanImpl(); 65 | joe.setName("Joe Javabean"); 66 | joe.setInterests("cars, computers, music"); 67 | joe.setHotOrNotRating(7); 68 | datingDB.put(joe.getName(), joe); 69 | 70 | PersonBean kelly = new PersonBeanImpl(); 71 | kelly.setName("Kelly Klosure"); 72 | kelly.setInterests("ebay, movies, music"); 73 | kelly.setHotOrNotRating(6); 74 | datingDB.put(kelly.getName(), kelly); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Iterator/src/main/java/com/crow/Waitress.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.Iterator; 4 | 5 | public class Waitress { 6 | Menu pancakeHouseMenu; 7 | Menu dinerMenu; 8 | Menu cafeMenu; 9 | 10 | public Waitress(Menu pancakeHouseMenu, Menu dinerMenu, Menu cafeMenu) { 11 | this.pancakeHouseMenu = pancakeHouseMenu; 12 | this.dinerMenu = dinerMenu; 13 | this.cafeMenu = cafeMenu; 14 | } 15 | 16 | public void printMenu() { 17 | Iterator pancakeIterator = pancakeHouseMenu.createIterator(); 18 | Iterator dinerIterator = dinerMenu.createIterator(); 19 | Iterator cafeIterator = cafeMenu.createIterator(); 20 | 21 | System.out.println("MENU\n----\nBREAKFAST"); 22 | printMenu(pancakeIterator); 23 | System.out.println("\nLUNCH"); 24 | printMenu(dinerIterator); 25 | System.out.println("\nDINNER"); 26 | printMenu(cafeIterator); 27 | } 28 | 29 | private void printMenu(Iterator iterator) { 30 | while (iterator.hasNext()) { 31 | MenuItem menuItem = (MenuItem)iterator.next(); 32 | System.out.print(menuItem.getName() + ", "); 33 | System.out.print(menuItem.getPrice() + " -- "); 34 | System.out.println(menuItem.getDescription()); 35 | } 36 | } 37 | 38 | public void printVegetarianMenu() { 39 | System.out.println("\nVEGETARIAN MENU\n---------------"); 40 | printVegetarianMenu(pancakeHouseMenu.createIterator()); 41 | printVegetarianMenu(dinerMenu.createIterator()); 42 | printVegetarianMenu(cafeMenu.createIterator()); 43 | } 44 | 45 | public boolean isItemVegetarian(String name) { 46 | Iterator pancakeIterator = pancakeHouseMenu.createIterator(); 47 | if (isVegetarian(name, pancakeIterator)) { 48 | return true; 49 | } 50 | Iterator dinerIterator = dinerMenu.createIterator(); 51 | if (isVegetarian(name, dinerIterator)) { 52 | return true; 53 | } 54 | Iterator cafeIterator = cafeMenu.createIterator(); 55 | if (isVegetarian(name, cafeIterator)) { 56 | return true; 57 | } 58 | return false; 59 | } 60 | 61 | 62 | private void printVegetarianMenu(Iterator iterator) { 63 | while (iterator.hasNext()) { 64 | MenuItem menuItem = (MenuItem)iterator.next(); 65 | if (menuItem.isVegetarian()) { 66 | System.out.print(menuItem.getName() + ", "); 67 | System.out.print(menuItem.getPrice() + " -- "); 68 | System.out.println(menuItem.getDescription()); 69 | } 70 | } 71 | } 72 | 73 | private boolean isVegetarian(String name, Iterator iterator) { 74 | while (iterator.hasNext()) { 75 | MenuItem menuItem = (MenuItem)iterator.next(); 76 | if (menuItem.getName().equals(name)) { 77 | if (menuItem.isVegetarian()) { 78 | return true; 79 | } 80 | } 81 | } 82 | return false; 83 | } 84 | } 85 | //^^ WaitressCafeMain 86 | //^^ WaitressCafe 87 | -------------------------------------------------------------------------------- /Proxy/src/main/java/com/crow/remoteproxy/GumballMachine.java: -------------------------------------------------------------------------------- 1 | package com.crow.remoteproxy; 2 | 3 | import java.rmi.*; 4 | import java.rmi.server.*; 5 | 6 | public class GumballMachine 7 | extends UnicastRemoteObject implements GumballMachineRemote 8 | { 9 | State soldOutState; 10 | State noQuarterState; 11 | State hasQuarterState; 12 | State soldState; 13 | State winnerState; 14 | 15 | State state = soldOutState; 16 | int count = 0; 17 | String location; 18 | 19 | public GumballMachine(String location, int numberGumballs) throws RemoteException { 20 | soldOutState = new SoldOutState(this); 21 | noQuarterState = new NoQuarterState(this); 22 | hasQuarterState = new HasQuarterState(this); 23 | soldState = new SoldState(this); 24 | winnerState = new WinnerState(this); 25 | 26 | this.count = numberGumballs; 27 | if (numberGumballs > 0) { 28 | state = noQuarterState; 29 | } 30 | this.location = location; 31 | } 32 | 33 | 34 | public void insertQuarter() { 35 | state.insertQuarter(); 36 | } 37 | 38 | public void ejectQuarter() { 39 | state.ejectQuarter(); 40 | } 41 | 42 | public void turnCrank() { 43 | state.turnCrank(); 44 | state.dispense(); 45 | } 46 | 47 | void setState(State state) { 48 | this.state = state; 49 | } 50 | 51 | void releaseBall() { 52 | System.out.println("A gumball comes rolling out the slot..."); 53 | if (count != 0) { 54 | count = count - 1; 55 | } 56 | } 57 | 58 | public void refill(int count) { 59 | this.count = count; 60 | state = noQuarterState; 61 | } 62 | 63 | public int getCount() { 64 | return count; 65 | } 66 | 67 | public State getState() { 68 | return state; 69 | } 70 | 71 | public String getLocation() { 72 | return location; 73 | } 74 | 75 | public State getSoldOutState() { 76 | return soldOutState; 77 | } 78 | 79 | public State getNoQuarterState() { 80 | return noQuarterState; 81 | } 82 | 83 | public State getHasQuarterState() { 84 | return hasQuarterState; 85 | } 86 | 87 | public State getSoldState() { 88 | return soldState; 89 | } 90 | 91 | public State getWinnerState() { 92 | return winnerState; 93 | } 94 | 95 | public String toString() { 96 | StringBuffer result = new StringBuffer(); 97 | result.append("\nMighty Gumball, Inc."); 98 | result.append("\nJava-enabled Standing Gumball Model #2004"); 99 | result.append("\nInventory: " + count + " gumball"); 100 | if (count != 1) { 101 | result.append("s"); 102 | } 103 | result.append("\n"); 104 | result.append("Machine is " + state + "\n"); 105 | return result.toString(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Composite/src/test/java/com/crow/MenuTestDrive.java: -------------------------------------------------------------------------------- 1 | package com.crow; 2 | 3 | import java.util.*; 4 | 5 | public class MenuTestDrive { 6 | public static void main(String args[]) { 7 | 8 | MenuComponent pancakeHouseMenu = 9 | new Menu("PANCAKE HOUSE MENU", "Breakfast"); 10 | MenuComponent dinerMenu = 11 | new Menu("DINER MENU", "Lunch"); 12 | MenuComponent cafeMenu = 13 | new Menu("CAFE MENU", "Dinner"); 14 | MenuComponent dessertMenu = 15 | new Menu("DESSERT MENU", "Dessert of course!"); 16 | 17 | MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); 18 | 19 | allMenus.add(pancakeHouseMenu); 20 | allMenus.add(dinerMenu); 21 | allMenus.add(cafeMenu); 22 | 23 | pancakeHouseMenu.add(new MenuItem( 24 | "K&B's Pancake Breakfast", 25 | "Pancakes with scrambled eggs, and toast", 26 | true, 27 | 2.99)); 28 | pancakeHouseMenu.add(new MenuItem( 29 | "Regular Pancake Breakfast", 30 | "Pancakes with fried eggs, sausage", 31 | false, 32 | 2.99)); 33 | pancakeHouseMenu.add(new MenuItem( 34 | "Blueberry Pancakes", 35 | "Pancakes made with fresh blueberries, and blueberry syrup", 36 | true, 37 | 3.49)); 38 | pancakeHouseMenu.add(new MenuItem( 39 | "Waffles", 40 | "Waffles, with your choice of blueberries or strawberries", 41 | true, 42 | 3.59)); 43 | 44 | dinerMenu.add(new MenuItem( 45 | "Vegetarian BLT", 46 | "(Fakin') Bacon with lettuce & tomato on whole wheat", 47 | true, 48 | 2.99)); 49 | dinerMenu.add(new MenuItem( 50 | "BLT", 51 | "Bacon with lettuce & tomato on whole wheat", 52 | false, 53 | 2.99)); 54 | dinerMenu.add(new MenuItem( 55 | "Soup of the day", 56 | "A bowl of the soup of the day, with a side of potato salad", 57 | false, 58 | 3.29)); 59 | dinerMenu.add(new MenuItem( 60 | "Hotdog", 61 | "A hot dog, with saurkraut, relish, onions, topped with cheese", 62 | false, 63 | 3.05)); 64 | dinerMenu.add(new MenuItem( 65 | "Steamed Veggies and Brown Rice", 66 | "A medly of steamed vegetables over brown rice", 67 | true, 68 | 3.99)); 69 | 70 | dinerMenu.add(new MenuItem( 71 | "Pasta", 72 | "Spaghetti with Marinara Sauce, and a slice of sourdough bread", 73 | true, 74 | 3.89)); 75 | 76 | dinerMenu.add(dessertMenu); 77 | 78 | dessertMenu.add(new MenuItem( 79 | "Apple Pie", 80 | "Apple pie with a flakey crust, topped with vanilla icecream", 81 | true, 82 | 1.59)); 83 | dessertMenu.add(new MenuItem( 84 | "Cheesecake", 85 | "Creamy New York cheesecake, with a chocolate graham crust", 86 | true, 87 | 1.99)); 88 | dessertMenu.add(new MenuItem( 89 | "Sorbet", 90 | "A scoop of raspberry and a scoop of lime", 91 | true, 92 | 1.89)); 93 | 94 | cafeMenu.add(new MenuItem( 95 | "Veggie Burger and Air Fries", 96 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 97 | true, 98 | 3.99)); 99 | cafeMenu.add(new MenuItem( 100 | "Soup of the day", 101 | "A cup of the soup of the day, with a side salad", 102 | false, 103 | 3.69)); 104 | cafeMenu.add(new MenuItem( 105 | "Burrito", 106 | "A large burrito, with whole pinto beans, salsa, guacamole", 107 | true, 108 | 4.29)); 109 | 110 | Waitress waitress = new Waitress(allMenus); 111 | 112 | waitress.printVegetarianMenu(); 113 | 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------