package creational;

/**
 * Created by 李恒名 on 2017/6/14.
 * <p>
 * 简单工厂模式
 */
public class SimpleFactoryPattern {

    public static void main(String[] args) {
        CarFactory factory = new CarFactory();
        Car car = factory.getCar("Benz");
        //Car car = factory.getCar("BMW");
        car.run();
    }

    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 class CarFactory {
        Car getCar(String name) {
            if (name.equals("Benz")) {
                return new Benz();
            } else if (name.equals("BMW")) {
                return new BMW();
            } else {
                throw new IllegalArgumentException("No support this car");
            }
        }
    }
}


@@ -111,211 +111,18 @@
 n %7B%0A
-%0A    public static void main(String%5B%5D args) %7B%0A        CarFactory factory = new CarFactory();%0A        Car car = factory.getCar(%22Benz%22);%0A        //Car car = factory.getCar(%22BMW%22);%0A        car.run();%0A    %7D%0A
+    //%E6%B1%BD%E8%BD%A6%E6%8A%BD%E8%B1%A1
 %0A   
@@ -181,25 +181,35 @@
 un();%0A    %7D%0A
+    //%E5%A5%94%E9%A9%B0%E6%B1%BD%E8%BD%A6
 %0A
-
     static c
@@ -321,33 +321,43 @@
         %7D%0A    %7D%0A
+    //%E5%AE%9D%E9%A9%AC%E6%B1%BD%E8%BD%A6
 %0A
-
     static class
@@ -472,17 +472,38 @@
 %7D%0A    %7D%0A
+    //%E6%B1%BD%E8%BD%A6%E5%B7%A5%E5%8E%82%EF%BC%8C%E6%A0%B9%E6%8D%AE%E6%B1%BD%E8%BD%A6%E5%90%8D%E7%A7%B0%E5%88%9B%E5%BB%BA%E6%B1%BD%E8%BD%A6
 %0A
-
     stat
@@ -817,24 +817,24 @@
           %7D%0A
-
         %7D%0A  
@@ -836,12 +836,215 @@
  %7D%0A    %7D
+%0A%0A    public static void main(String%5B%5D args) %7B%0A        CarFactory factory = new CarFactory();%0A        Car car = factory.getCar(%22Benz%22);%0A        //Car car = factory.getCar(%22BMW%22);%0A        car.run();%0A    %7D
 %0A%7D%0A%0A

update > 简单工厂模式