Factory Design Pattern
Ganesh Manchi
Full Stack @TCS ? | Ex-SDE @ZopSmart ?? ( Go | Java | React ) ??♂? | 1900+ Knight @Leetcode ?? | Open Source @ Layer5 ?? | CS Engineer ??
One sunny morning, Alice and Bob met at a bustling factory.
Alice: Hi Bob, what brings you to this factory today?
Bob: Hey Alice, I'm here for a tour. This place is fascinating! What about you?
Alice: I'm also came here for tour. I am remembering Factory Design Pattern by watching these.
Bob: Factory Design Pattern? That sounds interesting. What's it all about?
Alice: The Factory Design Pattern is a creational design pattern that provides an interface for creating objects in a super class but allows subclasses to alter the type of objects that will be created.
Bob: Can you explain that with an example?
Alice: Sure! Imagine this factory we're in produces different types of vehicles: cars, trucks, and motorcycles. Instead of having a client code that directly creates these vehicle objects, we use a factory to handle this creation process. This makes the code more flexible and easier to maintain.
Bob: That sounds like a smart way to manage object creation. How would we implement this in code?
Alice: Let’s start by defining a Vehicle interface and specific vehicle classes like Car, Truck, and Motorcycle.
interface Vehicle {
void drive();
}
class Car implements Vehicle {
@Override
public void drive() {
System.out.println("Driving a car");
}
}
class Truck implements Vehicle {
@Override
public void drive() {
System.out.println("Driving a truck");
}
}
class Motorcycle implements Vehicle {
@Override
public void drive() {
System.out.println("Riding a motorcycle");
}
}
Bob: Okay, so these classes implement the Vehicle interface. How does the factory come into play?
领英推荐
Alice: Now, we'll create a VehicleFactory that produces these vehicles based on input.
class VehicleFactory {
public Vehicle createVehicle(String type) {
switch (type) {
case "car":
return new Car();
case "truck":
return new Truck();
case "motorcycle":
return new Motorcycle();
default:
throw new IllegalArgumentException("Unknown vehicle type");
}
}
}
Bob: So, the factory decides which vehicle to create based on the type provided. This keeps the client code clean and decoupled from the specific vehicle classes.
Alice: Exactly! Here's how you can use the VehicleFactory in a client code.
public class Main {
public static void main(String[] args) {
VehicleFactory factory = new VehicleFactory();
Vehicle car = factory.createVehicle("car");
car.drive();
Vehicle truck = factory.createVehicle("truck");
truck.drive();
Vehicle motorcycle = factory.createVehicle("motorcycle");
motorcycle.drive();
}
}
Bob: I see. This way, if we need to add a new vehicle type, we just update the factory, and the client code remains unchanged.
Alice: Precisely. This pattern adheres to the Open/Closed Principle, allowing the code to be open for extension but closed for modification.
Bob: It's a powerful way to manage object creation. I'll definitely consider using the Factory Design Pattern in my projects.
Alice: Great! If you have any more questions or need further examples, feel free to ask.
Alice and Bob finished their tour of the factory, excited to implement the Factory Design Pattern in their future projects...
Until next time, signing off with ? Ganesh