美文网首页
设计模式 - 抽象工厂模式 (3/23)

设计模式 - 抽象工厂模式 (3/23)

作者: 夹板儿孩 | 来源:发表于2022-12-04 00:56 被阅读0次

抽象工厂模式

从设计层面上看,抽象工厂模式就是对简单工厂模式的改进或者进一步的抽象。将工厂抽象成两层,AbstractFactory 和具体的实现工厂子类。程序员可以根据句创建对象类型使用对应的工厂子类。这样可以将简单工厂变成工厂簇,更利于代码的维护和扩展

上面都是抄的,我理解的意思就是它实际跟工厂方法差不多。这个抽象方法实际就是将工厂方法模式和简单工厂的一个整合。其他没有什么特别之处,与工厂方法模式比较。唯一的好处我个人理解就是少了继承。片面的讲,它满足了七大原则中的 合成复用原则

  1. 抽象工厂模式定义了一个 interface 用于创建相关或有依赖关系的对象簇,而无需指明具体的类
  2. 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合

例子:

public abstract class Fresh {

    protected String name;

    public abstract void prepare();

    public void washing() {
        System.out.println(name + "正在清洗");
    }

    public void cut() {
        System.out.println(name + "正在分切");
    }

    public void box() {
        System.out.println(name + "正在打包");
    }

}
public class Panda extends Fresh {

    public Panda() {
        super.name = "鲜活大熊猫";
    }

    @Override
    public void prepare() {
        System.out.println(super.name + "正在调货");
    }
}

还有三个实体类与 Panda 实体类内容一致,就不贴了

public interface Factory {

    Fresh createFresh(String type);

}
public class CDFactory implements Factory {
    @Override
    public Fresh createFresh(String type) {
        System.out.println("成都工厂");
        if ("panda".equals(type)) {
            return new Panda();
        } else if ("fungi".equals(type)) {
            return new Fungi();
        }
        return null;
    }
}
public class GZFactory implements Factory {

    @Override
    public Fresh createFresh(String type) {
        System.out.println("广州工厂");
        if ("octopus".equals(type)) {
            return new Panda();
        } else if ("crab".equals(type)) {
            return new Fungi();
        }
        return null;
    }
}
public class Boxhorse {
    Factory factory;

    public Boxhorse(Factory factory){
        this.factory = factory;
    }

    public void shopping() {
        if (null == factory) {
            System.out.println("工厂未实例化");
            return;
        }
        do {
            Fresh fresh = factory.createFresh(this.getType());
            if (null != fresh) {
                fresh.prepare();
                fresh.washing();
                fresh.cut();
                fresh.box();
            } else {
                System.out.println("不卖了");
                break;
            }
        } while (true);
    }

