设计模式 建造者模式

建造者模式

介绍

建造者模式是一种创建型的设计模式 它用于分步骤构建复杂对象

核心思想

它的核心思想就是将复杂对象部件的创建和组装过程抽象出来

代码示例

cpp 复制代码
class SpeechServer {
    public:
        using ptr = std::shared_ptr<SpeechServer>;
        SpeechServer(const ASRClient::ptr asr_client, 
            const Registry::ptr &reg_client,
            const std::shared_ptr<brpc::Server> &server):
            _asr_client(asr_client),
            _reg_client(reg_client),
            _rpc_server(server){}
        ~SpeechServer(){}
        //搭建RPC服务器,并启动服务器
        void start() {
            _rpc_server->RunUntilAskedToQuit();
        }
    private:
        ASRClient::ptr _asr_client;
        Registry::ptr _reg_client;
        std::shared_ptr<brpc::Server> _rpc_server;
};

class SpeechServerBuilder {
    public:
        //构造语音识别客户端对象
        void make_asr_object(const std::string &app_id,
            const std::string &api_key,
            const std::string &secret_key) {
            _asr_client = std::make_shared<ASRClient>(app_id, api_key, secret_key);
        }
        //用于构造服务注册客户端对象
        void make_reg_object(const std::string &reg_host,
            const std::string &service_name,
            const std::string &access_host) {
            _reg_client = std::make_shared<Registry>(reg_host);
            _reg_client->registry(service_name, access_host);
        }
        //构造RPC服务器对象
        void make_rpc_server(uint16_t port, int32_t timeout, uint8_t num_threads) {
            if (!_asr_client) {
                LOG_ERROR("还未初始化语音识别模块!");
                abort();
            }
            _rpc_server = std::make_shared<brpc::Server>();
            SpeechServiceImpl *speech_service = new SpeechServiceImpl(_asr_client);
            int ret = _rpc_server->AddService(speech_service, 
                brpc::ServiceOwnership::SERVER_OWNS_SERVICE);
            if (ret == -1) {
                LOG_ERROR("添加Rpc服务失败!");
                abort();
            }
            brpc::ServerOptions options;
            options.idle_timeout_sec = timeout;
            options.num_threads = num_threads;
            ret = _rpc_server->Start(port, &options);
            if (ret == -1) {
                LOG_ERROR("服务启动失败!");
                abort();
            }
        }
        SpeechServer::ptr build() {
            if (!_asr_client) {
                LOG_ERROR("还未初始化语音识别模块!");
                abort();
            }
            if (!_reg_client) {
                LOG_ERROR("还未初始化服务注册模块!");
                abort();
            }
            if (!_rpc_server) {
                LOG_ERROR("还未初始化RPC服务器模块!");
                abort();
            }
            SpeechServer::ptr server = std::make_shared<SpeechServer>(
                _asr_client, _reg_client, _rpc_server);
            return server;
        }
    private:
        ASRClient::ptr _asr_client;
        Registry::ptr _reg_client;
        std::shared_ptr<brpc::Server> _rpc_server;
};

就像上面这段代码 如果不用建造者模式的话

我们每创建一个新的server对象就需要自己手动维护一遍

并且每次要添加新的依赖的时候需要重写这些配置的顺序 比较麻烦 而且不符合DRY原则 维护困难

相关推荐
花好月圆春祺夏安12 分钟前
基于odoo17的设计模式详解---策略模式
设计模式·策略模式
钢铁男儿4 小时前
PyQt5高级界而控件(容器:装载更多的控件QDockWidget)
数据库·python·qt
收破烂的小熊猫~12 小时前
《Java修仙传:从凡胎到码帝》第四章:设计模式破万法
java·开发语言·设计模式
佛祖让我来巡山15 小时前
【工厂和策略设计模式妙用】解决接口选择与多重if-else 问题
设计模式·策略模式·工厂模式
追烽少年x15 小时前
Qt中的QProcess类
qt
轩情吖16 小时前
Qt的第一个程序(2)
服务器·数据库·qt·qt creator·qlineedit·hello world·编辑框
心愿许得无限大18 小时前
Qt 常用界面组件
开发语言·c++·qt
机器视觉知识推荐、就业指导18 小时前
Qt 与Halcon联合开发八: 结合Qt与Halcon实现海康相机采图显示(附源码)
开发语言·数码相机·qt
hqxstudying19 小时前
Java创建型模式---原型模式
java·开发语言·设计模式·代码规范
charlie11451419119 小时前
如何使用Qt创建一个浮在MainWindow上的滑动小Panel
开发语言·c++·qt·界面设计