关闭

组合模式实例之文件浏览

396人阅读 评论(0) 收藏 举报
分类:

一.实例说明:

       文件有不同的类型,不同类型的文件其浏览方式有所区别,如文本文件和图片文件的浏览方式就不同。对文件夹的浏览实际上就是对其中所包含文件的浏览,而客户端可以一致地对文件和文件进行操作,无须关心它们的区别。使用组合模式来浏览文件的浏览操作。

二.实例类图:


代码:

package MyElement;

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

package feaffile;

class Video:

import MyElement.AbstractFile;

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");
	}
}

class TextFile:

import MyElement.AbstractFile;

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");
	}
}

class ImageFile:

</pre><pre class="java" name="code">import MyElement.AbstractFile;

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("ImageFile:add");
	}
	public void remove(AbstractFile element)
	{
		System.out.println("ImageFile:remove");
	}
	public void display()
	{
		System.out.println(fileName);
		System.out.println("ImageFile:display");
	}
	
}

class Floder:

import java.util.*;

import MyElement.AbstractFile;

public class Folder extends AbstractFile
{
	private String fileName;
	private ArrayList fileList=new ArrayList();
	public Folder(String filename)
	{
		this.fileName=new String();
		this.fileName=filename;
	}
	public void add(AbstractFile element)
	{
		fileList.add(element);
		System.out.println("Folder:add");
	}
	public void remove(AbstractFile element)
	{
		fileList.remove(element);
		System.out.println("Folder:remove");
	}
	public void display()
	{
		for(Object object:fileList)
		{
			((AbstractFile)object).display();
		}
		System.out.println("Folder:display");
	}
}

package Client;

class Client:

import MyElement.AbstractFile;
import feaffile.Folder;
import feaffile.ImageFile;
import feaffile.TextFile;
import feaffile.VideoFile;
public class Client 
{
	public static void main(String a[])
	{
		AbstractFile obj1,obj2,obj3,obj4,obj5;
		Folder plate1,plate2,plate3;
		obj1=new ImageFile("aaa");
		obj2=new TextFile("bbb");
		plate1=new Folder("ccc");
		plate1.add(obj1);
		plate1.add(obj2);
		
		obj3=new VideoFile("ddd");
		obj4=new VideoFile("eee");
		plate2=new Folder("fff");
		plate2.add(obj3);
		plate2.add(obj4);
		
		obj5=new ImageFile("ggg");
		plate3=new Folder("hhh");
		plate3.add(plate1);
		plate3.add(plate2);
		plate3.add(obj5);
		
		plate3.display();
		
	}
}






 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33450次
    • 积分:610
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:7篇
    • 译文:0篇
    • 评论:5条
    博客专栏
    文章分类
    最新评论