From 5faafbeda268cd15a15b50c703555559ec0055a0 Mon Sep 17 00:00:00 2001 From: Hammy Date: Tue, 22 Dec 2020 17:07:24 +0000 Subject: [PATCH] Implement Another Implementation of Factory Design Pattern --- .../src/factory/coffee/Coffee.java | 55 +++++++++++++++++++ .../src/factory/coffee/CoffeeFactory.java | 14 +++++ .../src/factory/coffee/Cortado.java | 18 ++++++ Design Patterns/src/factory/coffee/Latte.java | 18 ++++++ Design Patterns/src/factory/coffee/Main.java | 16 ++++++ .../src/factory/coffee/MoonBucks.java | 21 +++++++ 6 files changed, 142 insertions(+) create mode 100644 Design Patterns/src/factory/coffee/Coffee.java create mode 100644 Design Patterns/src/factory/coffee/CoffeeFactory.java create mode 100644 Design Patterns/src/factory/coffee/Cortado.java create mode 100644 Design Patterns/src/factory/coffee/Latte.java create mode 100644 Design Patterns/src/factory/coffee/Main.java create mode 100644 Design Patterns/src/factory/coffee/MoonBucks.java diff --git a/Design Patterns/src/factory/coffee/Coffee.java b/Design Patterns/src/factory/coffee/Coffee.java new file mode 100644 index 0000000..93fdb9d --- /dev/null +++ b/Design Patterns/src/factory/coffee/Coffee.java @@ -0,0 +1,55 @@ +package factory.coffee; + +public abstract class Coffee { + + private String name; + private String milk; + private String beans; + + public void grindBeans(){ System.out.println("Grinding beans....");} + + public void steamMilk(){ System.out.println("Steaming milk...."); } + + public void serveCoffee(){ + System.out.println("Serving coffee...."); + } + + public void pourShot(){ + System.out.println("Shot of coffee...."); + } + + public void getCup(){ + System.out.println("Cup selected...."); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMilk() { + return milk; + } + + public void setMilk(String milk) { + this.milk = milk; + } + + public String getBeans() { + return beans; + } + + public void setBeans(String beans) { this.beans = beans; } + + @Override + public String toString() { + return "Coffee{" + + "name='" + name + '\'' + + ", milk='" + milk + '\'' + + ", beans='" + beans + '\'' + + '}'; + } +} diff --git a/Design Patterns/src/factory/coffee/CoffeeFactory.java b/Design Patterns/src/factory/coffee/CoffeeFactory.java new file mode 100644 index 0000000..925f6d7 --- /dev/null +++ b/Design Patterns/src/factory/coffee/CoffeeFactory.java @@ -0,0 +1,14 @@ +package factory.coffee; + +public class CoffeeFactory { + + public CoffeeFactory(){ } + + public Coffee createCoffee(String item) { + return switch (item) { + case "cortado" -> new Cortado(); + case "latte" -> new Latte(); + default -> null; + }; + } +} diff --git a/Design Patterns/src/factory/coffee/Cortado.java b/Design Patterns/src/factory/coffee/Cortado.java new file mode 100644 index 0000000..96b9c97 --- /dev/null +++ b/Design Patterns/src/factory/coffee/Cortado.java @@ -0,0 +1,18 @@ +package factory.coffee; + +public class Cortado extends Coffee{ + + public Cortado() { + setName("Cortado"); + setBeans("Columbian"); + setMilk("Semi Skimmed Milk"); + } + + @Override + public void grindBeans() { + System.out.println("Grinding Beans Extra fine...."); + } + +} + + diff --git a/Design Patterns/src/factory/coffee/Latte.java b/Design Patterns/src/factory/coffee/Latte.java new file mode 100644 index 0000000..cd82ffe --- /dev/null +++ b/Design Patterns/src/factory/coffee/Latte.java @@ -0,0 +1,18 @@ +package factory.coffee; + +public class Latte extends Coffee{ + + public Latte() { + setName("Latte"); + setBeans("Kopi luwak"); + setMilk("Almond Milk"); + } + + @Override + public void pourShot() { + System.out.println("Double shot of coffee....."); + } + + @Override + public void steamMilk() { System.out.println("Steaming Almond Milk...."); } +} diff --git a/Design Patterns/src/factory/coffee/Main.java b/Design Patterns/src/factory/coffee/Main.java new file mode 100644 index 0000000..f7a436e --- /dev/null +++ b/Design Patterns/src/factory/coffee/Main.java @@ -0,0 +1,16 @@ +package factory.coffee; + +public class Main { + + public static void main(String[] args) { + MoonBucks moonBucks = new MoonBucks(); + + Coffee latte = moonBucks.orderCoffee("latte"); + System.out.println("-------------------------------------\nLatte Was Ordered: " + latte); + + System.out.println(); + + Coffee cortado = moonBucks.orderCoffee("cortado"); + System.out.println("-------------------------------------\nCortado Was Ordered: " + cortado); + } +} diff --git a/Design Patterns/src/factory/coffee/MoonBucks.java b/Design Patterns/src/factory/coffee/MoonBucks.java new file mode 100644 index 0000000..96283a6 --- /dev/null +++ b/Design Patterns/src/factory/coffee/MoonBucks.java @@ -0,0 +1,21 @@ +package factory.coffee; + +public class MoonBucks { + + public Coffee orderCoffee(String type){ + + CoffeeFactory coffeeFactory = new CoffeeFactory(); + + Coffee coffee = coffeeFactory.createCoffee(type); + System.out.println("-------Brewing a " + coffee.getName() + "-------"); + coffee.grindBeans(); + coffee.steamMilk(); + coffee.getCup(); + coffee.pourShot(); + coffee.serveCoffee(); + + return coffee; + + } + +}