package com.test.composite;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Demo {
public static void main(String[] args) {
Date d = new Date();
Dir f1 = new Dir("我的收藏", d);
d.setYear(2012);
Dir f2 = new Dir("图片", d);
Dir f3 = new Dir("音乐", d);
d.setYear(2013);
ActualFile f4 = new ActualFile("喜洋洋与灰太狼.avi", d);
f1.add(f4);
ActualFile f5 = new ActualFile("taiyanghua.jpg", d);
ActualFile f6 = new ActualFile("变形精钢.jpg", d);
f2.add(f5);
f2.add(f6);
f1.add(f2);
f1.add(f3);
f1.showFile();
}
}
/**
* 首先目录和文件都属于文件,所以我们可以抽象一个抽象文件出来
*/
interface AbstractFile {
/**
* 展示文件方法
*/
public void showFile();
}
/**
* 真实文件
*/
class ActualFile implements AbstractFile {
private String name;
private Date createDate;
public ActualFile(String name, Date createDate) {
this.name = name;
this.createDate = createDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/**
* 实现抽象文件类的展示文件方法
*/
public void showFile() {
System.out.println("文件名:"+this.name+"--创建时间:"+this.createDate.getTime());
}
}
/**
* 目录文件
*/
class Dir implements AbstractFile {
private String name;
private Date createDate;
/**
* 作为目录文件,会多出一个子文件列表
*/
private List<AbstractFile> list = new ArrayList<>();
public Dir(String name, Date createDate) {
super();
this.name = name;
this.createDate = createDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
/**
* 目录文件的添加操作,为目录添加子文件或者子目录
*/
public void add(AbstractFile f){
this.list.add(f);
}
/**
* 目录文件的删除操作,删除子文件或者子目录
*/
public void remove(AbstractFile f){
this.list.remove(f);
}
/**
* 目录文件的获取操作,获取目录下面的子文件或者子目录
*/
public AbstractFile getIndex(int index){
return this.list.get(index);
}
public void showFile() {
System.out.println("目录名:"+this.name+"--创建时间:"+this.createDate.getTime());
for(AbstractFile f:list){
f.showFile();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有