工厂设计模式

工厂模式是用工厂方法代替new操作的一种模式。
在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

3种:简单工厂模式、工厂方法模式、抽象工厂模式。

适用场景
(1)在编码时不能预见需要创建哪种类的实例。
(2)系统不应依赖于产品类实例如何被创建、组合和表达的细节
(3)Spring框架的IOC就是使用的工厂模式

优点
(1)一个调用者想创建一个对象,只要知道其名称就可以了
(2)扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以
(3)屏蔽产品的具体实现,调用者只关心产品的接口。

缺点
每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。

1. 简单工厂模式:

通过专门定义一个类来负责创建其他类的实例

结构如下:
在这里插入图片描述

代码如下:

package com.revision.DesignMethod;

public class EasyFactory {
    public static Operation createOperate(String operate) {
        Operation oper = null;
        switch (operate) {
            case "+":
                oper = new OperationAdd();
                break;
            case "-":
                oper = new OperationSub();
                break;
            case "*":
                oper = new OperationMul();
                break;
            case "/":
                oper = new OperationDiv();
                break;
            default:
                return null;
        }
        return oper;
    }
}

    class Operation {
        private int i, j;

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        public int getJ() {
            return j;
        }

        public void setJ(int j) {
            this.j = j;
        }

        public double getRes() {
            return 0;
        }
    }

class OperationAdd extends Operation {
        @Override
        public double getRes() {
            return this.getI() + this.getJ();
        }
    }

class OperationSub extends Operation {
        @Override
        public double getRes() {
            return this.getI() - this.getJ();
        }
    }

class OperationMul extends Operation {
        @Override
        public double getRes() {
            return this.getI() * this.getJ();
        }
    }

class OperationDiv extends Operation {
    @Override
    public double getRes() {
        if (this.getJ() == 0) {
            System.out.println("除数不能为0!");
            return 0;
        } else {
            return this.getI() / this.getJ();
        }
    }
}
class User{
    public static void main(String[] args) {
        Operation operation = null;
        operation = EasyFactory.createOperate("+");
        operation.setI(1);
        operation.setJ(2);
        System.out.println(operation.getRes());
    }
}

2. 工厂方法模式

定义一个用于创建对象的接口,让子类决定实例化哪一个类。

结构如下:
先定义抽象接口,再创建抽象产品类,创建具体产品类,创建具体工厂类。外界通过调用具体工厂类的方法,从而创建不同具体产品类的实例。
在这里插入图片描述

代码如下:(逻辑重复的部分并没有写,具体可以参照上面的代码)

package com.revision.DesignMethod.factorydesign;

interface IFactory{
    Operation createOperation();
}

public class AbstractFactory {
    public static void main(String[] args) {

    }
}

class AddFactory implements IFactory{
    @Override
    public Operation createOperation() {
        return new OperationAdd();
    }
}

class SubFactory implements IFactory{
    @Override
    public Operation createOperation() {
        return new OperationSub();
    }
}

class MulFactory implements IFactory{
    @Override
    public Operation createOperation() {
        return new OperationMul();
    }
}

class DivFactory implements IFactory{
    @Override
    public Operation createOperation() {
        return new OperationDiv();
    }
}


3. 抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

【好处】

①便于交换产品系列

②让具体的创建实例过程与客户端分离

结构如图:
先创建接口,如一个形状接口一个颜色接口,再创建接口实现类,如各种形状和各种颜色,覆写了这两个接口的涂色方法和定型方法。然后创建一个抽象类,用来定义获取颜色和形状的方法,然后定义工厂继承这个抽象类,覆写需要的代码,并提供返回什么形状或者什么颜色的示例。

在这里插入图片描述

代码如下:

package com.revision.DesignMethod.factorydesign;

interface Shape {
    void draw();
}

interface Color{
    void fill();
}

class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Rectangle");
    }
}

class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Square");
    }
}

class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Circle");
    }
}

class Red implements Color{
    @Override
    public void fill() {
        System.out.println("red");
    }
}

class Green implements Color{
    @Override
    public void fill() {
        System.out.println("green");
    }
}

class Blue implements Color{
    @Override
    public void fill() {
        System.out.println("blue");
    }
}

public abstract class AbstractFactory {
    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}

class ShapeFactory extends AbstractFactory {

    @Override
    public Shape getShape(String shapeType){
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE")){
            return new Circle();
        } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
            return new Rectangle();
        } else if(shapeType.equalsIgnoreCase("SQUARE")){
            return new Square();
        }
        return null;
    }

    @Override
    public Color getColor(String color) {
        return null;
    }
}

class ColorFactory extends AbstractFactory {

    @Override
    public Shape getShape(String shapeType){
        return null;
    }

    @Override
    public Color getColor(String color) {
        if(color == null){
            return null;
        }
        if(color.equalsIgnoreCase("RED")){
            return new Red();
        } else if(color.equalsIgnoreCase("GREEN")){
            return new Green();
        } else if(color.equalsIgnoreCase("BLUE")){
            return new Blue();
        }
        return null;
    }
}

class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("SHAPE")){
            return new ShapeFactory();
        } else if(choice.equalsIgnoreCase("COLOR")){
            return new ColorFactory();
        }
        return null;
    }
}
class AbstractFactoryPatternDemo {
    public static void main(String[] args) {

        //获取形状工厂
        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

        //获取形状为 Circle 的对象
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //调用 Circle 的 draw 方法
        shape1.draw();

        //获取形状为 Rectangle 的对象
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //调用 Rectangle 的 draw 方法
        shape2.draw();

        //获取形状为 Square 的对象
        Shape shape3 = shapeFactory.getShape("SQUARE");

        //调用 Square 的 draw 方法
        shape3.draw();

        //获取颜色工厂
        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

        //获取颜色为 Red 的对象
        Color color1 = colorFactory.getColor("RED");

        //调用 Red 的 fill 方法
        color1.fill();

        //获取颜色为 Green 的对象
        Color color2 = colorFactory.getColor("Green");

        //调用 Green 的 fill 方法
        color2.fill();

        //获取颜色为 Blue 的对象
        Color color3 = colorFactory.getColor("BLUE");

        //调用 Blue 的 fill 方法
        color3.fill();
    }
}

更多推荐