设计模式 简单工厂模式

本文介绍了工厂模式在Java中的应用,特别是如何利用反射进行动态对象创建,以遵循开闭原则。通过Clothes类和其子类(如Jackets、Trousers、TShirts),展示了如何通过配置文件控制不同类型的衣物实例化过程。
摘要由CSDN通过智能技术生成

简单工厂模式

1 什么是工厂模式?

开发中有一个非常重要的原则“开闭原则”,对拓展开放、对修改关闭。

工厂模式主要负责对象创建的问题。

可通过反射进行工厂模式的设计,完成动态的对象创建。

2 代码演示

Clothes类

public abstract class Clothes {
    abstract void prepare();
    abstract void make();
    abstract void box();
}

Jackets类

public class Jackets extends Clothes{
    @Override
    void prepare() {
        System.out.println("准备夹克的布料...");
    }

    @Override
    void make() {
        System.out.println("开始制作夹克...");
    }

    @Override
    void box() {
        System.out.println("包装夹克...");
    }
}

Trousers类

public class Trousers extends Clothes{
    @Override
    void prepare() {
        System.out.println("准备裤子的布料...");
    }

    @Override
    void make() {
        System.out.println("开始制作裤子...");
    }

    @Override
    void box() {
        System.out.println("包装裤子...");
    }
}

TShirts类

public class TShirts extends Clothes{
    @Override
    void prepare() {
        System.out.println("准备短袖的布料...");
    }

    @Override
    void make() {
        System.out.println("开始制作短袖...");
    }

    @Override
    void box() {
        System.out.println("包装短袖...");
    }
}

ClothesFactory类

原代码:

public class ClothesFactory {
    public static Clothes createClothes(int type) {
        Clothes clothes = null;
        if (type == 1) {
            clothes = new Trousers();
        } else if (type == 2) {
            clothes = new TShirts();
        } else if (type == 3) {
            clothes = new Jackets();
        }
        if (clothes != null) {
            clothes.prepare();
            clothes.make();
            clothes.box();
        }
        return clothes;
    }
}

使用反射优化:

package StageOne.day24.ClothesFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author 胡昊龙
 * @version 1.0
 * @description: TODO
 * @date 2024/1/19 16:52
 * 工厂模式
 */
public class ClothesFactory  {
    private static final Properties properties = new Properties();
    static {
        try {
            FileInputStream fis = new FileInputStream("Properties/clothes.properties");
            properties.load(fis);
            fis.close();
        } catch (IOException e) {
            System.out.println("加载配置文件失败");
        }
    }
    public static  Clothes createClothes(String type) {
        Clothes clothes = null;
        if (properties.containsKey(type)) {
            String name = properties.getProperty(type);
            try {
                clothes = (Clothes) Class.forName(name).newInstance();
            } catch (Exception e) {
                System.out.println("Class.forName(name).newInstance()...错误");
            }
        }

        if (clothes != null) {
            clothes.prepare();
            clothes.make();
            clothes.box();
        }
        return clothes;
    }
}

clothes.properties配置文件

1=StageOne.day24.demo3.Trousers
2=StageOne.day24.demo3.TShirts
3=StageOne.day24.demo3.Jackets

Test类

public class Test {
    public static void main(String[] args) {
        System.out.println("1.裤子 2.短袖 3.夹克");
        System.out.println("请选择");
        Scanner input = new Scanner(System.in);
        String choose = input.next();
        //调用工厂方法
        Clothes clothes = ClothesFactory.createClothes(choose);
        if (clothes != null) {
            System.out.println("购买成功...");
        } else {
            System.out.println("购买失败...");
        }
    }
}
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贰贰柒丶阿拽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值