【设计模式】2、工厂模式之简单工厂、方法工厂、抽象工厂

针对于工厂模式 有三种方式 1.简单工厂 2.方法工厂 3.抽象工厂 我们依次实现

1、简单工厂模式

简单工厂介绍:

1)简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出那一中产品类的实例,简单工厂模式就是工厂模式家族中最简单使用的模式

2)简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为

3)在软件开发中,当我们会使用大量的创建某种、某类或者某批对象实例时,就可以使用工厂模式

我们假设一个这样的场景,有两种电脑PC  MAC和 联想  学生要使用电脑 肯定不能自己去创建电脑内,因为电脑的内部细节 主板 cpu 内存等等 只有电脑本身这个类自己清楚,学生只需要使用这个电脑的对外提供的功能就可以。

因此 代码实现如下。

电脑类

public interface Computer {

    public void use();

}

联想电脑类 

public class LianXiangPC implements Computer {

    @Override
    public void use() {
        System.out.println("欢迎使用联想电脑 机器运行中。。。");
    }
}

MAC电脑类

public class MacPC implements Computer {

    @Override
    public void use() {
        System.out.println("欢迎使用Mac电脑 机器运行中、、、");
    }

}

简单工厂方法

/**
 * @author i
 * @create 2019/10/14 21:37
 * @Description 简单工厂方法
 */
public class PCFactory  {

    //构造方法私有化
    private PCFactory(){

    }

    private static Properties config = new Properties();

    //加载配置文件
    static {
        try {
            config.load(PCFactory.class.getResourceAsStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取实例对象
     * @return
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static Computer getPC() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        String className = config.getProperty("pc");
        Class<?> clazz = Class.forName(className);
        return (Computer) clazz.newInstance();
    }
}

学生类

public class Student {

    private Computer computer;

    public Student(Computer computer){
        this.computer = computer;
    }

    public void use(){
        computer.use();
    }

}

测试类

public class StudentTest {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        Computer pc  = PCFactory.getPC();
        Student student = new Student(pc);
        student.use();

    }

}

2、工厂方法模式

      工厂方法模式介绍

1、工厂方法模式:定义一个创建对象的抽象方法,由子类决定要实例化的类,工厂方法模式将对对象的实例化延迟到子类、

进一步代码推进

定义一个抽象类sell 销售者 持有一个抽象方法

public abstract  class Sell {

    public Computer sellComputer(){
        return getComputer();
    }

    public abstract Computer getComputer();

}

联想工厂方法代码:

public class LianXiangPCFactory extends Sell {

    @Override
    public Computer getComputer() {
        return new LianXiangPC();
    }
}

MAC工厂方法代码 

public class MacPCFactory extends Sell {

    @Override
    public Computer getComputer() {
        return new MacPC();
    }
}

测试类 

public class Test {

    public static void main(String[] args) {
        Sell sell = new LianXiangPCFactory();
        Computer computer = sell.getComputer();
        computer.use();
    }

}

3、抽象工厂模式

1、抽象工厂模式:定义了一个interface用于创建相关或者有依赖关系的对象,而无需指明具体的类。

2、抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合。

3、从设计层面看,抽象工厂模式就是对简单工厂模式的改造。

4、将工厂抽象成两层。AbsFactory(抽象工厂)和 具体实现的子类工厂 程序员可以根据创建对象类型使用对应的工厂子类,这样将单个的简单工厂类变成了工厂,有利于代码的拓展和维护。

抽象工厂类 

public interface AbstractPCFactory {

    public Computer getComputer();

}

具体的实现的子类工厂

public class LianXiangPCFFactory implements AbstractPCFactory {
    @Override
    public Computer getComputer() {
        return new LianXiangPC();
    }
}

具体的实现的子类工厂

public class Sell {

    private AbstractPCFactory pcPCFactory;

    public AbstractPCFactory getPcPCFactory() {
        return pcPCFactory;
    }

    public void setPcPCFactory(AbstractPCFactory pcPCFactory) {
        this.pcPCFactory = pcPCFactory;
    }

    public Computer getPC(){
        return pcPCFactory.getComputer();
    }
}
public class MacPCFactory implements AbstractPCFactory {
    @Override
    public Computer getComputer() {
        return new MacPC();
    }
}

测试类 

public static void main(String[] args) {
        MacPCFactory macPCFactory = new MacPCFactory();
        Sell sell = new Sell();
        sell.setPcPCFactory(macPCFactory);
        sell.getPC().use();
    }

4、工厂模式在jdk中的使用-Calendar

5、小结

       1、工厂模式的意义

           将实例化对象的代码提取出来,放到一个类中进行统一管理和维护,达到和主项目的依赖关系的解耦,从而提高了项目的拓展和维护性。

       2、三种工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)

       3、设计模式的依赖抽象原则

  • 创建对象实例时不要直接new,而是把这个new类的动作放到一个工厂的方法中,并返回。有的书上说,变量不要直接持有具体类的引用。
  • 不要让类继承具体的类,而是继承抽象类或者实现interface
  • 不要覆盖基类中已实现的方法。
原文地址:https://www.cnblogs.com/qxlxi/p/12860790.html