|
4 | 4 | @author: Aseem Jain |
5 | 5 | @title: Design Patterns with Java 9 |
6 | 6 | @link: https://premaseem.wordpress.com/category/computers/design-patterns/ |
7 | | -@copyright: 2018 Packt Publication |
8 | 7 | */ |
9 | 8 |
|
10 | | -import com.premaseem.icecreams.AmulChocolateIceCream; |
11 | | -import com.premaseem.icecreams.AmulStrawberryIceCream; |
12 | | -import com.premaseem.icecreams.DairyQueenChocolateIceCream; |
13 | | -import com.premaseem.icecreams.DairyQueenStrawberryIceCream; |
| 9 | +import com.premaseem.icecreams.IceCream; |
| 10 | +import com.premaseem.milkshake.MilkShake; |
14 | 11 |
|
15 | 12 | import java.util.Scanner; |
16 | 13 |
|
17 | 14 | public class Client { |
18 | 15 | public static void main (String[] args) { |
19 | 16 | Scanner scan = new Scanner(System.in); |
20 | 17 | System.out.println("Ice cream Abstract factory example "); |
21 | | - System.out.println("Please enter the Brand of Ice cream ... "); |
22 | | - System.out.println("Dairy Queen"); |
23 | | - System.out.println("Amul"); |
24 | | - String iceCreamBrand = scan.next(); |
| 18 | + |
| 19 | + |
| 20 | + System.out.println("What you would like to order ... "); |
| 21 | + System.out.println("Icecream"); |
| 22 | + System.out.println("Milkshake"); |
| 23 | + String choice = scan.next(); |
25 | 24 |
|
26 | 25 | System.out.println("Please enter your ice cream flavor ... "); |
27 | 26 | System.out.println("Strawberry"); |
28 | 27 | System.out.println("Chocolate"); |
29 | | - String iceCreamChoice = scan.next(); |
30 | | - |
31 | | - |
32 | | - // Family of similar products with different brands. ( Tight couple) |
33 | | - DairyQueenStrawberryIceCream dairyQueenStrawberryIceCream =null; |
34 | | - DairyQueenChocolateIceCream dairyQueenChocolateIceCream = null; |
35 | | - AmulStrawberryIceCream amulStrawberryIceCream =null; |
36 | | - AmulChocolateIceCream amulchocolateIceCream = null; |
37 | | - |
38 | | - // Sphegati code with if else ladder |
39 | | - if (iceCreamBrand.equalsIgnoreCase("Amul") |
40 | | - && iceCreamChoice.equalsIgnoreCase("Strawberry")){ |
41 | | - amulStrawberryIceCream = new AmulStrawberryIceCream(); |
| 28 | + String flavor = scan.next(); |
42 | 29 |
|
43 | | - }else if (iceCreamBrand.equalsIgnoreCase("Amul") && |
44 | | - iceCreamChoice.equalsIgnoreCase("Chocolate")){ |
45 | | - amulchocolateIceCream = new AmulChocolateIceCream(); |
| 30 | + System.out.println("Please enter the Brand of Ice cream ... "); |
| 31 | + System.out.println("Dairy Queen"); |
| 32 | + System.out.println("Amul"); |
| 33 | + String brand = scan.next(); |
46 | 34 |
|
47 | | - }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen") |
48 | | - && iceCreamChoice.equalsIgnoreCase("Strawberry")){ |
49 | | - dairyQueenStrawberryIceCream = new DairyQueenStrawberryIceCream(); |
| 35 | + AbstractIceCreamFactory factory = FactoryCreator.getFactory(brand); |
50 | 36 |
|
51 | | - }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen") && |
52 | | - iceCreamChoice.equalsIgnoreCase("Chocolate")){ |
53 | | - dairyQueenChocolateIceCream = new DairyQueenChocolateIceCream(); |
| 37 | + // Virtual constructor ( takes care of configuration ) |
| 38 | + if ( choice.equalsIgnoreCase("icecream")) { |
| 39 | + IceCream iceCream = factory.createIceCream(flavor); |
| 40 | + System.out.println(iceCream); |
| 41 | + }else { |
| 42 | + MilkShake milkShake = factory.createMilkShake(flavor); |
| 43 | + System.out.println(milkShake); |
54 | 44 | } |
55 | 45 |
|
56 | | - // Ice cream of your choice is : |
57 | | - System.out.print("Ice cream of your choice is "); |
58 | | - if (amulStrawberryIceCream != null){ |
59 | | - System.out.println(amulStrawberryIceCream); |
60 | | - } |
61 | | - if (amulchocolateIceCream != null){ |
62 | | - System.out.println(amulchocolateIceCream); |
63 | | - } |
64 | | - if (dairyQueenStrawberryIceCream != null){ |
65 | | - System.out.println(dairyQueenStrawberryIceCream); |
66 | | - } |
67 | | - if (dairyQueenChocolateIceCream != null){ |
68 | | - System.out.println(dairyQueenChocolateIceCream); |
69 | | - } |
| 46 | + // Family of similar products with different brands. ( Tight couple) |
| 47 | +// DairyQueenStrawberryIceCream dairyQueenStrawberryIceCream =null; |
| 48 | +// DairyQueenChocolateIceCream dairyQueenChocolateIceCream = null; |
| 49 | +// AmulStrawberryIceCream amulStrawberryIceCream =null; |
| 50 | +// AmulChocolateIceCream amulchocolateIceCream = null; |
| 51 | + |
| 52 | +// // Sphegati code with if else ladder |
| 53 | +// if (iceCreamBrand.equalsIgnoreCase("Amul") |
| 54 | +// && iceCreamChoice.equalsIgnoreCase("Strawberry")){ |
| 55 | +// amulStrawberryIceCream = new AmulStrawberryIceCream(); |
| 56 | +// |
| 57 | +// }else if (iceCreamBrand.equalsIgnoreCase("Amul") && |
| 58 | +// iceCreamChoice.equalsIgnoreCase("Chocolate")){ |
| 59 | +// amulchocolateIceCream = new AmulChocolateIceCream(); |
| 60 | +// |
| 61 | +// }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen") |
| 62 | +// && iceCreamChoice.equalsIgnoreCase("Strawberry")){ |
| 63 | +// dairyQueenStrawberryIceCream = new DairyQueenStrawberryIceCream(); |
| 64 | +// |
| 65 | +// }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen") && |
| 66 | +// iceCreamChoice.equalsIgnoreCase("Chocolate")){ |
| 67 | +// dairyQueenChocolateIceCream = new DairyQueenChocolateIceCream(); |
| 68 | +// } |
| 69 | +// |
| 70 | +// // Ice cream of your choice is : |
| 71 | +// System.out.print("Ice cream of your choice is "); |
| 72 | +// if (amulStrawberryIceCream != null){ |
| 73 | +// System.out.println(amulStrawberryIceCream); |
| 74 | +// } |
| 75 | +// if (amulchocolateIceCream != null){ |
| 76 | +// System.out.println(amulchocolateIceCream); |
| 77 | +// } |
| 78 | +// if (dairyQueenStrawberryIceCream != null){ |
| 79 | +// System.out.println(dairyQueenStrawberryIceCream); |
| 80 | +// } |
| 81 | +// if (dairyQueenChocolateIceCream != null){ |
| 82 | +// System.out.println(dairyQueenChocolateIceCream); |
| 83 | +// } |
70 | 84 |
|
71 | 85 | } |
72 | 86 | } |
0 commit comments