0% found this document useful (0 votes)
4 views11 pages

Lab 9 OEL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Lab 9 OEL

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Department of Software Engineering

SE-211: Software Design and Architecture

Class: BESE 14AB

Lab 9: Open Ended Lab

Date: April 7th, 2025

Instructor: Mehvish Rashid

Name: Ushba Fatima

CMS: 467212

Clas: BESE

SE-211: Software Design and Architecture Page 1


Singleton Design Pattern:

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:

 Customer (User): Initiates a purchase.


 StationeryShop (Main Application): Displays available items and processes orders.
 PurchaseLogger (Singleton): Logs the purchase to a file.
 Item Class: Represents an individual product.

Class Diagram:

SE-211: Software Design and Architecture Page 2


Code:

Item Class:

public class Item {


private String name;
private double price;

public Item(String name, double price) {


this.name = name;
this.price = price;
}

public String getName() {


return name;
}

public double getPrice() {


return price;
}
}

import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;

Purchase Logger Class:

This class creates a single instance for logger that is used to access the file

public class PurchaseLogger {


private static PurchaseLogger instance;
private static final String FILE_NAME = "purchases.txt";

private PurchaseLogger() {}

public static PurchaseLogger getInstance() {


if (instance == null) {
instance = new PurchaseLogger();
}
return instance;
}

SE-211: Software Design and Architecture Page 3


public void logPurchase(String itemName) {
try (FileWriter writer = new FileWriter(FILE_NAME, true)) {
String time = LocalDateTime.now().toString();
writer.write("Purchased: " + itemName + " at " + time + "\
n");
} catch (IOException e) {
System.out.println("Error logging purchase: " +
e.getMessage());
}
}
}

Main Application:

import java.util.Scanner;

public class StationeryShop {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Item[] items = {
new Item("Pencil", 10.0),
new Item("Register", 50.0),
new Item("Calculator", 300.0)
};

System.out.println("Welcome to the Stationery Shop!");


System.out.println("What would you like to buy?");
for (int i = 0; i < items.length; i++) {
System.out.println((i + 1) + ". " + items[i].getName() + "
- Rs " + items[i].getPrice());
}

System.out.print("Enter item number (1-3): ");


int choice = scanner.nextInt();

if (choice >= 1 && choice <= items.length) {


Item selected = items[choice - 1];
System.out.println("You bought a " + selected.getName() +
" for Rs " + selected.getPrice());

PurchaseLogger logger = PurchaseLogger.getInstance();


logger.logPurchase(selected.getName());

System.out.println("Purchase logged successfully.");

SE-211: Software Design and Architecture Page 4


} else {
System.out.println("Invalid choice.");
}

scanner.close();
}
}

Output:

Logger file:

SE-211: Software Design and Architecture Page 5


Factory Design Pattern:

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:

 Customer (User): Initiates the order.


 Main Application (Client): Accepts pizza type and delegates creation.
 PizzaFactory Interface: Routes to the correct concrete factory.
 Concrete Pizza Factories: Create their specific pizza types.
 Pizza Classes: Implement pizza behavior (prepare, bake, cut, box).

Class Diagram:

SE-211: Software Design and Architecture Page 6


Code:

PRODUCT CLASSES:

Pizza Abstract Class:

Abstract class that defines the template method makePizza(), which outlines the steps to prepare, bake,
cut, and box a pizza.

public abstract class Pizza {


public abstract void prepare();
public abstract void bake();
public abstract void cut();
public abstract void box();

public final void makePizza() {


prepare();
bake();
cut();
box();
}
}

MargheritaPizza Class:

Concrete class that implements the Pizza class, representing the Margherita pizza with specific
preparation, baking, cutting, and boxing methods.

public class MargheritaPizza extends Pizza {


public void prepare() {
System.out.println("Preparing Margherita Pizza: tomato sauce
and cheese.");
}
public void bake() {
System.out.println("Baking Margherita Pizza at 380 degrees.");
}
public void cut() {
System.out.println("Cutting Margherita Pizza into 8 slices.");
}
public void box() {
System.out.println("Boxing Margherita Pizza.");
}
}

SE-211: Software Design and Architecture Page 7


PepperoniPizza Class:

Concrete class that implements the Pizza class, representing the Pepperoni pizza with specific
preparation, baking, cutting, and boxing methods.

public class PepperoniPizza extends Pizza {


public void prepare() {
System.out.println("Preparing Pepperoni Pizza: tomato sauce,
cheese, and pepperoni.");
}
public void bake() {
System.out.println("Baking Pepperoni Pizza at 375 degrees.");
}
public void cut() {
System.out.println("Cutting Pepperoni Pizza into 8 slices.");
}
public void box() {
System.out.println("Boxing Pepperoni Pizza.");
}
}

VeggiePizza Class:

Concrete class that implements the Pizza class, representing the Veggie pizza with specific preparation,
baking, cutting, and boxing methods.

public class VeggiePizza extends Pizza {


public void prepare() {
System.out.println("Preparing Veggie Pizza: tomato sauce,
cheese, peppers, onions, olives.");
}
public void bake() {
System.out.println("Baking Veggie Pizza at 360 degrees.");
}
public void cut() {
System.out.println("Cutting Veggie Pizza into 6 slices.");
}
public void box() {
System.out.println("Boxing Veggie Pizza.");
}
}

SE-211: Software Design and Architecture Page 8


CREATOR CLASSES:

PizzaFactory Interface:

Interface that declares the createPizza() method; also contains a static method to create pizzas based on
the type.

public interface PizzaFactory {


Pizza createPizza();

static Pizza createPizza(String type) {


return switch (type.toLowerCase()) {
case "pepperoni" -> new
PepperoniPizzaFactory().createPizza();
case "margherita" -> new
MargheritaPizzaFactory().createPizza();
case "veggie" -> new VeggiePizzaFactory().createPizza();
default -> {
System.out.println("Sorry, we don't have that
pizza.");
yield null;
}
};
}
}

MargheritaPizzaFactory Class:

Concrete class implementing PizzaFactory to create a Margherita pizza.

public class MargheritaPizzaFactory implements PizzaFactory {


public Pizza createPizza() {
return new MargheritaPizza();
}
}

PepperoniPizzaFactory Class:

Concrete class implementing PizzaFactory to create a Pepperoni pizza.

public class PepperoniPizzaFactory implements PizzaFactory {


public Pizza createPizza() {
return new PepperoniPizza();
}
}

SE-211: Software Design and Architecture Page 9


VeggiePizzaFactory Class:

Concrete class implementing PizzaFactory to create a Veggie pizza.

public class VeggiePizzaFactory implements PizzaFactory {


public Pizza createPizza() {
return new VeggiePizza();
}
}

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Pizza Factory!");
System.out.println("Available Pizzas: Margherita, Pepperoni,
Veggie");

System.out.print("Enter the type of pizza you want: ");


String input = scanner.nextLine();

Pizza pizza = PizzaFactory.createPizza(input);

if (pizza != null) {
pizza.makePizza();
System.out.println("Your pizza is ready! Enjoy 🍕");
} else {
System.out.println("Order failed. Please try again.");
}

scanner.close();
}
}

SE-211: Software Design and Architecture Page 10


Output:

SE-211: Software Design and Architecture Page 11

You might also like