Singleton-Multiton-Factory
Directory - Designing a Pizza Ordering System Using Creational Design PatternsThe Singleton-Multiton-Factory
directory demonstrates how to implement creational design patterns—specifically the Singleton, Multiton, and Factory Method patterns—in the context of a pizza ordering system. By modeling ingredients, pizzas, and specialized pizza factories, this directory highlights the flexibility and power of design patterns in controlling the instantiation process and creating modular, reusable components.
This directory is organized into three phases:
PizzaImpl
class to represent fully customizable pizzas, integrating the relationships between size, crust, sauce, cheese, and toppings.PizzaFactory
to automate the creation of predefined pizza types using the Factory Method pattern.IngredientImpl
and SubclassesThe IngredientImpl
class implements the Ingredient
interface, encapsulating the name of the ingredient, its vegetarian status, and its vegan status. This class is immutable, ensuring ingredient properties cannot change after creation. Specialized subclasses—Crust
, Sauce
, Cheese
, and Topping
—use the Multiton Design Pattern to manage specific predefined ingredient instances.
Each subclass defines public static final
fields to represent available options. For example:
Crust
: Options include HAND_TOSSED
, THIN
, and DEEP_DISH
.Sauce
: Options include TOMATO
, BARBECUE
, PESTO
, ALFREDO
, and RANCH
.Cheese
: Options include MOZZARELLA
, BLEND
, and VEGAN
.Topping
: A wide array of toppings like MUSHROOMS
, PEPPERONI
, ONION
, and BACON
.Multiton Design Pattern:
This setup allows for efficient and reusable ingredient management while maintaining immutability.
PizzaImpl
ClassPizzaImpl
The PizzaImpl
class models a pizza as a composition of:
Size
(small, medium, or large) represented by an enum
.Crust
instance.Sauce
instance.Cheese
instance.Topping
instances.This class implements the Pizza
interface and is also immutable, ensuring pizzas cannot be altered after creation.
Key features include:
getPrice()
method calculates the pizza’s price based on its size and number of toppings:
This class represents the core of the pizza ordering system, offering flexibility while adhering to clean design principles.
PizzaFactory
The PizzaFactory
class automates the creation of specific pizza types using the Factory Method Design Pattern. By centralizing pizza creation, this class ensures consistency and reusability while simplifying client code.
Key factory methods include:
makeCheesePizza(Size size)
: Creates a cheese pizza with hand-tossed crust, tomato sauce, and cheese blend.makeHawaiianPizza(Size size)
: Creates a Hawaiian pizza with hand-tossed crust, tomato sauce, cheese blend, ham, and pineapple.makeMeatLoversPizza(Size size)
: Creates a meat lover’s pizza with deep-dish crust, tomato sauce, cheese blend, and toppings like pepperoni, sausage, bacon, and ground beef.makeVeggieSupremePizza(Size size)
: Creates a veggie pizza with thin crust, tomato sauce, cheese blend, and toppings like sun-dried tomato, mushrooms, green peppers, and olives.makeDailySpecialPizza()
: Creates a unique pizza defined by the developer (e.g., favorite size and toppings).Factory Method Design Pattern:
Each factory method outputs a fully configured Pizza
instance, ensuring that client code does not need to manage ingredient combinations manually.
Singleton-Multiton-Factory
DirectoryThis directory exemplifies the creational design patterns within the context of a pizza ordering system. It demonstrates how to control instantiation effectively and how to structure modular, reusable components for complex systems.
IngredientImpl
and PizzaImpl
ensure data integrity by making instances immutable, preventing accidental modifications.By integrating these patterns, the directory highlights the elegance and power of design patterns in creating a robust, scalable system for real-world applications like a pizza shop.