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

来源:互联网 发布:月季 淘宝推荐 编辑:程序博客网 时间:2024/05/21 11:20

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

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

其代码结构如下:

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

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 0
原创粉丝点击