我处于一种情况下,我试图实现一个(相对简单的)抽象语法树。所有节点都从名为SimpleNode的类型继承,该类型包含一些用于存储行和列信息并接受访问者的代码。
现在,某些节点也应该是可命名的,而其他节点应该具有“可访问”的属性(例如,公共或私有)。某些节点甚至应该同时支持两个接口。
我最好使用虚拟继承来实现这一点,并编写两个类NameableNode和AccessibleNode,但是Java不支持MI。
例如NameableNode可能具有字段“名称”,并为此字段实现简单的获取器和设置器。同样,AccessibleNode也可能具有“可访问性”字段和获取器/设置器。
什么是实现此目标并避免在代码库的很大一部分中引入代码重复的好方法?
小代码示例:
public class SimpleNode {
private int line = 0;
private int column = 0;
/* Getters and setters for line/column. */
/* ... */
}
public class NameableNode extends SimpleNode {
private String name = "";
/* Getters and setters for name */
}
public class AccessibleNode extends SimpleNode {
private boolean isPublic = false;
/* Getters and setters for accessibility */
}
答案 0 :(得分:2)
您正在寻找composition。这种方式有很多种-根据我对您要构建的内容的理解,我将提出一种应该适合您的目的。
首先,让我们为您的Node
s创建一些接口:
public interface Nameable {
/* Getters and setters for name */
}
public interface Accessible {
/* Getters and setters for accessibility */
}
接下来,您可能不想为每个Node
重复相同的实现,因此让我们创建这些实现:
public class NameDelegate() {
private String name = "";
/* Getters and setters for name */
}
public class AccessDelegate() {
private boolean isPublic = false;
/* Getters and setters for accessibility */
}
现在,让我们将所有内容放在一起:
public class SomeNodeA extends SimpleNode implements Nameable {
private NameDelegate nameDelegate;
public SomeNodeA(NameDelegate nameDelegate) {
this.nameDelegate = nameDelegate;
}
@Override
public String getName() {
return nameDelegate.getName();
}
@Override
public String setName(String name) {
nameDelegate.setName(name);
}
}
您也可以在一个类中同时拥有两种行为:
public class SomeNodeB extends SimpleNode implements Nameable, Accessible {
private NameDelegate nameDelegate;
private AccessDelegate accessDelegate;
public SomeNodeB(NameDelegate nameDelegate, AccessDelegate accessDelegate) {
this.nameDelegate = nameDelegate;
this.accessDelegate = accessDelegate;
}
@Override
public String getName() {
return nameDelegate.getName();
}
@Override
public String setName(String name) {
nameDelegate.setName(name);
}
@Override
public boolean getAccessibility() {
return accessDelegate.getAccessibility();
}
/* etc... */
}
想法是,您可以将不同“功能”的状态和功能打包到单独的委托中,并将它们作为节点中的相应接口公开。
此外,在Node
上进行操作时,如果您需要了解Node
的给定实例是否支持特定功能,则可以使用instanceof
-例如:>
if (someNode instanceof Nameable) {
// do naming stuff
}
答案 1 :(得分:0)
在这种情况下,我将对继承使用合成方法:
public class Node {
private int line = 0;
private int column = 0;
/* Getters and setters for line/column. */
/* ... */
private String name = null;
public String getName() {
return this.name;
}
public void setName(String name) {
this._name = name;
}
private Boolean _isPublic = null;
public String isPublic() {
return this.name;
}
public void setIsPublic(boolean isPublic) {
this._isPublic = isPublic;
}
public boolean hasAccessibility() {
return this._isPublic != null;
}
public boolean hasName() {
return this._name != null;
}
}
我更喜欢的另一种解决方案是使用HashMap和一个指示节点的所有可能属性的枚举来动态创建这些属性。这种方法更为通用,因为它需要编写更少的代码来支持新属性,但是它的类型安全性也较差,因为其他属性需要在运行时进行强制转换:
import java.util.HashMap;
enum NodeAttribute {
NAME,
ACCESSIBILTY
}
enum NodeAccessibility {
PUBLIC,
PRIVATE
}
public class Node {
private int line = 0;
private int column = 0;
// Notice that this Object usage might involve some boxing for attributes of premitive type
private HashMap<NodeAttribute, Object> additionalAttributes = new HashMap<NodeAttribute, Object>();
/* Getters and setters for line/column. */
/* ... */
public boolean hetAttribute(NodeAttribute attribute) {
return this.additionalAttributes.containsKey(attribute);
}
public <T> T getAttributeValue(NodeAttribute attribute, Class<T> attributeClass) {
Object attributeValue = this.additionalAttributes.get(attribute);
// You may want to wrap the ClassCastException that may be raisen here to a more specfic error
T castedAttributeValue = attributeClass.cast(attributeValue);
return castedAttributeValue;
}
public void setAttributeValue(NodeAttribute attribute, Object value) {
// Notice that this implemintation allows changing the type of an existing attribute,
// If this is invalid behavior in your case you can throw an exception instead
this.additionalAttributes.put(attribute, value);
}
}
// Example usage
public class Program {
public static void main(String[] args) {
Node nodeWithNameOnly = new Node();
nodeWithNameOnly.setAttributeValue(NodeAttribute.NAME, 'node1');
Node nodeWithBoth = new Node();
nodeWithBoth.setAttributeValue(NodeAttribute.NAME, 'node2');
nodeWithBoth.setAttributeValue(NodeAttribute.ACCESSIBILTY, NodeAccessibility.PRIVATE);
Program.doStuffWithNode(nodeWithNameOnly);
/* output:
Node name: node1
*/
Program.doStuffWithNode(nodeWithBoth);
/* output:
Node name: node2
Node is public: False
*/
}
public static void doStuffWithNode(Node node) {
if (nodeWithNameOnly.hetAttribute(NodeAttribute.NAME)) {
String nodeName = nodeWithNameOnly.getAttributeValue(NodeAttribute.NAME, String.class);
system.out.println("Node name: " + nodeName);
}
if (nodeWithNameOnly.hetAttribute(NodeAttribute.ACCESSIBILTY)) {
NodeAccessibility nodeAccessibilty =
nodeWithNameOnly.getAttributeValue(NodeAttribute.ACCESSIBILTY, NodeAccessibility.class);
boolean nodeIsPublic = nodeAccessibilty == NodeAccessibility.PUBLIC;
system.out.println("Node is public: " + String.valueOf(nodeIsPublic));
}
}
}
在任何情况下,这都是主要的经验法则-继承应用于“是”关系,而合成应用于“具有”关系。
例如:
在我们的例子中,节点具有名称和可访问性级别,因此它应该包含它们。