學習Markdown語法&自我介紹
阿新 • • 發佈:2022-01-16
public class TreeNode {
private Integer x;
private Integer y;
private Tree tree;
public TreeNode(Integer x, Integer y, Tree tree) {
this.x = x;
this.y = y;
this.tree = tree;
}
public static void main(String[] args) {
TreeNode treeNode1 = new TreeNode(10, 20, TreeFactory.getTree("name1", "data1"));
TreeNode treeNode2 = new TreeNode(11, 21, TreeFactory.getTree("name1", "data1"));
TreeNode treeNode3 = new TreeNode(12, 22, TreeFactory.getTree("name2", "data1"));
}
}
@Getter
public class Tree {
private final String name;
private final String data;
public Tree(String name, String data) {
System.out.println("name = " + name + ", data = " + data);
this.name = name;
this.data= data;
}
}
public class TreeFactory {
private static Map<String, Tree> map = new ConcurrentHashMap<>();
public static final Tree getTree(String name, String data) {
if (map.containsKey(name)) {
return map.get(name);
}
Tree tree = new Tree(name, data);
map.put(name, tree);
return tree;
}
}
列印結果:
name = name1, data = data1
name = name2, data = data1