嘚儿驾...

创建者模式---工厂设计模式

  • 工厂模式主要负责对象的创建的问题。
  • 开发中有一个非常重要的原则 "开闭原则" ,对拓展开放,对修改关闭。(可以加功能,加代码,但是不要改以前的东西。)
  • 可通过反射进行工厂模式的设计,完成动态的对象创建。(达到 解耦合的目的,毕竟它就是用来解耦合的)。

情景如下:

  1. 先创建父类产品 Usb (接口)
/*
 * 父类产品
 * */

public interface Usb {
    void service();


}
  1. 子类产品(风扇,U盘,鼠标)
/*
*鼠标
* */

public class mouse implements Usb{

    @Override
    public void service() {
        System.out.println("鼠标开始工作。。。");
    }
}
/*
* 风扇
**/
public class Fan implements Usb {
    @Override
    public void service() {
        System.out.println("风扇开始工作了。。。");
    }
}
/*
*  U盘
* */
public class Upand implements Usb {
    @Override
    public void service() {
        System.out.println("U盘开始工作了。。。");
    }
}
  1. 工厂类,负责对象的创建
/*
 * 工厂类
 * */
public class UsbFactory {
    public static Usb creteUsb(int type) {
        Usb usb = null;
        if (type == 1) { // 1 表示鼠标
            usb = new mouse();
        } else if (type == 2) { // 2 表示风扇
            usb = new Fan();
        } else if (type == 3){  // 3 表示 Upand
            usb = new Upand();
        } else if (type == 4){  // 4 表示键盘
            usb = new KetBoard();
        }

            return usb;
    }


}
  1. 客户程序
/*
 * 客户程序
 * */
public class Demo {
    public static void main(String[] args) {
        System.out.println("=====请选择  1.鼠标  2.风扇   3.U盘=======");
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        Usb usb = UsbFactory.creteUsb(choice);
        if (usb != null){
            System.out.println("购买成功!");
            usb.service();
        } else {
            System.out.println("购买失败,您要购买的产品不存在!");
        }


    }
}

问题来了:

若是在加一些子类产品,则必须在工厂类中修改代码,这样不符合,开闭原则。

(所以我们可以用反射来优化工厂模式)

反射优化工厂模式:


只需简单的修改代码

/*
 * 工厂类
 * */
public class UsbFactory {
    public static Usb creteUsb(String type) {  // 类的全名称
        Usb usb = null;
        Class<?> class1 = null;
        try {
            class1 = Class.forName(type);  // 获取类对象,传入一个类的全名称,把这些类的全名称放在一个文件里,用流读取
            usb = (Usb) class1.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return usb;
    }
    
}
/*
 * 客户程序
 * */
public class Demo {
    public static void main(String[] args) throws Exception {
        System.out.println("=====请选择  1.鼠标  2.风扇   3.U盘  4.Broad=======");
        Scanner input = new Scanner(System.in);
        String choice = input.next();

//        1 = com.kang.r3.Mouse
//        2 = com.kang.r3.Fan
//        3 = com.kang.r3.Upand
//        4 = com.kang.r3.KeyBoard
// 将这些放在 properties 里面,用流读取,如果想再加子类产品,直接在properties 里加子类的全路径就好了。
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("src\\usb.properties");
        properties.load(fis);
        fis.close();

        Usb usb = UsbFactory.creteUsb(properties.getProperty(choice));
        if (usb != null) {
            System.out.println("购买成功!");
            usb.service();
        } else {
            System.out.println("购买失败,您要购买的产品不存在!");
        }
        
    }
}

usb.properties


1 = com.kang.r3.Mouse
2 = com.kang.r3.Fan
3 = com.kang.r3.Upand
4 = com.kang.r3.KeyBoard
posted @   走马!  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示