注释:

// Headphone接口类,定义了一个接口函数 class HeadphoneInterface { public: virtual void playMusic() = 0; };

// 3.5mm接口类,实现了Headphone接口 class Headphone35mm : public HeadphoneInterface { public: void playMusic() { cout << "使用3.5mm接口播放音乐" << endl; } };

// 2.5mm接口类,实现了Headphone接口 class Headphone25mm : public HeadphoneInterface { public: void playMusic() { cout << "使用2.5mm接口播放音乐" << endl; } };

// USB Type-C接口类,实现了Headphone接口 class HeadphoneUsb : public HeadphoneInterface { public: void playMusic() { cout << "使用USB Type-C接口播放音乐" << endl; } };

// lighting接口类,实现了Headphone接口 class HeadphoneLighting : public HeadphoneInterface { public: void playMusic() { cout << "使用lighting接口播放音乐" << endl; } };

// 转换器类,可以将不同接口的耳机转换为其他接口的耳机 class HeadphoneConverter { public: // 将3.5mm接口的耳机转换为2.5mm接口的耳机 Headphone25mm* convert35mmTo25mm() { return new Headphone25mm(); }

// 将2.5mm接口的耳机转换为3.5mm接口的耳机
Headphone35mm* convert25mmTo35mm() {
    return new Headphone35mm();
}

// 将3.5mm接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* convert35mmToUsb() {
    return new HeadphoneUsb();
}

// 将2.5mm接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* convert25mmToUsb() {
    Headphone25mm* headphone25mm = new Headphone25mm();
    HeadphoneConverter converter;
    Headphone35mm* headphone35mm = converter.convert25mmTo35mm();
    HeadphoneUsb* headphoneUsb = converter.convert35mmToUsb();
    delete headphone25mm;
    delete headphone35mm;
    return headphoneUsb;
}

// 将USB Type-C接口的耳机转换为3.5mm接口的耳机
Headphone35mm* convertUsbTo35mm() {
    HeadphoneUsb* headphoneUsb = new HeadphoneUsb();
    HeadphoneConverter converter;
    Headphone35mm* headphone35mm = converter.convert25mmTo35mm();
    delete headphoneUsb;
    return headphone35mm;
}

// 将USB Type-C接口的耳机转换为2.5mm接口的耳机
Headphone25mm* convertUsbTo25mm() {
    HeadphoneUsb* headphoneUsb = new HeadphoneUsb();
    HeadphoneConverter converter;
    Headphone35mm* headphone35mm = converter.convertUsbTo35mm();
    Headphone25mm* headphone25mm = converter.convert35mmTo25mm();
    delete headphoneUsb;
    delete headphone35mm;
    return headphone25mm;
}

// 将lighting接口的耳机转换为3.5mm接口的耳机
Headphone35mm* convertLightingTo35mm() {
    return new Headphone35mm();
}

// 将lighting接口的耳机转换为2.5mm接口的耳机
Headphone25mm* convertLightingTo25mm() {
    HeadphoneConverter converter;
    Headphone35mm* headphone35mm = converter.convertLightingTo35mm();
    Headphone25mm* headphone25mm = converter.convert35mmTo25mm();
    delete headphone35mm;
    return headphone25mm;
}

// 将lighting接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* convertLightingToUsb() {
    HeadphoneConverter converter;
    Headphone35mm* headphone35mm = converter.convertLightingTo35mm();
    HeadphoneUsb* headphoneUsb = converter.convert35mmToUsb();
    delete headphone35mm;
    return headphoneUsb;
}

};

// 测试代码 int main() { HeadphoneConverter converter;

// 将3.5mm接口的耳机转换为2.5mm接口的耳机
Headphone25mm* headphone25mm = converter.convert35mmTo25mm();
headphone25mm->playMusic();
delete headphone25mm;

// 将2.5mm接口的耳机转换为3.5mm接口的耳机
Headphone35mm* headphone35mm = converter.convert25mmTo35mm();
headphone35mm->playMusic();
delete headphone35mm;

// 将3.5mm接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* headphoneUsb1 = converter.convert35mmToUsb();
headphoneUsb1->playMusic();
delete headphoneUsb1;

// 将2.5mm接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* headphoneUsb2 = converter.convert25mmToUsb();
headphoneUsb2->playMusic();
delete headphoneUsb2;

// 将USB Type-C接口的耳机转换为3.5mm接口的耳机
Headphone35mm* headphone35mm2 = converter.convertUsbTo35mm();
headphone35mm2->playMusic();
delete headphone35mm2;

// 将USB Type-C接口的耳机转换为2.5mm接口的耳机
Headphone25mm* headphone25mm2 = converter.convertUsbTo25mm();
headphone25mm2->playMusic();
delete headphone25mm2;

// 将lighting接口的耳机转换为3.5mm接口的耳机
Headphone35mm* headphone35mm3 = converter.convertLightingTo35mm();
headphone35mm3->playMusic();
delete headphone35mm3;

// 将lighting接口的耳机转换为2.5mm接口的耳机
Headphone25mm* headphone25mm3 = converter.convertLightingTo25mm();
headphone25mm3->playMusic();
delete headphone25mm3;

// 将lighting接口的耳机转换为USB Type-C接口的耳机
HeadphoneUsb* headphoneUsb3 = converter.convertLightingToUsb();
headphoneUsb3->playMusic();
delete headphoneUsb3;

return 0;

}

类图:

![image.png](https://cdn.nlark.com/yuque/0/2021/png/97322/1631241854588-3f6d7b68-0a5f-4f57-af0d-3f6e2b7f9a5f.png


原文地址: https://www.cveoy.top/t/topic/hjmC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录