1. 程式人生 > 實用技巧 >簡單工廠模式和工廠方法模式

簡單工廠模式和工廠方法模式

1、簡單工廠模式

簡單工廠模式又稱靜態工廠模式

簡單工廠模式的實質:一個工廠類根據傳入的引數,動態決定應該建立哪一類產品類(這些產品類均繼承自一個父類或介面)例項。

比如使用者買車,要先去4S店就看車,4S店又要從車廠進貨。

  1. 傳統方式

    • 介面Car
    public interface Car {
        void name();
    }
    
    • Car的實現類->Maserati類
    public class Maserati implements Car{
        @Override
        public void name() {
            System.out.println("我是瑪莎拉蒂");
        }
    }
    
    • Car的實現類->WuLing類
    public class WuLing implements Car{
        @Override
        public void name() {
            System.out.println("我是五菱巨集光");
        }
    }
    
    • CarFactory
    public class CarFactory {
        //方法一
    //    public static Car getCar(String name){
    //        if("WuLing".equals(name)){
    //            return new WuLing();
    //        }else if("MSLD".equals(name)){
    //            return new MSLD();
    //        }else{
    //            return null;
    //        }
    //    }
    
        //方法二
        public static Car getWuLing(){
            return new WuLing();
        }
        public static Car getMSLD(){
            return new Maserati();
        }
    }
    
    • 使用者Customer
    public class Customer {
        public static void main(String[] args) {
            //法一對應的程式碼
    //        Car car1 = CarFactory.getCar("");
    //        Car car2 = CarFactory.getCar("WuLing");
            
            //法二對應的程式碼
            Car car1 = CarFactory.getWuLing();
            final Car car2 = CarFactory.getMSLD();
            try {
                car1.name();
                car2.name();
            } catch (NullPointerException e){
                System.out.println("不存在該車");
            }
        }
    }
    /**
    執行結果就是 :
    	我是五菱巨集光
    	我是瑪莎拉蒂
    */
    

2、工廠方法(Factory Method)

public interface Car {
    void name();
}
public interface CarFactory {
    Car getCar();
}
public class Maserati implements Car{
    @Override
    public void name() {
        System.out.println("我是瑪莎拉蒂");
    }
}

public class MaseratiFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Maserati();
    }
}

public class Tesla implements Car{
    @Override
    public void name() {
        System.out.println("我是特斯拉");
    }
}
public class TeslaFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new Tesla();
    }
}
public class WuLing implements Car{
    @Override
    public void name() {
        System.out.println("我是五菱巨集光");
    }
}
public class WuLingFactory implements CarFactory{
    @Override
    public Car getCar() {
        return new WuLing();
    }
}

  •   public class Customer {
          public static void main(String[] args) {
              Car car1 = new WuLingFactory().getCar();
              Car car2 = new MaseratiFactory().getCar();
              Car car3 = new TeslaFactory().getCar();
              car1.name();;
              car2.name();
              car3.name();
          }
      }
      /**
      執行結果:
      	我是五菱巨集光
          我是瑪莎拉蒂
          我是特斯拉
      */