zoukankan      html  css  js  c++  java
  • 组合模式

    组合模式:将对象组合成树形结构以表示"部分-整体"的层次结构。"Composite使得用户对单个对象和组合对象的使用具有一致性。"

    public abstract class AComponent {

    protected String name;
    public AComponent(String name){
    System.out.println(name);
    }
    abstract public void Add(AComponent c); //添加结点
    abstract public void Remove(AComponent c); //移除结点
    abstract public void Display(int i); //输出结点结构
    }

    //组合类 

    public class Composite extends AComponent{

    static AComponent component;

    private ArrayList children = new ArrayList();

    public Composite(String name) {

    super(name);  }

    public void Add(AComponent c) {

    this.component = c; children.add(c); }

    public void Remove(AComponent c) {

    children.remove(c); }

    public void Display(int i) {

    Iterator iter = children.iterator();

    AComponent c = null;

    c = (AComponent) iter.next();

    while(iter.hasNext()&&iter.next().equals(null))

    { System.out.println("abc------"+c.name); } } }

    //单对象类

    public class Leaf extends AComponent{  

    public Leaf(String name) {

    super(name); }

    public void Add(AComponent c) {

    System.out.println("不能添加子项!"); }

    public void Remove(AComponent c) {

    System.out.println("不能移除子项!"); }

    public void Display(int i) {

    System.out.println("abc"+new String()+name); } }

    //测试类 

    public class Test {

    public static void main(String[] args) {
    Composite root = new Composite("根目录");
    root.Add(new Leaf("---子项A"));
    root.Add(new Leaf("---子项B"));
    Composite comp = new Composite("组合X");
    comp.Add(new Leaf("---子项XA"));
    comp.Add(new Leaf("---子项XB"));
    root.Add(comp);
    root.Add(new Leaf("---子项C"));
    Leaf l = new Leaf("---子项D");
    root.Add(l);
    root.Remove(l);
    root.Display(2);
    }
    }

  • 相关阅读:
    bzoj4407 于神之怒加强版
    bzoj4600 [Sdoi2016]硬币游戏
    javascript 笔记(1)
    thinkphp 杂乱笔记(1)
    think ajax 应用
    jquery ajax post, get, javascript ajax post, get 处理
    文件操作 系统备份和还原,压缩,解压 tar dump/restore
    查看系统信息
    进程显示,删除,调度 ps, top kill
    相看系统中用户的信息 passwd, shadow
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2511486.html
Copyright © 2011-2022 走看看