【设计模式专题之抽象工厂模式】3. 家具工厂

题目描述

小明家新开了两个工厂用来生产家具,一个生产现代风格的沙发和椅子,一个生产古典风格的沙发和椅子,现在工厂收到了一笔订单,请你帮他设计一个系统,描述订单需要生产家具的信息。

输入描述

输入的第一行是一个整数 N(1 ≤ N ≤ 100),表示订单的数量。

接下来的 N 行,每行输入一个字符串,字符串表示家具的类型。家具类型分为 "modern" 和 "classical" 两种。

输出描述

对于每笔订单,输出字符串表示该订单需要生产家具的信息。

modern订单会输出下面两行字符串

modern chair

modern sofa

classical订单会输出下面两行字符串

classical chair

classical soft

输入示例
复制代码
3
modern
classical
modern
输出示例
复制代码
modern chair
modern sofa
classical chair
classical sofa
modern chair
modern sofa
提示信息

在示例中,工厂收到了3笔订单,其中有2笔要求生产modern风格,1笔要求生产classical风格。根据输入的类型,每次订单生产的家具信息被输出到控制台上。

java 复制代码
package abstractFactory;

import java.util.Scanner;

// 抽象椅子接口
interface Chair {
    void showInfo();
}

// 具体现代风格椅子
class ModernChair implements Chair {

    @Override
    public void showInfo() {
        System.out.println("modern chair");
    }
}

// 具体古典风格椅子
class ClassicalChair implements Chair {

    @Override
    public void showInfo() {
        System.out.println("classical chair");
    }
}

// 抽象沙发接口
interface Sofa {
    void displayInfo();
}

// 具体现代风格沙发
class ModernSofa implements Sofa {
    @Override
    public void displayInfo() {
        System.out.println("modern sofa");
    }
}

// 具体古典风格沙发
class ClassicalSofa implements Sofa {
    @Override
    public void displayInfo() {
        System.out.println("classical sofa");
    }
}

// 抽象家居工厂接口
interface FurnitureFactory {
    Chair createChair();
    Sofa createSofa();
}

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

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

// 具体古典风格家居工厂
class ClassicalFurnitureFactory implements FurnitureFactory {
    @Override
    public Chair createChair() {
        return new ClassicalChair();
    }

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

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取订单数量
        int N = scanner.nextInt();

        // 处理每个订单
        for (int i = 0; i < N; i++) {
            // 读取家具类型
            String furnitureType = scanner.next();

            // 创建相应风格的家居装饰品工厂
            FurnitureFactory factory = null;
            if (furnitureType.equals("modern")) {
                factory = new ModernFurnitureFactory();
            } else if (furnitureType.equals("classical")) {
                factory = new ClassicalFurnitureFactory();
            }

            // 根据工厂生产椅子和沙发
            Chair chair = factory.createChair();
            Sofa sofa = factory.createSofa();

            // 输出家具信息
            chair.showInfo();
            sofa.displayInfo();
        }
    }
}
相关推荐
三翼鸟数字化技术团队9 分钟前
提升开发思维的设计模式(上)
前端·javascript·设计模式
怀旧,12 分钟前
【数据结构】7. 栈和队列
数据结构
坚持学习永不言弃1 小时前
创建型-原型模式
设计模式
W说编程2 小时前
算法导论第一章:算法基础与排序艺术
c语言·数据结构·算法
titan TV man2 小时前
上海市计算机学会竞赛平台2022年5月月赛丙组最远城市距离
数据结构·算法
慢半拍iii11 小时前
数据结构——D/串
c语言·开发语言·数据结构·c++
怀旧,11 小时前
【数据结构】5. 双向链表
数据结构·windows·链表
会不再投降21911 小时前
《算法复杂度:数据结构世界里的“速度与激情”》
数据结构·算法
vvilkim11 小时前
深入解析 Pandas 核心数据结构:Series 与 DataFrame
数据结构·pandas
Frankabcdefgh11 小时前
Python基础数据类型与运算符全面解析
开发语言·数据结构·python·面试