美文网首页
工厂模式

工厂模式

作者: 古早味蛋糕 | 来源:发表于2022-11-30 19:08 被阅读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());
}

相关文章

  • 常用设计模式

    设计模式 工厂模式 工厂模式思路上分:简单工厂模式,工厂模式, 抽象工厂模式// 抽象工厂模式可以代替工厂模式,做...

  • 工厂模式

    工厂模式细分三种:简单工厂模式、工厂模式、抽象工厂模式。 工厂模式相当于抽象了简单工厂模式的工厂类,而抽象工厂模式...

  • 工厂模式

    工厂模式 就是工厂---生产-->产品 在设计模式中,分为 简单工厂模式, 工厂方法模式,抽象工厂模式. 工厂模式...

  • 找女朋友之简单工厂模式,工厂模式,抽象工厂模式

    找女朋友之简单工厂模式,工厂模式,抽象工厂模式 找女朋友之简单工厂模式,工厂模式,抽象工厂模式

  • 【设计模式】- 工厂模式

    工厂模式分为三种:简单工厂模式、工厂方法模式和抽象工厂模式。 工厂模式:靠工厂生产对象 简单工厂模式中只有一个工厂...

  • 工厂模式

    工厂模式包含三种模式:简单工厂模式、工厂方法模式和抽象工厂模式。 简单工厂模式 定义简单工厂模式:由一个工厂类根据...

  • Java设计模式——工厂模式

    工厂模式简单工厂模式工厂方法模式抽象工厂模式 1.简单工厂模式 1.基本介绍1)简单工厂模式也叫静态工厂模式,是属...

  • 设计模式-3种工厂模式

    工厂模式包括:简单工厂模式,工厂方法模式,抽象工厂模式 简单工厂模式 工厂方法根据参数直接创建实例:工厂->产品 ...

  • 设计模式-工厂模式

    工厂模式概念 实例化对象,用工厂方法代替new操作。工厂模式包括工厂方法模式和抽象工厂模式。抽象工厂模式是工厂模式...

  • 第一章2.0工厂- 基础类准备

    2.1工厂-简单工厂模式2.2工厂-工厂方法模式2.3工厂-抽象工厂模式

网友评论

      本文标题:工厂模式

      本文链接:https://www.haomeiwen.com/subject/eajkfdtx.html