搜索

组合模式java+c++


发布时间: 2022-11-24 21:43:00    浏览次数:35 次

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

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

 

 

 

 

package test10;

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

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

        obj3=new VideoFile("c.mp4");
        plate2=new Folder("Image_Video");
        plate2.add(obj1);
        plate2.add(obj3);

        obj4=new ImageFile("d.jpg");
        plate3=new Folder("Image_Text_Video_Folder");
        plate3.add(plate1);
        plate3.add(plate2);
        plate3.remove(plate1);
        plate3.add(obj4);

        plate3.display();

    }
}
package test10;

import java.util.ArrayList;

public class Folder extends AbstractFile {
    private String fileName;
    private ArrayList<AbstractFile> 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("add Folder");
    }
    public void remove(AbstractFile element)
    {
        fileList.remove(element);
        System.out.println("remove Folder");
    }
    public void display()
    {
        System.out.println(fileName);
        for(Object object:fileList)
        {
            ((AbstractFile)object).display();
        }
        System.out.println("****Folder display****");
    }
}
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("add ImageFile");
    }
    public void remove(AbstractFile element)
    {
        System.out.println("remove ImageFile");
    }
    public void display()
    {
        System.out.print(fileName);
        System.out.println(" ImageFile");
    }
}
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("add TextFile");
    }
    public void remove(AbstractFile element)
    {
        System.out.println("remove TextFile");
    }
    public void display()
    {
        System.out.print(fileName);
        System.out.println(" TextFile");
    }
}
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("add VideoFile");
    }
    public void remove(AbstractFile element)
    {
        System.out.println("remove VideoFile");
    }
    public void display()
    {
        System.out.print(fileName);
        System.out.println(" VideoFile");
    }
}

 

 

#include <iostream>
#include <string>
#include<list>
using namespace std;

class AbstractFile
{
public:
    virtual void add(AbstractFile* abstractfile){};
    virtual void remove(AbstractFile* abstractfile){};
    virtual void display(){};
};

class ImageFile: public AbstractFile
{
private:
    string fileName;
public:
    ImageFile(string filename)
    {
        fileName = filename;
    }
    void add(AbstractFile* abstractfile)
    {
        cout << "add ImageFile" << endl;
    }
    void remove(AbstractFile* abstractfile)
    {
        cout << "remove ImageFile" << endl;
    }
    void display()
    {
        cout << fileName <<" ImageFile"<< endl;
    }
};


class TextFile : public AbstractFile
{
private:
    string fileName;
public:
    TextFile(string filename)
    {
        fileName = filename;
    }
    void add(AbstractFile* abstractfile)
    {
        cout << "add TextFile" << endl;
    }
    void remove(AbstractFile* abstractfile)
    {
        cout << "remove TextFile" << endl;
    }
    void display()
    {
        cout << fileName <<" TextFile"<< endl;
    }
};

class VideoFile : public AbstractFile
{
private:
    string fileName;
public:
    VideoFile(string filename)
    {
        fileName = filename;
    }
    void add(AbstractFile* abstractfile)
    {
        cout << "add VideoFile" << endl;
    }
    void remove(AbstractFile* abstractfile)
    {
        cout << "remove VideoFile" << endl;
    }
    void display()
    {
        cout << fileName <<" VideoFile"<< endl;
    }
};

class Folder: public  AbstractFile
{
private:
    string fileName;
    int level;
    list<AbstractFile*> abstractfiles;

public:
    Folder(string filename)
    {
        fileName = filename;
    }
    Folder(string filename,int level)
    {
        fileName = filename;
        this->level = level;
    }
    void add(AbstractFile* abstractfile)
    {
        abstractfiles.push_front(abstractfile);
    }
    void remove(AbstractFile* abstractfile)
    {
        abstractfiles.remove(abstractfile);
    }
    void  display()
    {
        cout << fileName << endl;
        list<AbstractFile*>::iterator iter = abstractfiles.begin();
        for (; iter != abstractfiles.end(); iter++)
        {
            if (this->level != 1)
            {
                cout << "  " ;
                (*iter)->display();
            }
            else {
                cout << "     " ;
                (*iter)->display();
            }
        }
    }
};

int main()
{
    AbstractFile *obj1,*obj2,*obj3,*obj4;
    Folder *fold1,*fold2,*fold3;
    obj1 = new ImageFile("a.jpg");
    obj2 = new TextFile("b.txt");
    fold1 = new Folder("Image_Text", 1);
    fold1->add(obj1);
    fold1->add(obj2);

    obj3 = new VideoFile("c.mp4");
    fold2 = new Folder("Image_Video", 1);
    fold2->add(obj1);
    fold2->add(obj3);

    obj4 = new ImageFile("d.jpg");
    fold3 = new Folder("Image_Text_Video_Folder");
    fold3->add(fold1);
    fold3->add(fold2);
    fold3->remove(fold1);
    fold3->add(obj4);
    fold3->display();

    delete obj1, obj2, obj3, obj4;
    delete fold1, fold2, fold3;
}

 

免责声明 组合模式java+c++,资源类别:文本, 浏览次数:35 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 09:43:00。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/rongzhang/p/16923544.html