Created
February 3, 2023 17:17
-
-
Save muhamedoufi/efde2d4350ebff6d97abea10bda17573 to your computer and use it in GitHub Desktop.
Pizza Creation Using Factory Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
PizzaFactory pizzaFactory = new PizzaFactory(); | |
Pizza pizza = pizzaFactory.createPizza("fromage"); | |
pizza.prepare(); | |
pizza.bake(); | |
pizza.cut(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Pizza { | |
void prepare(); | |
void bake(); | |
void cut(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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