35mm接口及25mm接口;USB Type-C接口;lighting接口 分别用类模式和对象模式实现耳机接口的转换 给出以上c++完整代码和类图
类图:
+---------------------+ +---------------------+
| Headphone | | AdapterInterface |
+---------------------+ +---------------------+
| + playMusic() | | + convert() |
| + pauseMusic() | +---------------------+
| + nextSong() |
| + previousSong() |
+---------------------+
^
|
+---------------------+
| 3.5mmAdapter |
+---------------------+
| + convert() |
+---------------------+
^
|
+---------------------+
| 2.5mmAdapter |
+---------------------+
| + convert() |
+---------------------+
^
|
+---------------------+
| USBTypeCAdapter |
+---------------------+
| + convert() |
+---------------------+
^
|
+---------------------+
| LightingAdapter |
+---------------------+
| + convert() |
+---------------------+
类实现:
// Headphone.h
#pragma once
class Headphone {
public:
virtual void playMusic() = 0;
virtual void pauseMusic() = 0;
virtual void nextSong() = 0;
virtual void previousSong() = 0;
};
// AdapterInterface.h
#pragma once
#include "Headphone.h"
class AdapterInterface : public Headphone {
public:
virtual void convert() = 0;
};
// 3.5mmAdapter.h
#pragma once
#include "AdapterInterface.h"
class ThreePointFiveMmAdapter : public AdapterInterface {
public:
ThreePointFiveMmAdapter(Headphone* headphone);
virtual void playMusic();
virtual void pauseMusic();
virtual void nextSong();
virtual void previousSong();
virtual void convert();
private:
Headphone* m_headphone;
};
// 3.5mmAdapter.cpp
#include "ThreePointFiveMmAdapter.h"
ThreePointFiveMmAdapter::ThreePointFiveMmAdapter(Headphone* headphone) {
m_headphone = headphone;
}
void ThreePointFiveMmAdapter::playMusic() {
m_headphone->playMusic();
}
void ThreePointFiveMmAdapter::pauseMusic() {
m_headphone->pauseMusic();
}
void ThreePointFiveMmAdapter::nextSong() {
m_headphone->nextSong();
}
void ThreePointFiveMmAdapter::previousSong() {
m_headphone->previousSong();
}
void ThreePointFiveMmAdapter::convert() {
std::cout << "Converting 3.5mm audio to headphone interface." << std::endl;
}
// 2.5mmAdapter.h
#pragma once
#include "AdapterInterface.h"
class TwoPointFiveMmAdapter : public AdapterInterface {
public:
TwoPointFiveMmAdapter(Headphone* headphone);
virtual void playMusic();
virtual void pauseMusic();
virtual void nextSong();
virtual void previousSong();
virtual void convert();
private:
Headphone* m_headphone;
};
// 2.5mmAdapter.cpp
#include "TwoPointFiveMmAdapter.h"
TwoPointFiveMmAdapter::TwoPointFiveMmAdapter(Headphone* headphone) {
m_headphone = headphone;
}
void TwoPointFiveMmAdapter::playMusic() {
m_headphone->playMusic();
}
void TwoPointFiveMmAdapter::pauseMusic() {
m_headphone->pauseMusic();
}
void TwoPointFiveMmAdapter::nextSong() {
m_headphone->nextSong();
}
void TwoPointFiveMmAdapter::previousSong() {
m_headphone->previousSong();
}
void TwoPointFiveMmAdapter::convert() {
std::cout << "Converting 2.5mm audio to headphone interface." << std::endl;
}
// USBTypeCAdapter.h
#pragma once
#include "AdapterInterface.h"
class USBTypeCAdapter : public AdapterInterface {
public:
USBTypeCAdapter(Headphone* headphone);
virtual void playMusic();
virtual void pauseMusic();
virtual void nextSong();
virtual void previousSong();
virtual void convert();
private:
Headphone* m_headphone;
};
// USBTypeCAdapter.cpp
#include "USBTypeCAdapter.h"
USBTypeCAdapter::USBTypeCAdapter(Headphone* headphone) {
m_headphone = headphone;
}
void USBTypeCAdapter::playMusic() {
m_headphone->playMusic();
}
void USBTypeCAdapter::pauseMusic() {
m_headphone->pauseMusic();
}
void USBTypeCAdapter::nextSong() {
m_headphone->nextSong();
}
void USBTypeCAdapter::previousSong() {
m_headphone->previousSong();
}
void USBTypeCAdapter::convert() {
std::cout << "Converting USB Type-C audio to headphone interface." << std::endl;
}
// LightingAdapter.h
#pragma once
#include "AdapterInterface.h"
class LightingAdapter : public AdapterInterface {
public:
LightingAdapter(Headphone* headphone);
virtual void playMusic();
virtual void pauseMusic();
virtual void nextSong();
virtual void previousSong();
virtual void convert();
private:
Headphone* m_headphone;
};
// LightingAdapter.cpp
#include "LightingAdapter.h"
LightingAdapter::LightingAdapter(Headphone* headphone) {
m_headphone = headphone;
}
void LightingAdapter::playMusic() {
m_headphone->playMusic();
}
void LightingAdapter::pauseMusic() {
m_headphone->pauseMusic();
}
void LightingAdapter::nextSong() {
m_headphone->nextSong();
}
void LightingAdapter::previousSong() {
m_headphone->previousSong();
}
void LightingAdapter::convert() {
std::cout << "Converting Lighting audio to headphone interface." << std::endl;
}
使用示例:
#include <iostream>
#include "Headphone.h"
#include "ThreePointFiveMmAdapter.h"
#include "TwoPointFiveMmAdapter.h"
#include "USBTypeCAdapter.h"
#include "LightingAdapter.h"
int main() {
// 使用3.5mm耳机
Headphone* headphone1 = new MyHeadphone();
AdapterInterface* adapter1 = new ThreePointFiveMmAdapter(headphone1);
adapter1->convert();
adapter1->playMusic();
adapter1->pauseMusic();
adapter1->nextSong();
adapter1->previousSong();
// 使用2.5mm耳机
Headphone* headphone2 = new MyHeadphone();
AdapterInterface* adapter2 = new TwoPointFiveMmAdapter(headphone2);
adapter2->convert();
adapter2->playMusic();
adapter2->pauseMusic();
adapter2->nextSong();
adapter2->previousSong();
// 使用USB Type-C耳机
Headphone* headphone3 = new MyHeadphone();
AdapterInterface* adapter3 = new USBTypeCAdapter(headphone3);
adapter3->convert();
adapter3->playMusic();
adapter3->pauseMusic();
adapter3->nextSong();
adapter3->previousSong();
// 使用Lighting耳机
Headphone* headphone4 = new MyHeadphone();
AdapterInterface* adapter4 = new LightingAdapter(headphone4);
adapter4->convert();
adapter4->playMusic();
adapter4->pauseMusic();
adapter4->nextSong();
adapter4->previousSong();
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/hjlT 著作权归作者所有。请勿转载和采集!