设计模式 简单工厂模式

简单工厂模式

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("购买失败...");
        }
    }
}

你可能感兴趣的:(从零学Java,设计模式,简单工厂模式,java)