Factory Design Pattern
Exploring the Factory Design Pattern: Alice and Bob at the Factory

Factory Design Pattern

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

要查看或添加评论,请登录

Ganesh Manchi的更多文章

  • Facade Design Pattern

    Facade Design Pattern

    Alice and Bob are watching a movie at Bob's place, enjoying the latest action flick on Bob's impressive home theater…

  • Decorator Design Pattern

    Decorator Design Pattern

    One sunny afternoon, Alice and Bob found themselves at their favorite coffee shop. Alice: Hi Bob, how's your day going?…

    2 条评论
  • Observer Design Pattern

    Observer Design Pattern

    One sunny afternoon, Alice and Bob decided to meet at their favorite park. Alice: Hi Bob, how's everything going? Bob:…

  • Strategy Design Pattern

    Strategy Design Pattern

    Bob came to Alice's house, and they have been playing Mortal Kombat for two hours. Alice: Hey Bob, we've played a lot…

  • Builder Design Pattern

    Builder Design Pattern

    Alice and Bob find themselves at a construction site once again. Alice : Hi Bob, what are you doing in this…

  • Singleton Design Pattern

    Singleton Design Pattern

    One day, Alice and Bob meet at a coffee shop. Alice : Hi Bob, how are you doing? Bob : I'm good.

    2 条评论
  • Coding culture in colleges :(

    Coding culture in colleges :(

    Hello, World! Note: If your college has a good coding culture, then skip this article. I want to share what I've…

    5 条评论
  • DSA vs CP

    DSA vs CP

    Hello World! One of the most confusing topics that often crosses our minds is the choice between Data Structures and…

社区洞察

其他会员也浏览了