11.17

 

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

实验要求:

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

2.提交源代码;

3.注意编程规范。

 

类图:

 

 

源代码:

 

AbstractFile.java

 

public abstract class AbstractFile {

 

    public abstract void add(AbstractFile ele);

    public abstract void remove(AbstractFile ele);

    public abstract void display();

 

}

 

Folder.java

 

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.java

 

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.java

 

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.java

 

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

    }

 

}

 

Cient.java

 

public class Client {

    public static void main(String a[])

    {

        AbstractFile obj1,obj2,obj3,obj4,obj5;

        Folder plate1,plate2,plate3;

        obj1=new ImageFile("one.png");

        obj2=new TextFile("two.txt");

        plate1=new Folder("three");

        plate1.add(obj1);

        plate1.add(obj2);

        

        obj3=new VideoFile("four.mp3");

        obj4=new VideoFile("five.mp3");

        plate2=new Folder("seven");

        plate2.add(obj3);

        plate2.add(obj4);

        

        obj5=new ImageFile("eight.jpg");

        plate3=new Folder("nine");

        plate3.add(plate1);

        plate3.add(plate2);

        plate3.add(obj5);

        

        plate3.display();

  }

}

posted on 2024-11-17 21:45  HA_wind  阅读(3)  评论(0编辑  收藏  举报

导航