设计模式 建造者模式

建造者模式

介绍

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

核心思想

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

代码示例

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原则 维护困难

相关推荐
yxc_inspire9 分钟前
基于Qt的app开发第七天
开发语言·c++·qt·app
workflower1 小时前
使用谱聚类将相似度矩阵分为2类
人工智能·深度学习·算法·机器学习·设计模式·软件工程·软件需求
dot to one2 小时前
Qt 中 QWidget涉及的常用核心属性介绍
开发语言·c++·qt
码农新猿类3 小时前
初入OpenCV
qt·opencv·计算机视觉
枣伊吕波3 小时前
第六节第二部分:抽象类的应用-模板方法设计模式
android·java·设计模式
lalajh4 小时前
论软件设计模式及其应用
设计模式
洛克希德马丁4 小时前
QLineEdit增加点击回显功能
c++·qt·ui
向日葵xyz6 小时前
Qt5与现代OpenGL学习(十一)OpenGL Widget鼠标控制直线旋转
开发语言·qt·学习
lgily-12256 小时前
常用的设计模式详解
java·后端·python·设计模式
小宋加油啊7 小时前
Mac QT水平布局和垂直布局
开发语言·qt·macos