从字符串创建类并使用命令模式(Create Class from String and use Command Pattern)

编程入门 行业动态 更新时间:2024-10-10 08:26:49
字符串创建类并使用命令模式(Create Class from String and use Command Pattern)

我知道这在stackoverflow之前已被质疑,但我找不到解决我的问题。 我想从ButtonClick事件中分离出一些代码,我在ButtonClick上动态创建节点并将它们添加到父AnchorPane。 在我的节点上是按钮,我的按钮上的这些事件用CommandPattern进行处理。 我创建不同的ButtonEvents,这取决于我创建的节点。

目前工作正常的代码是:

@FXML void addErosionNode(ActionEvent event){ DragNode nde = new DragNode(); /* id = nde.getId(); name = new String("Erosion"); Erosion cmd; cmd = new Erosion(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); */ setupNode(nde); nde.setNodeWithTwoCircles(); }

但是我想将代码放在另一个Method中的/ ** /内,所以我可以用setupNode(nde, name);替换代码setupNode(nde, name);

并尝试使用此方法:

public void setupNode(DragNode nde, String name){ id = nde.getId(); Class clazz; className = new String ("application.bvfunc." + type); //This will be e.g. application.bvfunc.Erosion which is the class I want to use try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { System.out.println("fail"); } clazz cmd; cmd = new clazz(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }

但clazz不能解析为类型。

我该如何更换

Erosion cmd; cmd = new Erosion();

用clazz创建我的节点的名称? 喜欢这个:

clazz cmd; cmd = new clazz();

I know this has been questioned before on stackoverflow, but I could not find a solution for my problem. I want to separate some Code from ButtonClick event, where I dynamically create Nodes on ButtonClick and add them to a parent AnchorPane. On my Nodes are Buttons, these Events on my Button are handled with a CommandPattern. I create different ButtonEvents, depending on the Node I created.

The Code which works fine so far is:

@FXML void addErosionNode(ActionEvent event){ DragNode nde = new DragNode(); /* id = nde.getId(); name = new String("Erosion"); Erosion cmd; cmd = new Erosion(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); */ setupNode(nde); nde.setNodeWithTwoCircles(); }

But I want to put the Code between /**/ inside another Method, so I can replace the Code with setupNode(nde, name);

And try to use this method:

public void setupNode(DragNode nde, String name){ id = nde.getId(); Class clazz; className = new String ("application.bvfunc." + type); //This will be e.g. application.bvfunc.Erosion which is the class I want to use try { clazz = Class.forName(className); } catch (ClassNotFoundException e) { System.out.println("fail"); } clazz cmd; cmd = new clazz(); nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }

But clazz cannot be resolved to a type.

How can I replace

Erosion cmd; cmd = new Erosion();

with clazz I create with the name of my Node? Like this:

clazz cmd; cmd = new clazz();

最满意答案

您可以不使用反射,而是创建一个将其命令的名称映射到构造函数的映射:

private static final Map<String, Supplier<Command>> commandMap = new HashMap<>(); static { commandMap.put("Erosion", Erosion::new); commandMap.put("Dilation", Dilation::new); }

然后在你的其他代码中:

public void setupNode(DragNode nde, String name){ id = nde.getId(); Command cmd = commandMap.get(name).get(); // <-- Calls the constructor nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }

You can, instead of using reflection, create a map that maps the names of their commands to the constructors:

private static final Map<String, Supplier<Command>> commandMap = new HashMap<>(); static { commandMap.put("Erosion", Erosion::new); commandMap.put("Dilation", Dilation::new); }

Then in your other code:

public void setupNode(DragNode nde, String name){ id = nde.getId(); Command cmd = commandMap.get(name).get(); // <-- Calls the constructor nodeList.add(new NodeList<String, String, Command>(name, id, cmd)); nde.nodeLayout(); rightAnchor.getChildren().add(nde); buildDragHandlers(); }

更多推荐

本文发布于:2023-08-06 22:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   命令   模式   Create   Pattern

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!