0% found this document useful (0 votes)
1 views2 pages

Java Design Patterns Part1

The document explains two design patterns: the Singleton Pattern and the Factory Method Pattern. The Singleton Pattern ensures a single instance of a class with a global access point, exemplified by a PrinterSpooler class. The Factory Method Pattern allows subclasses to determine the type of objects created, illustrated through a PizzaFactory that produces different types of pizzas based on client requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Java Design Patterns Part1

The document explains two design patterns: the Singleton Pattern and the Factory Method Pattern. The Singleton Pattern ensures a single instance of a class with a global access point, exemplified by a PrinterSpooler class. The Factory Method Pattern allows subclasses to determine the type of objects created, illustrated through a PizzaFactory that produces different types of pizzas based on client requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Singleton Pattern
Definition: Ensures only one instance of a class is created and provides a global point of access to

it.

Analogy: Imagine a print spooler - your computer must have one and only one print manager to

handle all printing jobs.

Code Example:

class PrinterSpooler {
private static PrinterSpooler instance = new PrinterSpooler();
private PrinterSpooler() {
System.out.println("Spooler Initialized.");
}
public static PrinterSpooler getInstance() {
return instance;
}
public void print(String doc) {
System.out.println("Printing: " + doc);
}
}

public class Main {


public static void main(String[] args) {
PrinterSpooler spooler = PrinterSpooler.getInstance();
spooler.print("DesignPatterns.pdf");
}
}

When to Use: Logging, Configuration Manager, Database connection pool

2. Factory Method Pattern


Definition: Provides an interface for creating objects in a superclass, but allows subclasses to alter

the type of objects that will be created.

Analogy: Think of a pizza store. You order a pizza, and the store decides whether to give you a Veg

or a Non-Veg pizza.

Code Example:

interface Pizza {
void prepare();
}

class VegPizza implements Pizza {


public void prepare() {
System.out.println("Preparing Veg Pizza");
}
}

class ChickenPizza implements Pizza {


public void prepare() {
System.out.println("Preparing Chicken Pizza");
}
}

class PizzaFactory {
public Pizza getPizza(String type) {
if (type.equalsIgnoreCase("veg")) return new VegPizza();
else if (type.equalsIgnoreCase("chicken")) return new ChickenPizza();
return null;
}
}

public class Main {


public static void main(String[] args) {
PizzaFactory factory = new PizzaFactory();
Pizza pizza = factory.getPizza("veg");
pizza.prepare();
}
}

When to Use: When object creation logic needs to be hidden from the client.

You might also like