java组合模式 节点,Java设计模式泛型化之组合模式 | 学步园

本文介绍了一个使用组合模式构建树形结构的例子,包括抽象组件、根节点、枝节点和叶节点的定义。通过泛型化,使得根节点和枝节点能够处理任意类型的组件,增强了代码的灵活性。在示例中,创建了一个动物分类的树结构,并展示了如何添加和打印节点。泛型化的修改使得结构更加通用,能够适应更多场景。
摘要由CSDN通过智能技术生成

组合模式通常用来描述一个类似树状的结构。

一个“根”节点

几个“枝”节点

几个“叶”节点

其代码结构如下:

抽象组件层(视为任何一种类型节点的抽象)

public abstract class Component {

private String name = "";

private int position = 0;

private String content = "";

private Component parent = null;

public Component(String name, int position, String content) {

this.name = name;

this.position = position;

this.content = content;

}

public void print() {

System.out.println("------------------");

System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content);

}

public Component getParent() {

return this.parent;

}

protected void setParent(Component parent) {

this.parent = parent;

}

}

”根“结构

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class CompositeRoot extends Component {

private String name = "";

private int position = 0;

private String content = "";

private List subordinaryList = new ArrayList();

private Component root = null;

public CompositeRoot(String name, int position, String content) {

super(name, position, content);

this.name = name;

this.position = position;

this.content = content;

}

protected void setRoot(Component root) {

this.root = root;

}

public Component getRoot() {

return this.root;

}

public List getSubordinary() {

return this.subordinaryList;

}

public void add(Component component) {

component.setParent(this);

subordinaryList.add(component);

}

@Override

public void print() {

System.out.println("--------------");

System.out.println("Name: " + this.name + "\nPosition: "

+ this.position + "\nContent: " + this.content + " (Root)");

Iterator it = subordinaryList.iterator();

while (it.hasNext()) {

Component comp = it.next();

comp.print();

}

}

}

”树枝“结构

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class CompositeBranch extends Component {

private String name = "";

private int position = 0;

private String content = "";

private List subordinaryList = new ArrayList();

public CompositeBranch(String name, int position, String content) {

super(name, position, content);

this.name = name;

this.position = position;

this.content = content;

}

public List getSubordinary() {

return this.subordinaryList;

}

public void add(Component component) {

component.setParent(this);

subordinaryList.add(component);

}

@Override

public void print() {

System.out.println("------------------");

System.out.println("Name: " + this.name + "\nPosition: "

+ this.position + "\nContent: " + this.content + " (Branch)");

Iterator it = subordinaryList.iterator();

while (it.hasNext()) {

Component comp = it.next();

comp.print();

}

}

}

”叶子“结构

public class CompositeLeaf extends Component {

private String name = "";

private int position = 0;

private String content = "";

public CompositeLeaf(String name, int position, String content) {

super(name, position, content);

this.name = name;

this.position = position;

this.content = content;

}

@Override

public void print() {

System.out.println("--------------");

System.out.println("Name: " + this.name + "\nPosition: "

+ this.position + "\nContent: " + this.content + " (Leaf)");

}

}

统一调用类

public class Invoker {

private Component node;

public Invoker(Component node) {

this.node = node;

}

public void printNode() {

node.print();

}

}

调用者

public class CompositeCaller {

public static void main(String[] args) {

CompositeRoot root = new CompositeRoot("Animal", 0, "Animal");

CompositeBranch cat = new CompositeBranch("Cat", 1, "Cat");

CompositeBranch dog = new CompositeBranch("Dog", 1, "Dog");

CompositeBranch duck = new CompositeBranch("Duck", 1, "Duck");

CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck");

CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck");

root.add(cat);

root.add(dog);

root.add(duck);

duck.add(peikingDuck);

duck.add(blackDuck);

Invoker invoker = new Invoker(root);

invoker.printNode();

}

}

那么,如何泛型化呢?

其实,在这个结构中,重要的部分就是”树枝“结构或者”根“结构。因为,只有这两个部分才是对整个”树“的结构进行维护的。其中,”树枝“结构可以包含”根“结构。

以下是泛型化代码:

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class CompositeRoot extends Component {

private String name = "";

private int position = 0;

private String content = "";

private List subordinaryList = new ArrayList();

private Component root = null;

public CompositeRoot(String name, int position, String content) {

super(name, position, content);

this.name = name;

this.position = position;

this.content = content;

}

protected void setRoot(T root) {

this.root = root;

}

public Component getRoot() {

return this.root;

}

public List getSubordinary() {

return this.subordinaryList;

}

public void add(T component) {

component.setParent(this);

subordinaryList.add(component);

}

@Override

public void print() {

System.out.println("--------------");

System.out.println("Name: " + this.name + "\nPosition: "

+ this.position + "\nContent: " + this.content + " (Root)");

Iterator it = subordinaryList.iterator();

while (it.hasNext()) {

Component comp = it.next();

comp.print();

}

}

}

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class CompositeBranch extends Component {

private String name = "";

private int position = 0;

private String content = "";

private List subordinaryList = new ArrayList();

public CompositeBranch(String name, int position, String content) {

super(name, position, content);

this.name = name;

this.position = position;

this.content = content;

}

public List getSubordinary() {

return this.subordinaryList;

}

public void add(T component) {

component.setParent(this);

subordinaryList.add(component);

}

@Override

public void print() {

System.out.println("------------------");

System.out.println("Name: " + this.name + "\nPosition: "

+ this.position + "\nContent: " + this.content + " (Branch)");

Iterator it = subordinaryList.iterator();

while (it.hasNext()) {

Component comp = it.next();

comp.print();

}

}

}

public class CompositeCaller {

public static void main(String[] args) {

CompositeRoot root = new CompositeRoot("Animal", 0, "Animal");

CompositeBranch cat = new CompositeBranch("Cat", 1, "Cat");

CompositeBranch dog = new CompositeBranch("Dog", 1, "Dog");

CompositeBranch duck = new CompositeBranch("Duck", 1, "Duck");

CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck");

CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck");

root.add(cat);

root.add(dog);

root.add(duck);

duck.add(peikingDuck);

duck.add(blackDuck);

Invoker invoker = new Invoker(root);

invoker.printNode();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值