当前位置:首页 > 设计模式 > 正文内容

组合模式

canca19年前 (2007-03-26)设计模式481

//组合模式
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.

import java.util.*;

public class ComponantMode{
 public static void main(String[] args){
  Componant Root = new compositionElement("Books");
  Root.Add(new leaf("Java 设计模式"));
  Root.Add(new leaf("C# 高级编程"));
  Root.Add(new leaf("VB 程序设计"));
  Componant js = new leaf("JavaScript 高级编程");
  Root.Add(js);
  Componant food = new compositionElement("Foods");
  food.Add(new leaf("Apple"));
  food.Add(new leaf("Banana"));
  food.Add(new leaf("Pear"));
  Root.Add(food);
  Root.Display(1);
  Root.Remove(js);
  System.out.println();
  Root.Display(1);
 } 
}

abstract class Componant{
 protected String name;
 protected final byte[] BLANK = "-----------------------------".getBytes();
 public Componant(String name){
  this.name = name; 
 }
 public abstract void Add(Componant componant);
 public abstract void Remove(Componant componant);
 public abstract void Display(int indent);
}

class leaf extends Componant{
 
 public leaf(String name){
  super(name);
 }
 
 public void Add(Componant componant){
  System.out.println("Leaf can't Add.");
 }
 
 public void Remove(Componant componant){
  System.out.println("Leaf can't Remove.");
 }
 
 public void Display(int indent){
  if(indent > BLANK.length - 1)indent = BLANK.length - 1;
  System.out.println(new String(BLANK,0,indent) + " " + this.name); 
 }
}

class compositionElement extends Componant{
 
 private ArrayList elements = new ArrayList();
 
 public compositionElement(String name){
  super(name);
 }
 
 public void Add(Componant componant){
  this.elements.add(componant);
 }
 
 public void Remove(Componant componant){
  this.elements.remove(componant);
 }
 
 public void Display(int indent){
  if(indent > BLANK.length - 1)indent = BLANK.length - 1;
  System.out.println(new String(BLANK,0,indent) + "+ " + this.name);
  for(int i = 0 ; i < this.elements.size() ; i++){
   ((Componant)elements.get(i)).Display(indent + 2);
  }
 }
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/658.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:装饰模式

下一篇:备忘录模式

“组合模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class Real_World_Example{ public static void...

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class BuilderMode{ public static void main(String[] arg...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ public static void main(String[] args){  ...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。