c++ - 使用哪种设计模式来获得想要的功能
问题描述
我想创建一个其他类继承的纯虚拟类,每个类实现相同的功能但具有不同的实现:假设我实现类 Server
class Server
{
Server();
~Server()=0;
virtual void send(const std::string& string);
virtual std::string receive();
};
class TcpServer:public Server
{
TcpServer();
~TcpServer();
void send(const std::string& string)
{...}
std::string receive(){...}
};
class UdpServer : public Server
{
UdpServer ();
~UdpServer ();
void send(const std::string& string)
{...}
std::string receive(){...}
};
main()
{
Server* server = new TcpServer()/UdpServer;
}
这种设计模式叫什么?因为我不明白它是 Adapter 还是 Composite 或 Facade,以及您是否对如何实现此行为有不同的想法。
解决方案
应该为此用例实施策略模式。检查下面的链接以获取更多信息。
推荐阅读
- vue.js - 我可以在 Vuejs 的同一个下拉列表中使用 2 个 v-fors,因为我需要在它们之间放置标签吗?
- c# - 在没有 WPF 的情况下使用 DispatcherTimer
- windows - 写入网络映射驱动器 - 空文件或失败
- r - 如何使用 dplyr::c_across() 总结不同类型的变量
- data-structures - 最适合存储二分键的数据库结构?
- java - 如何在 netbeans 12 中添加 java 库
- java - Spring Batch 将数字 2,981.00 扭曲为 2-981.00
- javascript - 如何更新剃刀页面中的画布
- swift - iOS 14 / XCode 12 - 以前的资产不再工作?
- python - 在目录的每个文件夹中创建一个文件