设计模式 建造者模式

建造者模式

介绍

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

核心思想

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

代码示例

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

相关推荐
蔡蓝8 小时前
设计模式-建造者模式
服务器·设计模式·建造者模式
galaxy_strive8 小时前
绘制饼图详细过程
开发语言·c++·qt
magic 24511 小时前
Java建造者模式(Builder Pattern)详解与实践
java·开发语言·建造者模式
不伤欣13 小时前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
漫谈网络13 小时前
MVC与MVP设计模式对比详解
设计模式·mvc
蔡蓝14 小时前
设计模式-观察着模式
java·开发语言·设计模式
哆啦A梦的口袋呀15 小时前
基于Python学习《Head First设计模式》第六章 命令模式
python·学习·设计模式
半路下车17 小时前
【Harmony OS 5】HarmonyOS应用测试指南
设计模式·harmonyos
周某某~17 小时前
一.设计模式的基本概念
设计模式
on the way 12317 小时前
行为型设计模式之Interpreter(解释器)
设计模式