软件设计实验9

实验9:桥接模式

[实验任务一]:两个维度的桥接模式

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

直接放源码:

#include<iostream>
using namespace std;
class transport
{
public :
    transport(){}
    virtual ~transport(){}
    virtual void bedrive()=0;
};

class bus :public transport
{
public:
    bus(){}
    virtual~bus(){}
    void bedrive(){cout<<"bus"<<endl;}
};

class car :public transport
{
public:
    car(){}
    virtual~car(){}
    void bedrive(){cout<<"car"<<endl;}
};




class road
{
public:
    road():_instance(NULL){}
    virtual~road(){}
    void settransport(transport *instance)
    {
        _instance=instance;
    }
    virtual void drive()=0;
protected:
    transport *_instance;

};

class shuini :public road
{

public:
    shuini(){}
    virtual~shuini(){}
    void drive(){cout<<"行驶在水泥路";_instance->bedrive();}
};

class liqing :public road
{
    liqing(){}
    virtual~liqing(){}
    void drive(){cout<<"行驶在沥青路";_instance->bedrive();}
};

int main()
{
    road *ro=new shuini;
    transport *tr=new bus;
    ro->settransport(tr);
    ro->drive();
    delete ro;
    delete tr;


    return 0;
}

  

posted @ 2022-10-14 15:10  Lindseyyip  阅读(26)  评论(0编辑  收藏  举报