Implement Another Implementation of Factory Design Pattern

master
Hammy 4 years ago
parent 484f3afd9d
commit 5faafbeda2

@ -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 + '\'' +
'}';
}
}

@ -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;
};
}
}

@ -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....");
}
}

@ -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...."); }
}

@ -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);
}
}

@ -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;
}
}
Loading…
Cancel
Save