GOF23设计模式之组合模式之实现


/**
 * 2015年4月6日18:37:49
 * 抽象组件,相当于组合模式中的抽象组件
 */
package com.bjsxt.cn.composite;
public interface Component {
 void operation();
}
/**
 * 叶子组件
 * @author wanna_000
 *
 */
interface Leaf extends Component {
 
}
/**
 * 一般在Composite含有一个children的成员变量,保存了所有子节点的引用。
 * @author wannachan@outlook.com
 *
 */
/**
 * 容器构件
 * @author wanna_000
 *
 */
interface Composite extends Component {
 void add(Component c);
 void remove(Component c);
 Component getChildComponent(int index);
 
}




package com.bjsxt.cn.composite;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * 抽象构件
 * @author wanna_000
 *
 */
public interface AbstractFile {
 void killCirus();
}
class ImageFile implements AbstractFile {
 private String name;
 
 
 public ImageFile(String name) {
  super();
  this.name = name;
 }
 @Override
 public void killCirus() {
  
  System.out.println("图片文件:" + name+",正在查杀");
 }
 
}
class TxtFile implements AbstractFile {
 private String name;
 
 
 public TxtFile(String name) {
  super();
  this.name = name;
 }
 @Override
 public void killCirus() {
  
  System.out.println("文本文件:" + name+ ", 正在查杀");
 }
 
}
class VideoFile implements AbstractFile {
 
 private String name;
 
 
 public VideoFile(String name) {
  super();
  this.name = name;
 }
 @Override
 public void killCirus() {
  
  System.out.println("视频文件:" + name+ ", 正在查杀");
 }
 
}
class Folder implements AbstractFile {
 //定义容器,用来存放本节点的所有子节点的引用
 private List<AbstractFile> list = new ArrayList<AbstractFile>();
 private String name;
 
 public Folder(String name) {
  super();
  this.name = name;
 }
 
 public void add(AbstractFile af) {
  list.add(af);
 }
 
 public void remove(AbstractFile af) {
  list.remove(af);
 }
 
 public AbstractFile getChildComponent(int index) {
  return list.get(index);
 }
 @Override
 public void killCirus() {
  System.out.println("文件夹:" +name + ",正在查杀");
//  for (Iterator iterator = list.iterator(); iterator.hasNext();) {
//   AbstractFile abstractFile = (AbstractFile) iterator.next();
//   abstractFile.killCirus();
//  }
  
  for (AbstractFile file : list) {
   file.killCirus();
  }
 }
 
}



/*
 *  测试组合模式
 * 2015年4月6日19:07:46
 * 深刻理解组合模式的三个组成部分,以及可以使用同统一的方式,处理部分对象和整体对象
 * */
package com.bjsxt.cn.composite;
public class Client {
 public static void main(String[] args) {
  Folder f4, f1;
  AbstractFile f2, f3,  f5;
  f1 = new Folder("我的收藏");
  f2 = new TxtFile("周杰伦.txt");
  f3 = new ImageFile("我的大头贴");
  f4 = new Folder("电影") ;
  f5 = new VideoFile("笑傲江湖");
  
  f1.add(f2);
  f1.add(f3);
  f1.killCirus();
  System.out.println("++++++++++++++++++++");
  f4.add(f1);
  f4.add(f5);
  f4.killCirus();
 }
}
/*
 * 
文件夹:我的收藏,正在查杀
文本文件:周杰伦.txt, 正在查杀
图片文件:我的大头贴,正在查杀
++++++++++++++++++++
文件夹:电影,正在查杀
文件夹:我的收藏,正在查杀
文本文件:周杰伦.txt, 正在查杀
图片文件:我的大头贴,正在查杀
视频文件:笑傲江湖, 正在查杀
 * 
 * */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值