    private String getType() {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("input pizza type: ");
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
public class Main {
    public static void main(String[] args) {
        Factory cd = new CDFactory();
        Factory gz = new GZFactory();
        new Boxhorse(cd).shopping();
        System.out.println("=============================================");
        new Boxhorse(gz).shopping();
    }
}

不足之处还望指出

抽象方法模式结合单例

这个模式我们也结合我们的单例模式完成一个工厂认证,实体类与上面的使用的是同一个,下面只贴不一样的代码

public interface Factory {

    /**
     * 创建工厂
     * @param type
     * @return
     */
    Fresh createFresh(String type);

    /**
     * 获取认证材料
     * @return
     */
    String getAuth();

    /**
     * 获取厂名
     * @return
     */
    String getFactoryName();
}
enum Authorization {
    INSTANCE;
    List<String> authorized = new ArrayList<>();
    List<String> certificate = new ArrayList<>(Arrays.asList("A1", "A2"));

    /**
     * 取得证书
     * @return 证书
     */
    String getAuth() {
        String auth = null;
        if (certificate.size() > 0) {
            auth = certificate.get(0);
            authorized.add(auth);
            certificate.remove(0);
        }
        return auth;
    }

    /**
     * 检查证书
     * @param auth 证书
     * @return 是否存在
     */
    boolean check(String auth) {
        return authorized.contains(auth);
    }
}
public class CDFactory implements Factory {

    String auth = null;
    String factoryName = "成都工厂";

    public CDFactory() {
        this.auth = Authorization.INSTANCE.getAuth();
    }

    @Override
    public Fresh createFresh(String type) {
        System.out.println(factoryName);
        if ("panda".equals(type)) {
            return new Panda();
        } else if ("fungi".equals(type)) {
            return new Fungi();
        }
        return null;
    }

    @Override
    public String getAuth() {
        System.out.println("正在获取" + factoryName + "的证书");
        return auth;
    }

    @Override
    public String getFactoryName() {
        return factoryName;
    }

}
public class GZFactory implements Factory {
    public String auth = null;
    public String factoryName = "广州工厂";

    public GZFactory() {
        this.auth = Authorization.INSTANCE.getAuth();
    }

    @Override
    public Fresh createFresh(String type) {
        System.out.println(factoryName);
        if ("octopus".equals(type)) {
            return new Panda();
        } else if ("crab".equals(type)) {
            return new Fungi();
        }
        return null;
    }

    @Override
    public String getAuth() {
        System.out.println("正在获取" + factoryName + "的证书");
        return auth;
    }

    @Override
    public String getFactoryName() {
        return factoryName;
    }
}
public class ZLFactory implements Factory {
    String auth = null;
    String factoryName = "脏乱工厂";

    public ZLFactory() {
        this.auth = Authorization.INSTANCE.getAuth();
    }

    @Override
    public Fresh createFresh(String type) {
        System.out.println(factoryName);
        return null;
    }

    @Override
    public String getAuth() {
        System.out.println("正在获取" + factoryName + "的证书");
        return auth;
    }

    @Override
    public String getFactoryName() {
        return factoryName;
    }
}
public class Boxhorse {
    Factory factory;

    public Boxhorse(Factory factory){
        // 检查证书
        if (Authorization.INSTANCE.check(factory.getAuth())) this.factory = factory;
    }

    public void shopping() {
        if (null == factory) {
            System.out.println("工厂没有证书");
            return;
        }
        do {
            Fresh fresh = factory.createFresh(this.getType());
            if (null != fresh) {
                fresh.prepare();
                fresh.washing();
                fresh.cut();
                fresh.box();
            } else {
                System.out.println("不卖了");
                break;
            }
        } while (true);
    }

    private String getType() {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("input pizza type: ");
        try {
            return bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
public class Main {
    public static void main(String[] args) {
        Factory cd = new CDFactory();
        Factory gz = new GZFactory();
        Factory zl = new ZLFactory();
        new Boxhorse(cd).shopping();
        System.out.println("=============================================");
        new Boxhorse(gz).shopping();
        System.out.println("=============================================");
        new Boxhorse(zl).shopping();
    }
}

相关文章

  • Go语言设计模式(3)抽象工厂模式

    Go语言设计模式(3)抽象工厂模式 抽象工厂模式的定义 抽象工厂模式的定义如下: Provide an inter...

  • spring 设计模式篇(前)

    设计模式俗称:套路 一、spring常见设计模式 (23种设计模式) 创建型:工厂方法模式、抽象工厂模式、建造者...

  • 设计模式

    设计模式分为 3 大类型共 23 种:创建型:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。结构型:...

  • 工厂模式理解

    工厂模式分为简单工厂、工厂模式、抽象工厂三种层级概念。简单工厂不属于23种设计模式,抽象工厂才是。 简单工厂 简单...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

  • Android设计模式:工厂设计模式

    目录 简单工厂模式工厂方法模式抽象工厂模式 简单工厂模式 简单工厂模式是所有工厂模式的基础,不属于23种设计模式范...

  • 设计模式四、抽象工厂模式

    系列传送门设计模式一、单例模式设计模式二、简单工厂模式设计模式三、工厂模式设计模式四、抽象工厂模式 抽象工厂模式 ...

  • 三、创建型模型

    简单工厂模式 工厂方法模式 抽象工厂模式 单例模式 1.简单工厂模式 简单工厂模式并不属于23种设计模式。 不难看...

  • 设计模式的基本概念与区别

    1、设计模式有哪些? 23种设计模式总体分为三大类创建型设计模式,共5种简单工厂、工厂方法模式,抽象工厂模式,单例...

  • 工厂模式

    工厂模式 问题: 工厂模式分为几类? GOF 23种设计模式中,工厂方法模式和抽象工厂模式有什么区别? 不在GOF...

网友评论

      本文标题:设计模式 - 抽象工厂模式 (3/23)

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