package creational;

/**
 * Created by 李恒名 on 2017/6/14.
 * <p>
 * 工厂方法模式
 */
public class FactoryMethodPattern {

    public static void main(String[] args) {
        CarFactory factory = new BenzFactory();
        //CarFactory factory = new BMWFactory();
        Car car = factory.getCar();
        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 abstract class CarFactory {
        abstract Car getCar();
    }

    static class BenzFactory extends CarFactory {
        @Override
        Car getCar() {
            return new Benz();
        }
    }

    static class BMWFactory extends CarFactory {
        @Override
        Car getCar() {
            return new BMW();
        }
    }
}


@@ -111,212 +111,18 @@
 n %7B%0A
-%0A    public static void main(String%5B%5D args) %7B%0A        CarFactory factory = new BenzFactory();%0A        //CarFactory factory = new BMWFactory();%0A        Car car = factory.getCar();%0A        car.run();%0A    %7D%0A
+    //%E6%B1%BD%E8%BD%A6%E6%8A%BD%E8%B1%A1
 %0A   
@@ -177,32 +177,42 @@
 id run();%0A    %7D%0A
+    //%E5%A5%94%E9%A9%B0%E6%B1%BD%E8%BD%A6
 %0A    static clas
@@ -321,32 +321,42 @@
         %7D%0A    %7D%0A
+    //%E5%AE%9D%E9%A9%AC%E6%B1%BD%E8%BD%A6
 %0A    static clas
@@ -464,32 +464,45 @@
         %7D%0A    %7D%0A
+    //%E6%8A%BD%E8%B1%A1%E7%9A%84%E6%B1%BD%E8%BD%A6%E5%B7%A5%E5%8E%82
 %0A    static abst
@@ -525,32 +525,39 @@
 ctory %7B%0A        
+public 
 abstract Car get
@@ -565,24 +565,56 @@
 ar();%0A    %7D%0A
+    //%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    static 
@@ -670,32 +670,39 @@
 verride%0A        
+public 
 Car getCar() %7B%0A 
@@ -747,16 +747,48 @@
 %7D%0A    %7D%0A
+    //%E5%AE%9D%E9%A9%AC%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
@@ -855,16 +855,23 @@
         
+public 
 Car getC
@@ -923,11 +923,214 @@
 %7D%0A    %7D%0A
+    public static void main(String%5B%5D args) %7B%0A        CarFactory factory = new BenzFactory();%0A        //CarFactory factory = new BMWFactory();%0A        Car car = factory.getCar();%0A        car.run();%0A    %7D%0A
 %7D%0A%0A

update > 工厂方法模式