软件设计——组合模式之文件夹浏览

1、类图

2、代码

2.1 AbstractFile.java 

package test10;

public abstract class AbstractFile 
{
	public abstract void add(AbstractFile element);
	public abstract void remove(AbstractFile element);
	public abstract void display();
}

2.2 ImageFile.java

package test10;

public class ImageFile extends AbstractFile
{
	private String fileName;
	public ImageFile(String filename)
	{
		this.fileName=new String();
		this.fileName=filename;
	}
	public void add(AbstractFile element)
	{
		//System.out.println("添加 "+element+" 文件");
	}
	public void remove(AbstractFile element)
	{
		//System.out.println("ImageFile:remove");
	}
	public void display()
	{
		System.out.println(fileName);
		
	}
	
}

2.3 VideoFile.java

package test10;

public class VideoFile extends AbstractFile
{
	private String fileName;
	public VideoFile(String filename)
	{
		this.fileName=new String();
		this.fileName=filename;
	}
	public void add(AbstractFile element)
	{
		//System.out.println("VideoFile:add");
	}
	public void remove(AbstractFile element)
	{
		//System.out.println("VideoFile:remove");
	}
	public void display()
	{
		System.out.println(fileName);
		//System.out.println("VideoFile:display");
	}
}

2.4 TextFile.java

package test10;

public class TextFile extends AbstractFile
{
	private String fileName;
	public TextFile(String filename)
	{
		this.fileName=new String();
		this.fileName=filename;
	}
	public void add(AbstractFile element)
	{
		//System.out.println("TextFile:add");
	}
	public void remove(AbstractFile element)
	{
		//System.out.println("TextFile:remove");
	}
	public void display()
	{
		System.out.println(fileName);
		//System.out.println("TextFile:display");
	}
}

2.5 Folder.java

package test10;

import java.util.*;
 
public class Folder extends AbstractFile
{
	private String fileName;
	private int level;
	private ArrayList fileList=new ArrayList();
	public Folder(String filename,int level)
	{
		this.fileName=filename;
		this.level=level;
	}
	public Folder(String filename)
	{
		this.fileName=filename;
	}
	public void add(AbstractFile element)
	{
		fileList.add(element);
	}
	public void remove(AbstractFile element)
	{
		fileList.remove(element);
	}
	public void display()
	{
		System.out.println(fileName);
		for(Object object:fileList)
		{
			if(this.level!=1) {
				System.out.print("  +");
				((AbstractFile)object).display();
			}else {
				System.out.print("      *");
				((AbstractFile)object).display();
			}
		}
	}
		//System.out.println("Folder:display");
	}

2.6 Client.java

package test10;

import java.util.Scanner;

public class Client 
{
	public static void main(String a[])
	{

		AbstractFile obj1,obj2,obj3,obj4,obj5,obj6;  //5个文件
		Folder fold1,fold2,fold3;  //3个文件夹
		obj1=new ImageFile("中国地图.png");
		obj2=new TextFile("中国地貌概览.txt");
		fold1=new Folder("中国地理",1);
		fold1.add(obj1);
		fold1.add(obj2);  //中国地理里有中国地图.png和中国地貌概览.txt

		
		obj3=new VideoFile("唐朝文化简介.mp3");
		obj4=new VideoFile("明朝文化简介.mp3");
		fold2=new Folder("中国古代文化",1);
		fold2.add(obj3); 
		fold2.add(obj4);   //中国古代文化里有唐朝文化简介.mp3和明朝文化简介.mp3

		
		obj5=new ImageFile("长城.png");
		obj6=new TextFile("故宫历史简介.txt");
		fold3=new Folder("中国地理文化知识");
		fold3.add(fold1);
		fold3.add(fold2);
		fold3.add(obj5);   //中国地理文化知识里有中国地理、中国古代文化和长城.png
		fold3.add(obj6);
		fold3.display();	
	}
}

3、运行截图

posted @ 2021-11-18 22:05  第厘  阅读(68)  评论(0编辑  收藏  举报