设计模式-组合模式

题目:

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

 

 

Java代码

package t10;

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

    }
}






package t10;

public abstract class AbstractFile {
    public abstract void add(AbstractFile element);

    public abstract void remove(AbstractFile element);

    public abstract void display();
}


package t10;

import java.util.ArrayList;

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

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

}


package t10;

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


package t10;

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

 

 

c++代码

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

class Component
{
protected:
    string name;
public:
    Component(string name)
        :name(name)
    {   }
    virtual void AddComponent(Component *component) {   }
    virtual void RemoveComponent(Component *component)  {   }
    virtual void GetChild(int depth)    { }
};

class Leaf: public Component
{
public:
    Leaf(string name)
        :Component(name)
    {   }
    void AddComponent(Component *component)
    {
        cout<<"can't add"<<endl;
    }
    void RemoveComponent(Component *component)
    {
        cout<<"can't remove"<<endl;
    }
    void GetChild(int depth)
    {
        string _tmpstring(depth, '-');
        cout<<_tmpstring<<name<<endl;
    }
};

class Composite:public Component
{
private:
    list<Component*> _componets;

public:
    Composite(string name)
        :Component(name)
    { }
    void AddComponent(Component *component)
    {
        _componets.push_back(component);
    }
    void RemoveComponent(Component *component)
    {
        _componets.remove(component);
    }
    void GetChild(int depth)
    {
        string tmpstring (depth, '-');
        cout<<tmpstring<<name<<endl;
        list<Component*>::iterator iter = _componets.begin();
        for(; iter != _componets.end(); iter++)
        {
            (*iter)->GetChild(depth + 2);
        }
    }
};

main()
{
    Composite *root = new Composite("根目录");
    Leaf *leaf1 = new Leaf("A");
    Leaf *leaf2 = new Leaf("B");
    root->AddComponent(leaf1);
    root->AddComponent(leaf2);

    Composite *lay2 = new Composite("AA");
    Leaf *leaf4 = new Leaf("BB");
    lay2->AddComponent(leaf4);

    Composite *lay1 = new Composite("CC");
    Leaf *leaf3 = new Leaf("C");
    lay1->AddComponent(leaf3);
    lay1->AddComponent(lay2);

    root->AddComponent(lay1);

    root->GetChild(1);
    cout<<endl;
    lay1->GetChild(1);
    cout<<endl;
    lay2->GetChild(1);

    delete root;
    delete lay1;
    delete lay2;
    delete leaf1;
    delete leaf2;
    delete leaf3;
    delete leaf4;
}

 

posted @ 2021-11-05 20:32  大雄的脑袋  阅读(39)  评论(0编辑  收藏  举报