每日随笔——组合模式

[实验任务一]:组合模式

用透明组合模式实现教材中的“文件夹浏览”这个例子。

实验要求:

1.文件的执行不需真正实现,只需简单提示即可;

2.提交源代码;

3.注意编程规范。

 

AbstractFile
package SC10;

public abstract class AbstractFile {

    public abstract void add(AbstractFile ele);
    public abstract void remove(AbstractFile ele);
    public abstract void display();

}
Client
package SC10;

public class Client {
    public static void main(String a[])
    {
        AbstractFile obj1,obj2,obj3,obj4,obj5;
        Folder plate1,plate2,plate3;
        obj1=new ImageFile("aaa.png");
        obj2=new TextFile("bbb.txt");
        plate1=new Folder("ccc");
        plate1.add(obj1);
        plate1.add(obj2);

        obj3=new VideoFile("ddd.mp3");
        obj4=new VideoFile("eee.mp3");
        plate2=new Folder("fff");
        plate2.add(obj3);
        plate2.add(obj4);

        obj5=new ImageFile("ggg.jpg");
        plate3=new Folder("hhh");
        plate3.add(plate1);
        plate3.add(plate2);
        plate3.add(obj5);

        plate3.display();
    }
}

Folder
package SC10;

import java.util.ArrayList;

public class Folder extends AbstractFile{

    private String filename;
    private ArrayList al=new ArrayList<>();


    public Folder(String f) {
        filename=f;
    }
    @Override
    public void add(AbstractFile ele) {
        // TODO Auto-generated method stub
        al.add(ele);
        System.out.println("Folder add");
    }

    @Override
    public void remove(AbstractFile ele) {
        // TODO Auto-generated method stub
        al.remove(ele);
        System.out.println("Folder remove");
    }

    @Override
    public void display() {
        // TODO Auto-generated method stub
        for(Object object:al)
        {
            ((AbstractFile)object).display();
        }
        System.out.println("Folder:display");

    }

}

ImageFile
package SC10;

public class ImageFile extends AbstractFile{

    private String filename;

    public ImageFile(String f) {
        filename=f;
    }
    @Override
    public void add(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("添加Image文件");
    }

    @Override
    public void remove(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("删除Image文件");
    }

    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("展示Image文件"+filename);
    }

}

TextFile
package SC10;

public class TextFile extends AbstractFile{

    private String filename;

    public TextFile(String f) {
        filename=f;
    }
    @Override
    public void add(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("添加Text文件");
    }

    @Override
    public void remove(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("删除Text文件");
    }

    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("展示Text文件"+filename);
    }

}

VideoFile
package SC10;

public class VideoFile extends AbstractFile{

    private String filename;

    public VideoFile(String f) {
        filename=f;
    }
    @Override
    public void add(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("添加Video文件");
    }

    @Override
    public void remove(AbstractFile ele) {
        // TODO Auto-generated method stub
        System.out.println("删除Video文件");
    }

    @Override
    public void display() {
        // TODO Auto-generated method stub
        System.out.println("展示Video文件"+filename);
    }

}

 

 

 

posted @ 2023-11-17 22:46  伽澄  阅读(13)  评论(0编辑  收藏  举报