Skip to content

Instantly share code, notes, and snippets.

@muhamedoufi
Created February 3, 2023 17:17
Show Gist options
  • Save muhamedoufi/efde2d4350ebff6d97abea10bda17573 to your computer and use it in GitHub Desktop.
Save muhamedoufi/efde2d4350ebff6d97abea10bda17573 to your computer and use it in GitHub Desktop.
Pizza Creation Using Factory Method
class CheesePizza implements Pizza {
@Override
public void prepare() {
System.out.println("Préparation de la pizza au fromage");
}
@Override
public void bake() {
System.out.println("Cuisson de la pizza au fromage");
}
@Override
public void cut() {
System.out.println("Coupe de la pizza au fromage");
}
}
public class Main {
public static void main(String[] args) {
PizzaFactory pizzaFactory = new PizzaFactory();
Pizza pizza = pizzaFactory.createPizza("fromage");
pizza.prepare();
pizza.bake();
pizza.cut();
}
}
class PepperoniPizza implements Pizza {
@Override
public void prepare() {
System.out.println("Préparation de la pizza pepperoni");
}
@Override
public void bake() {
System.out.println("Cuisson de la pizza pepperoni");
}
@Override
public void cut() {
System.out.println("Coupe de la pizza pepperoni");
}
}
interface Pizza {
void prepare();
void bake();
void cut();
}
class PizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals("fromage")) {
pizza = new CheesePizza();
} else if (type.equals("pepperoni")) {
pizza = new PepperoniPizza();
}
return pizza;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment