当前位置: 首页 > news >正文

【设计模式】抽象工厂模式

抽象工厂模式

  • 抽象工厂模式介绍
  • 模式代码实现
    • 产品
      • Chair
      • Sofa
      • Coffee Table
    • 工厂
    • 测试
    • 参考

抽象工厂模式介绍

抽象工厂模式:模式介绍

模式代码实现

产品

Chair

chair/Chair.java

package abstract_factory.chair;

/**
 * 椅子接口
 */
public interface Chair {
    void hasLeg();
}

chair/ModernChair.java

package abstract_factory.chair;

/**
 * 现代风格椅子
 */
public class ModernChair implements Chair {
    @Override
    public void hasLeg() {
        System.out.println("Modern chair has no leg.");
    }
}

chair/VictoriaChair.java

package abstract_factory.chair;

/**
 * 维多利亚风格椅子
 */
public class VictoriaChair implements Chair {
    @Override
    public void hasLeg() {
        System.out.println("Victoria chair has leg.");
    }
}

Sofa

sofa/Sofa.java

package abstract_factory.sofa;

/**
 * 沙发接口
 */
public interface Sofa {
    void sitOn();
}

sofa/ModernSofa.java

package abstract_factory.sofa;

/**
 * 现代风格沙发
 */
public class ModernSofa implements Sofa {
    @Override
    public void sitOn() {
        System.out.println("Modern sofa sit on.");
    }
}

sofa/VictoriaSofa.java

package abstract_factory.sofa;

/**
 * 维多利亚风格沙发
 */
public class VictoriaSofa implements Sofa {
    @Override
    public void sitOn() {
        System.out.println("Victory sofa sit on.");
    }
}

Coffee Table

coffee_table/CoffeeTable.java

package abstract_factory.coffee_table;

/**
 * 咖啡桌接口
 */
public interface CoffeeTable {
    void putCoffee();
}

coffee_table/ModernCoffeeTable.java

package abstract_factory.coffee_table;

/**
 * 现代风格咖啡桌
 */
public class ModernCoffeeTable implements CoffeeTable {
    @Override
    public void putCoffee() {
        System.out.println("Modern coffee table put coffee.");
    }
}

coffee_table/VictoriaCoffeeTable.java

package abstract_factory.coffee_table;

/**
 * 维多利亚风格咖啡桌
 */
public class VictoriaCoffeeTable implements CoffeeTable {
    @Override
    public void putCoffee() {
        System.out.println("Victoria coffee table put coffee.");
    }
}

工厂

factory/FurnitureFactory.java

package abstract_factory.factory;

import abstract_factory.chair.Chair;
import abstract_factory.coffee_table.CoffeeTable;
import abstract_factory.sofa.Sofa;

/**
 * 抽象家具工厂
 */
public interface FurnitureFactory {
    Chair createChair();
    CoffeeTable createCoffeeTable();
    Sofa createSofa();
}

factory/ModernFurnitureFactory.java

package abstract_factory.factory;

import abstract_factory.chair.Chair;
import abstract_factory.chair.ModernChair;
import abstract_factory.coffee_table.CoffeeTable;
import abstract_factory.coffee_table.ModernCoffeeTable;
import abstract_factory.factory.FurnitureFactory;
import abstract_factory.sofa.ModernSofa;
import abstract_factory.sofa.Sofa;

/**
 * 现代风格家具工厂
 */
public class ModernFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new ModernChair();
    }

    @Override
    public CoffeeTable createCoffeeTable() {
        return new ModernCoffeeTable();
    }

    @Override
    public Sofa createSofa() {
        return new ModernSofa();
    }
}

factory/VictoriaFurnitureFactory.java

package abstract_factory.factory;

import abstract_factory.chair.Chair;
import abstract_factory.chair.VictoriaChair;
import abstract_factory.coffee_table.CoffeeTable;
import abstract_factory.coffee_table.VictoriaCoffeeTable;
import abstract_factory.factory.FurnitureFactory;
import abstract_factory.sofa.Sofa;
import abstract_factory.sofa.VictoriaSofa;

/**
 * 维多利亚风格家具工厂
 */
public class VictoriaFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new VictoriaChair();
    }

    @Override
    public CoffeeTable createCoffeeTable() {
        return new VictoriaCoffeeTable();
    }

    @Override
    public Sofa createSofa() {
        return new VictoriaSofa();
    }
}

测试

测试代码

package abstract_factory;

import abstract_factory.chair.Chair;
import abstract_factory.coffee_table.CoffeeTable;
import abstract_factory.factory.FurnitureFactory;
import abstract_factory.factory.ModernFurnitureFactory;
import abstract_factory.factory.VictoriaFurnitureFactory;
import abstract_factory.sofa.Sofa;

/**
 * 测试类
 */
public class Main {
    public static void main(String[] args) {
        // 创建现代风格家具
        FurnitureFactory furnitureFactory1 = new ModernFurnitureFactory();
        Chair chair1 = furnitureFactory1.createChair();
        chair1.hasLeg();
        CoffeeTable coffeeTable1 = furnitureFactory1.createCoffeeTable();
        coffeeTable1.putCoffee();
        Sofa sofa1 = furnitureFactory1.createSofa();;
        sofa1.sitOn();

        // 创建维多利亚风格家具
        FurnitureFactory furnitureFactory2 = new VictoriaFurnitureFactory();
        Chair chair2 = furnitureFactory2.createChair();
        chair2.hasLeg();
        CoffeeTable coffeeTable2 = furnitureFactory2.createCoffeeTable();
        coffeeTable2.putCoffee();
        Sofa sofa2 = furnitureFactory2.createSofa();
        sofa2.sitOn();
    }
}

测试结果
在这里插入图片描述

参考

[1] REFACTORING.GURU.抽象工厂模式

相关文章:

  • SSM医院挂号就诊预约系统 毕业设计-附源码250853
  • 【数学思维】运筹学-线性规划之标准形式与Hidden LP
  • 关于Me-Tetrazine-PEG2-Me-Tetrazine甲基四嗪试剂标记应用
  • 【vue-router】Vue路由从创建到使用
  • 多媒体信息处理学习笔记-Chap 6. Image RetrievalChap 7. Video Retrieval
  • 珂朵莉树 学习笔记
  • 03 - Qt 多线程网络通信——套接字
  • 飞书事件订阅的应用
  • MindSpore第七期集训营视频回顾
  • UNIX环境高级编程-第六章-系统数据文件和信息
  • 【云原生之Docker实战】使用Docker部署Pichome个人相册系统
  • js-原生对象
  • 软件测试的学习笔记(5)
  • [网站]node.js如何在云服务器上搭建
  • 【JAVA刷题初阶】刷爆力扣第十弹——二叉树
  • 卷积神经网络大致结构、卷积层设计的参数、卷积结果计算公式、卷积参数的共享、池化层、卷积神经网络的具体结构、感受野、推荐使用的卷积神经网络
  • Kruskal算法
  • 星淘惠告诉你跨境平台那么多,凭什么要选亚马逊?
  • 创邻科技入选Gartner全球《图数据库管理系统市场指南》代表厂商
  • 专访科杰科技CEO于洋:湖仓一体数据底座,企业构建数据能力的“最优解” | 爱分析访谈