设计模式(抽象工厂类)

白昼怎懂夜的黑 提交于 2019-12-03 00:05:21

说的直白一点就是比简单工厂类多了一层封装(可能我的措辞不是很严谨,但是我觉得这是最容易理解的解释了)
将好比两套独立生产的子公司有一个父公司, 子公司的创建还是得父公司拍板儿.
首先根据简单工厂的思路
创建出两个简单工厂:
这里就不详细介绍了 直接上代码

Shape接口

public interface Shape {
    void draw();
}

Square 类

public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("这是ShapeFactory工厂创建的Square类的方法");
    }
}

Rectangle 类

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("这是ShapeFactory工厂创建的Rectangle 类的方法");
    }
}

工厂类 ShapeFactory

public class ShapeFactory {
    public Shape getShape (String type){
        if (type == null){
            return null;
        }
        if (type.equalsIgnoreCase("Square")){
            return new Square();
        }
        if (type.equalsIgnoreCase("Rectangle")){
            return new Rectangle();
        }
        return null;
    }

第二个工厂

Shape1接口

public interface Shape1 {
    void draw();
}

Square1 类

public class Square1 implements Shape1 {
    @Override
    public void draw() {
        System.out.println("这是ShapeFactory1工厂创建的Square1类的方法");
    }
}

Rectangle1 类

public class Rectangle1 implements Shape1 {
    @Override
    public void draw() {
        System.out.println("这是ShapeFactory1工厂创建的Rectangle1 类的方法");
    }
}

工厂类 ShapeFactory1

public class ShapeFactory1 {
    public Shape getShape1 (String type){
        if (type == null){
            return null;
        }
        if (type.equalsIgnoreCase("Square")){
            return new Square1();
        }
        if (type.equalsIgnoreCase("Rectangle")){
            return new Rectangle1();
        }
        return null;
    }

根据简单工厂的思路, 需要把两个工厂通过一个父工厂创建出来, 但是就算是小工厂创建对象我们都有继承实现多态, 所以这里也会用到继承多态.
同样的为了不让这两个工厂告诉客户端我们需要另一个类AbstractShapeFactory
AbstractShapeFactory类:

public abstract class AbstractShapeFactory {
    abstract Shape getShape(String type);
    abstract Shape1 getShape1(String type);
}

修改ShapeFactory 类 和 ShapeFactory1 类, 让他们继承 AbstractShapeFactory 类

ShapeFactory类

public class ShapeFactory extends AbstractShapeFactory {
    @Override
    public Shape getShape (String type){
        if (type == null){
            return null;
        }
        if (type.equalsIgnoreCase("Square")){
            return new Square();
        }
        if (type.equalsIgnoreCase("Rectangle")){
            return new Rectangle();
        }
        return null;
    }

    @Override
    Shape1 getShape1(String type) {
        return null;
    }
}

ShapeFactory1类

public class ShapeFactory1 extends AbstractShapeFactory{
    @Override
    Shape getShape(String type) {
        return null;
    }

    @Override
    public Shape1 getShape1 (String type){
        if (type == null){
            return null;
        }
        if (type.equalsIgnoreCase("Square")){
            return new Square1();
        }
        if (type.equalsIgnoreCase("Rectangle")){
            return new Rectangle1();
        }
        return null;
    }
}

下面就是创建超级工厂类来将这两个工厂创建出来
SuperShapeFactory 类:

public class SuperShapeFactory {
    public AbstractShapeFactory getFactory(String factoryType){
        if (factoryType == null){
            return null;
        }
        if (factoryType.equalsIgnoreCase("Shape")){
            return new ShapeFactory();
        }
        if (factoryType.equalsIgnoreCase("Shape1")){
            return new ShapeFactory1();
        }
        return null;
    }
}

这里面的代买就跟ShapeFactory 和 ShapeFactory1 里面的代码很相似了.
Main类运行入口;

public class TestMain {
    public static void main(String[] args){
        SuperShapeFactory superShapeFactory = new SuperShapeFactory();
        //获取 shapeFactory 工厂
        AbstractShapeFactory shapeFactory = superShapeFactory.getFactory("Shape");
        shapeFactory.getShape("Square").draw();
        //获取 shapeFactory1 工厂
        AbstractShapeFactory shapeFactory1 = superShapeFactory.getFactory("Shape1");
        shapeFactory1.getShape1("Square").draw();
    }
}

运行结果:

这是ShapeFactory工厂创建的Square类的方法
这是ShapeFactory1工厂创建的 Square1 类的方法

参考文献:

https://www.w3cschool.cn/java/java-abstract-factory-pattern.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!