zoukankan      html  css  js  c++  java
  • 桥接模式——C++实现

    问题描述:

    用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。

    类图:

    C++源代码:

    #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->add(obj4);
        fold3->display();
    
        delete obj1, obj2, obj3, obj4;
        delete fold1, fold2, fold3;
    }
    

      

    运行结果:

     

     

     

  • 相关阅读:
    PYTHON 集合set 方法
    PYTHON 购物车程序
    转 mybatis javaType与jdbcType对应
    转 java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
    转 Could not create the view: An unexpected exception was thrown.问题解决
    [转] 数据库链接池配置
    HttpServletRequest.getServletContext()一直提示找不到,而引出的问题
    如何解决找不到方法HttpServletRequest.getServletContext() ---- NoSuchMethodError
    web项目编译出错时,原因之一,可能是build path 中order and Export引起
    mysql修改密码
  • 原文地址:https://www.cnblogs.com/znjy/p/14152868.html
Copyright © 2011-2022 走看看