C++11 使用中结者模式实现ChatRoom逻辑

本例使用中结者模式实现ChatRoom逻辑。就是一个ChatRoom持有多个Person类的实例,多个Person类公用一个ChatRoom的指针。使用ChatRoom指针实现聊天操作。
其中ChatRoom指针就是此处的中结者。
中结者模式结构示意图
test/CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
    set(CMAKE_MACOSX_RPATH 1)
    set(CMAKE_PREFIX_PATH /Users/aabjfzhu/software/vcpkg/ports/cppwork/vcpkg_installed/x64-osx/share )
elseif(UNIX)
    message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
    set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)

project(chat_room)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-narrowing")

add_definitions(-g)

find_package(ZLIB)

find_package(OpenCV REQUIRED )
find_package(Arrow CONFIG REQUIRED)

find_package(unofficial-brotli REQUIRED)
find_package(unofficial-utf8proc CONFIG REQUIRED)
find_package(Thrift CONFIG REQUIRED)

find_package(glog REQUIRED)

find_package(OpenSSL REQUIRED)

find_package(Boost REQUIRED COMPONENTS
    system
    filesystem
    serialization
    program_options
    thread
    )

find_package(DataFrame REQUIRED)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../../include)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../include/   ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/)
endif(APPLE)


if(APPLE)
    MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
    set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
    set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)

include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})

file( GLOB test_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 

file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/../include/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/arr_/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/death_handler/impl/*.cpp)

add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES} ${test_file})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib OpenSSL::SSL OpenSSL::Crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace libzstd.a libbz2.a libsnappy.a re2::re2 parquet lz4 unofficial::brotli::brotlidec-static unofficial::brotli::brotlienc-static unofficial::brotli::brotlicommon-static utf8proc thrift::thrift  arrow arrow_dataset)

foreach( test_file ${test_file_list} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${test_file})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file}  ${test_file})
    target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( test_file ${test_file_list})

test/chat_room_test.cpp

#include "person.h"
#include "chatroom.h"

#include <glog/logging.h>
#include <gtest/gtest.h>

#include <fstream>
#include <memory>
#include <algorithm>

#include "death_handler/death_handler.h"

int main(int argc, char** argv) {
    FLAGS_log_dir = "./";
    FLAGS_alsologtostderr = true;
    // 日志级别 INFO, WARNING, ERROR, FATAL 的值分别为0、1、2、3
    FLAGS_minloglevel = 0;

    Debug::DeathHandler dh;

    google::InitGoogleLogging("./logs.log");
    testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

// ChatRoom中结者模式简单演示
GTEST_TEST(ChatRoomTests, ChatRoom) {
    ChatRoom room;

    Person john {"john"};
    Person jane {"jane"};
    room.join(&john);
    room.join(&jane);

    john.say("hi room");
    jane.say("oh, hey john");

    Person simon {"simon"};
    room.join(&simon);
    simon.say("hi everyone");

    jane.pm("simon", "glad you could join us, simon");
}

include/person.h

#ifndef _FREDRIC_PERSON_H_
#define _FREDRIC_PERSON_H_

#include <string>
#include <iostream>
#include <vector>

struct ChatRoom;

struct Person {
    std::string name;
    ChatRoom* room {nullptr};

    Person(std::string const& name_);
    void receive(std::string const& origin, std::string const& message);
    void say(std::string const& message) const;
    // Private message from this person to "who"
    void pm(std::string const& who, std::string const& message) const;

    std::vector<std::string> chat_log;

   
    friend bool operator==(Person const& lhs, Person const& rhs) {
        return lhs.name == rhs.name;
    }

    friend bool operator!=(Person const& lhs, Person const& rhs) {
        return !(lhs == rhs);
    }
};

#endif

include/person.cpp

#include "person.h"
#include "chatroom.h"

Person::Person(std::string const& name_): name{name_} {

}

void Person::receive(std::string const& origin, std::string const& message) {
    std::string s {origin + ": \"" + message +"\""};
    std::cout << "[" << name << "'s chat session]" << s << "\n";
    chat_log.emplace_back(s);
}

void Person::say(std::string const& message) const {
    room->broadcast(name, message);
}

void Person::pm(std::string const& who, std::string const& message) const {
    room->message(name, who, message);
}

include/chat_room.h

#ifndef _FREDRIC_CHAT_ROOM_H_
#define _FREDRIC_CHAT_ROOM_H_

#include <vector>

struct ChatRoom {
    std::vector<Person*> people;
    void join(Person* p);
    void broadcast(std::string const& origin, std::string const& message);
    void message(std::string const& origin, std::string const& who, std::string const& message);
};
#endif

include/chat_room.cpp

#include "person.h"
#include "chatroom.h"
#include <algorithm>

void ChatRoom::broadcast(std::string const& origin, std::string const& message) {
    // 给除去自己以外的人广播这条消息
    for(auto&& p: people) {
        if(p->name != origin) {
            p->receive(origin, message);
        }
    }
}

void ChatRoom::join(Person* p) {
    std::string join_msg = p->name + " joins the chat";
    broadcast("room", join_msg);

    p->room = this;
    people.push_back(p);
}

// 从origin 给who发message
void ChatRoom::message(std::string const& origin, std::string const& who, std::string const& message) {
    auto target = std::find_if(people.begin(), people.end(), [&](Person const* p){
        return p->name == who;
    });

    if(target != people.end()) {
        (*target)->receive(origin, message);
    }
}

程序输出如下
中结者模式输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值