工厂模式

2022-11-30  本文已影响0人  古早味蛋糕

所谓的工厂,通俗来讲就是用来生产产品的地方。从代码的角度来说,产品就是一个个具体的类的实例对象,工厂也是一个实例对象,用来生产这些实例产品。工厂模式要解决的问题就是实例化对象。

一、 简单工厂
1. 优点

工厂类承担创建所有产品的职责,只要有想创建的产品实例,都可以在工厂类里面实现,工厂类号称“万能类”。

2. 缺点
3. 示例
    public interface ICar {
        void move();
    }

    private static class Benz implements ICar {
        @Override
        public void move() {
            System.out.println("Benz moved!");
        }
    }

    private static class BMW implements ICar {
        @Override
        public void move() {
            System.out.println("BMW moved!");
        }
    }

    //简单工厂
    private static class SimpleFactory {
        public static ICar getCar(int carType) {
            switch (carType) {
                case 0:
                    return new Benz();
                case 1:
                    return new BMW();
            }
            return null;
        }
    }
   }
    public static void main(String[] args) {
        //开始生产
        ICar car1 = SimpleFactory.getCar(1);
        car1.move();
    }
二、 工厂方法
1. 优点
2. 缺点
3. 实例
    private interface IFactory {
        ICar getCar();
    }
    
    private class BenzFactory implements IFactory {
        public ICar getCar() {
            return new Benz();
        }
    }
    
    private class BMWFactory implements IFactory {
        public ICar getCar() {
            return new BMW();
        }
    }
    public static void main(String[] args) {
        //工厂方法
        IFactory factory = new BenzFactory();
        ICar car1 = factory.getCar();
        car1.move();

        IFactory factory=new BMWFactory();ICar car2=factory.getCar();
        car2.move();
    }
4. 工厂方法的实现:泛型
    public class CarFactory {
        public static ICar createCar(Class<? extends ICar> c) {
            try {
                return (ICar) c.newInstance();
            } catch (Exception e) {
                System.out.println("初始化失败");
            }
            return null;
        }
    }
    
    ICar bmw = CarFactory.createCar(BMW.class);   
    if(bmw!=null){
            bmw.move();
    }

所有产品类必须实现ICar接口。

5. 工厂方法的实现:Enum
    enum EnumCarFactory {
        Benz {
            @Override
            public ICar create() {
                return new Benz();
            }
        },
        BMW {
            @Override
            public ICar create() {
                return new BMW();
            }
        };
    
        public abstract ICar create();
    }

接下来是使用:

  try{
      ICar ACar=EnumCarFactory.valueOf("Benz").create();
      ACar.move();
  }catch(Exception e){
      System.out.println("初始化失败");
  }

利用Enum的枚举特性,返回特定的产品类实例。

三、抽象工厂
1. 简介抽象工厂是由“产品族”的概念拓展而来的。

一个产品不止一个功能,例如我们为用户制定了一套出行方案,这个方案里面配备的有车辆、穿的衣服等,这些功能合在一起就成为“人群”这个产品的功能。如果只配备了车辆,那就跟工厂方法模式一样,只有一个功能,这是极端的情况。
所谓的抽象工厂指的是工厂不止生产某一具体的产品,而是能扩展到生产一系列的产品。

2. 示例
 public class Student {
        private String name;
        private int age;
        private boolean sex;
    
        public Student() {
        }
    
        public Student(StudentBuilder studentBuilder) {
            this.name = studentBuilder.name;
            this.age = studentBuilder.age;
            this.sex = studentBuilder.sex;
        }
    
            public static StudentBuilder newInstance() {
                return new StudentBuilder();
            }
    
        public static class StudentBuilder {
            private String name;
            private int age;
            private boolean sex;
    
            public StudentBuilder setName(String name) {
                this.name = name;
                return this;
            }
    
            public StudentBuilder setAge(int age) {
                this.age = age;
                return this;
            }
    
            public StudentBuilder setSex(boolean sex) {
                this.sex = sex;
                return this;
            }
    
            public Student build() {
                return new Student(this);
            }
        }
    
    
    
        public Student setName(String name) {
            this.name = name;
            return this;
        }
    
        public Student setAge(int age) {
            this.age = age;
            return this;
        }
    
        public Student setSex(boolean sex) {
            this.sex = sex;
            return this;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex=" + sex +
                    '}';
        }
    }

接下来是使用:三种方式

public static void main(String[] args) {
      //第一种
      Student student1 = new Student.StudentBuilder().setName("张三").setAge(18).
            setSex(true).build();
      //第二种
      Student student2 = Student.newInstance().setName("李四").setAge(20).setSex(false).
            build();
      //第三种
      Student student3 = new Student().setAge(22).setName("王五").setSex(true);

    System.out.println("student1= "+student1.toString());
    System.out.println("student2= "+student2.toString());
    System.out.println("student3= "+student3.toString());
}
上一篇 下一篇

猜你喜欢

热点阅读