package creational;

/**
 * Created by 李恒名 on 2017/6/14.
 * <p>
 * 工厂方法模式
 */
public class FactoryMethodPattern {
    //汽车抽象
    static abstract class Car {
        abstract void run();
    }
    //奔驰汽车
    static class Benz extends Car {

        @Override
        void run() {
            System.out.println("奔驰车在跑");
        }
    }
    //宝马汽车
    static class BMW extends Car {

        @Override
        void run() {
            System.out.println("宝马车在跑");
        }
    }
    //抽象的汽车工厂
    static abstract class CarFactory {
        public abstract Car getCar();
    }
    //奔驰汽车工厂，getCar()方法可以修改为静态方法
    static class BenzFactory extends CarFactory {
        @Override
        public Car getCar() {
            return new Benz();
        }
    }
    //宝马汽车工厂，getCar()方法可以修改为静态方法
    static class BMWFactory extends CarFactory {
        @Override
        public Car getCar() {
            return new BMW();
        }
    }
    public static void main(String[] args) {
        CarFactory factory = new BenzFactory();
        //CarFactory factory = new BMWFactory();
        Car car = factory.getCar();
        car.run();
    }
}


@@ -581,36 +581,16 @@
 //%E5%A5%94%E9%A9%B0%E6%B1%BD%E8%BD%A6%E5%B7%A5%E5%8E%82
-%EF%BC%8CgetCar()%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E4%B8%BA%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95
 %0A    sta
@@ -743,28 +743,8 @@
 %E6%B1%BD%E8%BD%A6%E5%B7%A5%E5%8E%82
-%EF%BC%8CgetCar()%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E4%B8%BA%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95
 %0A   

update > 工厂方法模式