前言

工程模式可以说是除了单例模式外,另外一个好理解的模式.所谓工厂,那么肯定是用来创作对象的,那么在工厂模式中,工厂工厂就是用于创建我们想要的对象.

工厂方法模式

工厂方法模式有的角色:

  1. Abstract Factory: 抽象工厂,提供创建产品接口,由具体工厂来实现如何创建
  2. Concrete Factory:具体工厂,实现抽象工厂中的抽象方法,完成具体产品的创建
  3. Abstract Product: 抽象产品,所有产品的抽象描述
  4. Concrete Product: 具体产品,和具体的工程之间一一对应

类图

2560px-FactoryMethod.svg

抽象工厂

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:52
 */
public interface AbstractFactory {
    /**
     * 创建具体的按钮
     *
     * @return button
     */
    Button createButton();
}

windows按钮的工厂

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:09
 */
public class WinFactory implements AbstractFactory {
    @Override
    public Button createButton() {
        return new WinButton();
    }
}

mac按钮的工程

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:09
 */
public class MacFactory implements AbstractFactory {
    @Override
    public Button createButton() {
        return new MacButton();
    }
}

抽象产品(按钮)

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public abstract class Button {
}

mac按钮

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public class MacButton extends Button {
    public MacButton() {
        System.out.println("this is a mac button");
    }
}

win按钮

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class WinButton extends Button {
    public WinButton() {
        System.out.println("this is a win button");
    }
}

使用工厂方法

package factorymethod;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class Customer {
    public static void main(String[] args) {
        AbstractFactory macFactory = new MacFactory();
        macFactory.createButton();
        AbstractFactory winFactory = new WinFactory();
        winFactory.createButton();
    }
}

简单工厂模式

简单工厂模式中主要有以下几个角色:

  1. Factory: 工厂类,用于创建产品
  2. Abstract Product: 抽象产品,所有产品的抽象描述
  3. Concrete Product: 具体产品

简单工厂模式可以理解为工厂方法模式的一个特例:

抽象产品:一个按钮

package simplefactory;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public abstract class Button {
}

具体产品:我们根据平台分为mac系统的按钮和windows系统的按钮

package simplefactory;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public class MacButton extends Button {
    public MacButton() {
        System.out.println("this is a mac button");
    }
}

package simplefactory;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class WinButton extends Button {
    public WinButton() {
        System.out.println("this is a win button");
    }
}

工厂:工厂根据我们的需要创建不同的产品(按钮)

package simplefactory;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:52
 */
public class Factory {
    public static  Button createButton(String platform) {
        switch (platform) {
            case "mac":
                return new MacButton();
            case "win":
                return new WinButton();
            default:
                return null;
        }
    }
}

package simplefactory;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class Customer {
    public static void main(String[] args) {
        Button win = Factory.createButton("win");
        Button mac = Factory.createButton("mac");
    }
}

抽象工厂模式

类图:

1920px-Abstract_factory_UML.svg

抽象工厂模式与工厂方法模式的不同在于工厂方法模式只创建一种同类型的产品(Button),现在我们为它新增一种产品(Plane).

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:52
 */
public interface AbstractFactory {
    /**
     * 创建具体的按钮
     *
     * @return button
     */
    Button createButton();

    /**
     * 创建具体的面板
     *
     * @return plane
     */
    Plane createPlane();
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:09
 */
public class MacFactory implements AbstractFactory {
    @Override
    public Button createButton() {
        return new MacButton();
    }

    @Override
    public Plane createPlane() {
        return new MacPlane();
    }
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:09
 */
public class WinFactory implements AbstractFactory {
    @Override
    public Button createButton() {
        return new WinButton();
    }

    @Override
    public Plane createPlane() {
        return new WinPlane();
    }
}

package abstractfactroy;

/**
 * 面板
 *
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:32
 */
public abstract class Plane {
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:32
 */
public class MacPlane extends Plane {
    public MacPlane() {
        System.out.println("this is a mac plane");
    }
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 12:32
 */
public class WinPlane extends Plane {
    public WinPlane() {
        System.out.println("this is a win plane");
    }
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public abstract class Button {
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class WinButton extends Button {
    public WinButton() {
        System.out.println("this is a win button");
    }
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:54
 */
public class MacButton extends Button {
    public MacButton() {
        System.out.println("this is a mac button");
    }
}

package abstractfactroy;

/**
 * @author caiyuanhao456 cyhyx521@gmail.com
 * @date 2020/9/27 11:55
 */
public class Customer {
    public static void main(String[] args) {
        AbstractFactory macFactory = new MacFactory();
        macFactory.createButton();
        macFactory.createPlane();
        AbstractFactory winFactory = new WinFactory();
        winFactory.createButton();
        winFactory.createPlane();
    }
}

所以工厂方法模式和抽象工厂模式最大的区别就是工厂方法模式只创建一种产品,
而抽象工厂模式可以创建多种产品,并且工厂类也有多种创建不同产品的方法.

代码地址

https://github.com/yuanhca/design-pattern/tree/master/factory-pattern

参考

  1. http://c.biancheng.net/view/1348.html
  2. https://zh.wikipedia.org/wiki/%E5%B7%A5%E5%8E%82%E6%96%B9%E6%B3%95#%E5%B7%A5%E5%8E%82
  3. https://blog.csdn.net/jason0539/article/details/23020989

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

人生中没有四季 唯有那寒冬的荒野