|
| 1 | +package creational.factory; |
| 2 | + |
| 3 | +import org.javatuples.Pair; |
| 4 | +import org.reflections.Reflections; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +interface IHotDrink |
| 12 | +{ |
| 13 | + void consume(); |
| 14 | +} |
| 15 | + |
| 16 | +class Tea implements IHotDrink |
| 17 | +{ |
| 18 | + @Override |
| 19 | + public void consume() |
| 20 | + { |
| 21 | + System.out.println("This tea is nice but I'd prefer it with milk."); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class Coffee implements IHotDrink |
| 26 | +{ |
| 27 | + @Override |
| 28 | + public void consume() |
| 29 | + { |
| 30 | + System.out.println("This coffee is delicious"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +interface IHotDrinkFactory |
| 35 | +{ |
| 36 | + IHotDrink prepare(int amount); |
| 37 | +} |
| 38 | + |
| 39 | +class TeaFactory implements IHotDrinkFactory |
| 40 | +{ |
| 41 | + @Override |
| 42 | + public IHotDrink prepare(int amount) |
| 43 | + { |
| 44 | + System.out.println( |
| 45 | + "Put in tea bag, boil water, pour " |
| 46 | + + amount + "ml, add lemon, enjoy!" |
| 47 | + ); |
| 48 | + return new Tea(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class CoffeeFactory implements IHotDrinkFactory |
| 53 | +{ |
| 54 | + |
| 55 | + @Override |
| 56 | + public IHotDrink prepare(int amount) |
| 57 | + { |
| 58 | + System.out.println( |
| 59 | + "Grind some beans, boil water, pour " |
| 60 | + + amount + " ml, add cream and sugar, enjoy!" |
| 61 | + ); |
| 62 | + return new Coffee(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +class HotDrinkMachine |
| 67 | +{ |
| 68 | + public enum AvailableDrink |
| 69 | + { |
| 70 | + COFFEE, TEA |
| 71 | + } |
| 72 | + |
| 73 | + private Map<AvailableDrink, IHotDrinkFactory> factories = |
| 74 | + new HashMap<>(); |
| 75 | + |
| 76 | + private List<Pair<String, IHotDrinkFactory>> namedFactories = |
| 77 | + new ArrayList<>(); |
| 78 | + |
| 79 | + public HotDrinkMachine() throws Exception |
| 80 | + { |
| 81 | + // option 1: use an enum |
| 82 | + for (AvailableDrink drink : AvailableDrink.values()) |
| 83 | + { |
| 84 | + String s = drink.toString(); |
| 85 | + String factoryName = "" + Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase(); |
| 86 | + Class<?> factory = Class.forName("creational.factory." + factoryName + "Factory"); |
| 87 | + factories.put(drink, (IHotDrinkFactory) factory.getDeclaredConstructor().newInstance()); |
| 88 | + } |
| 89 | + |
| 90 | + // option 2: find all implementors of IHotDrinkFactory |
| 91 | + Set<Class<? extends IHotDrinkFactory>> types = |
| 92 | + new Reflections("creational.factory") // "" |
| 93 | + .getSubTypesOf(IHotDrinkFactory.class); |
| 94 | + for (Class<? extends IHotDrinkFactory> type : types) |
| 95 | + { |
| 96 | + namedFactories.add(new Pair<>( |
| 97 | + type.getSimpleName().replace("Factory", ""), |
| 98 | + type.getDeclaredConstructor().newInstance() |
| 99 | + )); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public IHotDrink makeDrink() throws IOException |
| 104 | + { |
| 105 | + System.out.println("Available drinks"); |
| 106 | + for (int index = 0; index < namedFactories.size(); ++index) |
| 107 | + { |
| 108 | + Pair<String, IHotDrinkFactory> item = namedFactories.get(index); |
| 109 | + System.out.println("" + index + ": " + item.getValue0()); |
| 110 | + } |
| 111 | + |
| 112 | + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); |
| 113 | + while (true) |
| 114 | + { |
| 115 | + String s; |
| 116 | + int i, amount; |
| 117 | + if ((s = reader.readLine()) != null |
| 118 | + && (i = Integer.parseInt(s)) >= 0 |
| 119 | + && i < namedFactories.size()) |
| 120 | + { |
| 121 | + System.out.println("Specify amount: "); |
| 122 | + s = reader.readLine(); |
| 123 | + if (s != null |
| 124 | + && (amount = Integer.parseInt(s)) > 0) |
| 125 | + { |
| 126 | + return namedFactories.get(i).getValue1().prepare(amount); |
| 127 | + } |
| 128 | + } |
| 129 | + System.out.println("Incorrect input, try again."); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public IHotDrink makeDrink(AvailableDrink drink, int amount) |
| 134 | + { |
| 135 | + return ((IHotDrinkFactory)factories.get(drink)).prepare(amount); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +class AbstractFactoryDemo |
| 140 | +{ |
| 141 | + public static void main(String[] args) throws Exception |
| 142 | + { |
| 143 | + HotDrinkMachine machine = new HotDrinkMachine(); |
| 144 | + IHotDrink tea = machine.makeDrink(HotDrinkMachine.AvailableDrink.TEA, 200); |
| 145 | + tea.consume(); |
| 146 | + |
| 147 | + // interactive |
| 148 | + IHotDrink drink = machine.makeDrink(); |
| 149 | + drink.consume(); |
| 150 | + } |
| 151 | +} |
0 commit comments