Java设计模式泛型化之组合模式

24 篇文章 1 订阅
17 篇文章 0 订阅

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

一个“根”节点
几个“枝”节点
几个“叶”节点

其代码结构如下:

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

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<Component> subordinaryList = new ArrayList<Component>();
    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<Component> 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<Component> 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<Component> subordinaryList = new ArrayList<Component>();

    public CompositeBranch(String name, int position, String content) {
        super(name, position, content);
        this.name = name;
        this.position = position;
        this.content = content;
    }

    public List<Component> 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<Component> 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<T extends Component> extends Component {

    private String name = "";
    private int position = 0;
    private String content = "";
    private List<T> subordinaryList = new ArrayList<T>();
    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<T> 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<T> 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<T extends Component> extends Component {

    private String name = "";
    private int position = 0;
    private String content = "";
    private List<T> subordinaryList = new ArrayList<T>();

    public CompositeBranch(String name, int position, String content) {
        super(name, position, content);
        this.name = name;
        this.position = position;
        this.content = content;
    }

    public List<T> 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<T> it = subordinaryList.iterator();
        while (it.hasNext()) {
            Component comp = it.next();
            comp.print();
        }
    }

}

public class CompositeCaller {

    public static void main(String[] args) {
        CompositeRoot<Component> root = new CompositeRoot<Component>("Animal", 0, "Animal");
        CompositeBranch<Component> cat = new CompositeBranch<Component>("Cat", 1, "Cat");
        CompositeBranch<Component> dog = new CompositeBranch<Component>("Dog", 1, "Dog");
        CompositeBranch<Component> duck = new CompositeBranch<Component>("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();
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值