Lab 9 OEL
Lab 9 OEL
CMS: 467212
Clas: BESE
Scenario:
We are building a simple stationery shop system where customers can buy items like pencils, registers,
and calculators. Every purchase is logged to a file along with a timestamp. The application uses the
Singleton Design Pattern to ensure that there is only one logger instance responsible for writing logs,
which helps maintain consistent logging and efficient resource usage.
Actors:
Class Diagram:
Item Class:
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
This class creates a single instance for logger that is used to access the file
private PurchaseLogger() {}
Main Application:
import java.util.Scanner;
scanner.close();
}
}
Output:
Logger file:
Scenario:
Building a scalable pizza ordering system where new types of pizzas can be added easily without
modifying core logic. The system uses the Factory Design Pattern to decouple pizza creation from the
ordering process.
This application uses the Factory Design Pattern by separating the creation logic of pizzas from the client
code. The Pizza class is the abstract product, with concrete products like MargheritaPizza, VeggiePizza,
and PepperoniPizza extending it. The PizzaFactory interface acts as the creator, and each concrete
factory class (MargheritaPizzaFactory, VeggiePizzaFactory, PepperoniPizzaFactory) implements it to
create a specific type of pizza. The main application only calls PizzaFactory.createPizza("type"), which
internally delegates the creation task to the appropriate factory without using conditionals, following
the core principle of the Factory Design Pattern.
Actors:
Class Diagram:
PRODUCT CLASSES:
Abstract class that defines the template method makePizza(), which outlines the steps to prepare, bake,
cut, and box a pizza.
MargheritaPizza Class:
Concrete class that implements the Pizza class, representing the Margherita pizza with specific
preparation, baking, cutting, and boxing methods.
Concrete class that implements the Pizza class, representing the Pepperoni pizza with specific
preparation, baking, cutting, and boxing methods.
VeggiePizza Class:
Concrete class that implements the Pizza class, representing the Veggie pizza with specific preparation,
baking, cutting, and boxing methods.
PizzaFactory Interface:
Interface that declares the createPizza() method; also contains a static method to create pizzas based on
the type.
MargheritaPizzaFactory Class:
PepperoniPizzaFactory Class:
MAIN APPLICATION
Main Application:
Application that interacts with the user to take an order, create the selected pizza via the PizzaFactory,
and display the process of making the pizza.
import java.util.Scanner;
if (pizza != null) {
pizza.makePizza();
System.out.println("Your pizza is ready! Enjoy 🍕");
} else {
System.out.println("Order failed. Please try again.");
}
scanner.close();
}
}