[实验任务一]:组合模式
用透明组合模式实现教材中的“文件夹浏览”这个例子。
实验要求:
1. 文件的执行不需真正实现,只需简单提示即可;
2. 提交源代码;
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();
}
}
posted @
2021-10-12 23:12
Zwyooo
阅读(
75)
评论()
编辑
收藏
举报