Last active
February 18, 2022 11:47
-
-
Save garudareiga/4bc01df870e1c1b87843 to your computer and use it in GitHub Desktop.
Design Patterns in Java
This file contains hidden or 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
// ingredients: dough, sauce | |
public interface Dough {}; | |
public interface Cheese {}; | |
public interface Pepperoni {}; | |
public class ThinCrustDough implements Dough { | |
public String toString() { return "This Crust Dough"; } | |
} | |
public class ParmesanCheese implements Cheese { | |
public String toString() { return "Shredded Parmesan"; } | |
} | |
public class SlicedPepperoni implements Pepperoni { | |
public String toString() { return "Sliced Pepperoni"; } | |
} | |
// abstract ingredient factory | |
public interface PizzaIngredientFactory { | |
public Dough createDough(); | |
public Cheese createCheese(); | |
public Pepperoni createPepperoni(); | |
} | |
public class NYPizzaIngredientFactory implements PizzaIngredientFactory { | |
public Dough createDough() { return new ThinCrustDough(); } | |
public Sauce createCheese() { return new ParmesanCheese(); } | |
public Pepperoni createPepperoni() { return new SlicedPepperoni(); } | |
} | |
//The Product classes | |
public abstract class Pizza { | |
String name; | |
String dough; | |
String cheese; | |
String pepperoni; | |
public void setName(String name) { this.name = name; } | |
public String getName() { return name; } | |
abstract void prepare(); | |
public void bake() { System.out.println("Baking " + name); } | |
public String toString() { | |
StringBuffer sb = new StringBuffer(); | |
sb.append(name + ": "); | |
if (dough != null) sb.append(dough + " "); | |
if (cheese != null) sb.append(cheese + " "); | |
if (pepperoni != null) sb.append(pepperoni + " "); | |
return sb.toString(); | |
} | |
} | |
public class CheesePizza extends Pizza { | |
PizzaIngredientFactory ingredientFactory; | |
public CheesePizza(PizzaIngredientFactory ingredientFactory) { | |
this.ingredientFactory = ingredientFactory; | |
} | |
void prepare() { | |
System.out.println("Preparing " + name); | |
dough = ingredientFactory.createDough(); | |
cheese = ingredientFactory.createCheese(); | |
} | |
} | |
public class PepperoniPizza extends Pizza { | |
PizzaIngredientFactory ingredientFactory; | |
public PepperoniPizza(PizzaIngredientFactory ingredientFactory) { | |
this.ingredientFactory = ingredientFactory; | |
} | |
void prepare() { | |
System.out.println("Preparing " + name); | |
dough = ingredientFactory.createDough(); | |
cheese = ingredientFactory.createCheese(); | |
pepperoni = ingredientFactory.createPepperoni(); | |
} | |
} | |
// Client code | |
public abstract class PizzaStore { | |
public Pizza orderPizza(String type) { | |
Pizza pizza = createPizza(type); | |
pizza.prepare(); | |
pizza.bake(); | |
return pizza; | |
} | |
// A factory method handles object creation and encapsulates it in a subclass | |
protected abstract Pizza createPizza(String type); | |
} | |
public class NYPizzaStore extends PizzaStore { | |
protected Pizza createPizza(String type) { | |
Pizza pizza = null; | |
PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); | |
if (item.equalsIgnoreCase("cheese")) { | |
pizza = new CheesePizza(ingredientFactory); | |
pizza.setName("New York Style Cheese Pizza"); | |
} else if (item.equalsIgnoreCase("pepperoni")) { | |
pizza = new PepperoniPizza(ingredientFactory); | |
pizza.setName("New York Style Pepperoni Pizza"); | |
} | |
return pizza; | |
} | |
} |
This file contains hidden or 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
//The Product classes | |
public abstract class Pizza { | |
String name; | |
public String getName() { return name; } | |
public void prepare() { System.out.println("Preparing " + name); } | |
public void bake() { System.out.println("Baking " + name); } | |
} | |
public class NYStyleCheesePizza extends Pizza { | |
public NYStyleCheesePizza() { | |
name = "NY Style Cheese Pizza"; | |
} | |
} | |
public class NYStyleVeggiePizza extends Pizza { | |
public NYStyleVeggiePizza() { | |
name = "NY Style Veggie Pizza"; | |
} | |
} | |
//The Creator classes | |
public abstract class PizzaStore { | |
public Pizza orderPizza(String type) { | |
Pizza pizza = createPizza(type); | |
pizza.prepare(); | |
pizza.bake(); | |
return pizza; | |
} | |
// A factory method handles object creation and encapsulates it in a subclass | |
protected abstract Pizza createPizza(String type); | |
} | |
public class NYPizzaStore extends PizzaStore { | |
Pizza createPizza(String type) { | |
Pizza pizza = null; | |
if (type.equalsIgnoreCase("cheese")) { | |
pizza = new NYStyleCheesePizza(); | |
} else if (type.equalsIgnoreCase("veggie")) { | |
pizza = new NYStyleVeggiePizza(); | |
} | |
return pizza; | |
} | |
} |
This file contains hidden or 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 abstract class Pizza { | |
String name; | |
public String getName() { return name; } | |
public void prepare() { System.out.println("Preparing " + name); } | |
public void bake() { System.out.println("Baking " + name); } | |
} | |
public class CheesePizza extends Pizza { | |
public CheesePizza() { name = "CheesePizza"; } | |
} | |
public class VeggiePizza extends Pizza { | |
public VeggiePizza() { name = "VeggiePizza"; } | |
} | |
public class PizzaFactory { | |
public Pizza createPizza(String type) { | |
Pizza pizza = null; | |
if (type.equalsIgnoreCase("cheese")) { | |
pizza = new CheesePizza(); | |
} else if (type.equalsIgnoreCase("veggie")) { | |
pizza = new VeggiePizza(); | |
} | |
return pizza; | |
} | |
} | |
// Factory client code | |
public class PizzaStore { | |
PizzaFactory factory; | |
public PizzaStore(PizzaFactory factory) { | |
this.factory = factory; | |
} | |
public Pizza orderPizza(String type) { | |
Pizza pizza = factory.createPizza(type); | |
pizza.prepare(); | |
pizza.bake(); | |
return pizza; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment