package com.greenorange.promotion;

import org.junit.jupiter.api.Test;

public class FactoryDemo {

    @Test
    public void ClientTest() {
        CarFactory bmwFactory = new BMWFactory();
        Car bmw = bmwFactory.createCar();
        bmw.drive(); // 输出: Driving a BMW car

        CarFactory benzFactory = new BenzFactory();
        Car benz = benzFactory.createCar();
        benz.drive(); // 输出: Driving a Benz car
    }


}




interface Car {
    void drive();
}

class BMWCar implements Car {
    @Override
    public void drive() {
        System.out.println("Driving a BMW car");
    }
}

class BenzCar implements Car {
    @Override
    public void drive() {
        System.out.println("Driving a Benz car");
    }
}

interface CarFactory {
    Car createCar();
}


class BMWFactory implements CarFactory {
    @Override
    public Car createCar() {
        return new BMWCar();
    }
}

class BenzFactory implements CarFactory {
    @Override
    public Car createCar() {
        return new BenzCar();
    }
}
