搜索

桥接模式javac++


发布时间: 2022-11-24 21:39:00    浏览次数:30 次

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

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

 

 

 

 

 

package test9;

public class Bus implements Transport{
    @Override
    public void run() {
        System.out.print("Bus");
    }
}
package test9;

public class Car implements Transport{
    @Override
    public void run() {
        System.out.print("Car");
    }
}
package test9;

public class LIqing extends Road{
    public void drive() {
        transport.run();
        System.out.println("行驶在沥青路");
    }
}
package test9;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Transport transport = null;
        Road road = null;
        System.out.println("请选择:1、Car/水泥路  2、Car/沥青路  3、Bus/水泥路  4、Bus/沥青路");
        Scanner input=new Scanner(System.in);
        int i=input.nextInt();
        if(i==1) {
            transport=(Transport)new Car();
            road=(Road)new Shuini();
        }else if(i==2) {
            transport=(Transport)new Car();
            road=(Road)new LIqing();
        }else if(i==3){
            transport=(Transport)new Bus();
            road=(Road)new Shuini();
        }else if(i==4) {
            transport=(Transport)new Bus();
            road=(Road)new LIqing();
        }else {
            System.out.println("输入无效!");
        }
        road.setTransport(transport);
        road.drive();
    }
}
package test9;

public abstract class Road {
    protected Transport transport;
    public void setTransport(Transport tr) {
        this.transport=tr;
    }
    public abstract void drive();
}
package test9;

public class Shuini extends Road{
    public void drive() {
        transport.run();
        System.out.println("行驶在水泥路");
    }
}
package test9;

public interface Transport {
    public void run();
}

 

#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
{
public:
    liqing(){}
    virtual~liqing(){}
    void drive(){cout<<"在沥青路上行驶的";_instance->bedrive();}
};

int main()
{
    road *ro=new liqing;
    transport *tr=new bus;

    ro->settransport(tr);
    ro->drive();
    delete ro;
    delete tr;

    ro=new shuini;
    tr=new car;
     ro->settransport(tr);
    ro->drive();
    delete ro;
    delete tr;

    system("pause");
    return 0;
}

 

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