本例使用中结者模式实现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 COMPONENTSsystemfilesystemserializationprogram_optionsthread)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、3FLAGS_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);}
}

程序输出如下

C++11 使用中结者模式实现ChatRoom逻辑相关推荐

  1. Java设计模式学习总结(11)——结构型模式之装饰器模式

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装.这种模式创建了一个装饰类,用来包装原 ...

  2. Java23种设计模式——11.结构型模式之享元模式

    Java中除去有设计原则之外,还有23中设计模式. 这些模式都是前辈们一点一点积累下来,一直在改进,一直在优化的,而这些设计模式可以解决一些特定的问题. 并且在这些模式中,可以说是将语言的使用体现的淋 ...

  3. 设计模式之七个结构型模式的相关知识,简单易懂。

    七. 适配器模式-Adapter Pattern 1) 不兼容结构的协调--适配器模式(一) 我的笔记本电脑的工作电压是20V,而我国的家庭用电是220V,如何让20V的笔记本电脑能够在220V的电压 ...

  4. JAVA设计模式--结构型模式

    2019独角兽企业重金招聘Python工程师标准>>> 我们接着讨论设计模式,上篇文章我讲完了5种创建型模式,这章开始,我将讲下7种结构型模式:适配器模式.装饰模式.代理模式.外观模 ...

  5. .NET设计模式(15):结构型模式专题总结

    .NET设计模式(15):结构型模式专题总结 --探索设计模式系列之十五 Terrylee,2006年5月 摘要:结构型模式,顾名思义讨论的是类和对象的结构,它采用继承机制来组合接口或实现(类结构型模 ...

  6. 设计模式 结构型模式 外观模式(Facade Pattern)

    在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化. 这时为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 &quo ...

  7. 设计模式:结构型模式总结

    作者:TerryLee  创建于:2006-06-01 出处:http://terrylee.cnblogs.com/archive/2006/06/01/designpattern_articles ...

  8. 5 结构型模式之 - 适配器模式

    5 结构型模式之 - 适配器模式 适配器模式的介绍:适配器模式在开发中使用率很高,适配器是将两个不兼容的类融合在一起,它有点像粘合剂,将不同的东西通过一种转换使得它们能够协作起来.例如经常碰到两个不相 ...

  9. 结构型模式--装饰模式

    下面先用java,然后用Objective-C行对装饰模式的讲解: 对于java的装饰模式讲解和使用比较详细和难度有点偏高,而对于Objective-C的装饰模式讲解和使用方面比较简单,而且和java ...

最新文章

  1. Win7 64 bit 激活工具
  2. 服务机器人平台和后台
  3. CKEditor/FCKEditor的使用
  4. 同时获取同一等级下多个class值的节点的方法
  5. 8X53 VS 6763
  6. 217 - leetcode -存在重复元素 -数据结构类 先排序再操作
  7. Codeforces 148D:Bag of mice 概率DP
  8. 安全随笔1:谨慎一次MD5值的可被穷举性
  9. 在cmd里面运行adb命令的时候提示:adb server is out of date. killing...
  10. UVALive6929 Sums【数学】
  11. InDesign教程,如何更改字体和字体大小?
  12. 饭卡可以用水冲洗吗_关于饭卡使用与管理的规定
  13. 谷歌服务安装包_安卓手机安装谷歌服务框架和Google Play傻瓜式教程 100%好用
  14. 论文查重率【降重】从65%-25%的心路历程!超硬核!霸道降重!
  15. PyTorch模型 .pt、.pth与.pkl 的区别
  16. Elasticsearch面试专题总结
  17. 移动宽带运营商服务器未响应,中国移动宽带网络有问题怎么办
  18. 制作1~2020门牌号共需要几个2字符?
  19. 使用Visio来画图配置
  20. c/c++实现window简易串口通信

热门文章

  1. Android9.0 如何精准区分SDK接口和非 SDK接口
  2. 使用普中科技51单片机进行(I^2)C总线操作
  3. 中国移动云能力中心(苏小研)--秋招面经
  4. 图片批量下载并打包成zip
  5. vue 生命周期 详解
  6. 数据库的三级模式和两级映射--简单介绍
  7. 海滩上有一堆桃子,五只猴子来分(C语言)
  8. Email发送 带附件和抄送人
  9. PDF怎么去水印,去除PDF水印的方法
  10. Python爬虫——下载PPT模板