• 简单工厂模式和工厂方法模式


    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();
            }
        }
        /**
        运行结果:
        	我是五菱宏光
            我是玛莎拉蒂
            我是特斯拉
        */
      
  • 相关阅读:
    LeetCode 301. Remove Invalid Parentheses
    LeetCode 126. Word Ladder II
    LeetCode 44. Wildcard Matching
    LeetCode 10. Regular Expression Matching
    LeetCode 65. Valid Number
    LeetCode 149. Max Points on a Line
    LeetCode 68. Text Justification
    LeetCode 212. Word Search II
    LeetCode 79. Word Search
    LeetCode 218. The Skyline Problem
  • 原文地址:https://www.cnblogs.com/wfszmg/p/13550775.html
Copyright © 2020-2023  润新知