diff --git a/06.Singleton/2.Code/CMakeLists.txt b/06.Singleton/2.Code/CMakeLists.txt new file mode 100644 index 0000000..ac1b62e --- /dev/null +++ b/06.Singleton/2.Code/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.15) +project(BuilderPattern) +set(SRC_LIST main.cpp) + +get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(FOLDER_NAME ${FOLDER_NAME} NAME) +set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR}/${FOLDER_NAME}) +add_executable(app6 ${SRC_LIST}) \ No newline at end of file diff --git a/06.Singleton/2.Code/Singleton.h b/06.Singleton/2.Code/Singleton.h new file mode 100644 index 0000000..4589428 --- /dev/null +++ b/06.Singleton/2.Code/Singleton.h @@ -0,0 +1,50 @@ +#ifndef __SINGLETON_H__ +#define __SINGLETON_H__ + +#include +#include +#include +using namespace std; + +class Singleton_Lazy +{ +public: + static Singleton_Lazy* getInstance(){ + printf("This is Singleton Lazy mode...\n"); + if (instance == NULL){ + m_mutex.lock(); + if (instance == NULL){ + printf("创建新的实例\n"); + instance = new Singleton_Lazy(); + } + m_mutex.unlock(); + } + return instance; + } +private: + Singleton_Lazy(){} + + static Singleton_Lazy* instance; + static std::mutex m_mutex; +}; + +Singleton_Lazy* Singleton_Lazy::instance = NULL; +std::mutex Singleton_Lazy::m_mutex; + + +class Singleton_Hungry +{ +public: + static Singleton_Hungry* getInstance() + { + printf("This Singleton Hungry mode...\n"); + return instance; + } +private: + Singleton_Hungry() {} + static Singleton_Hungry* instance; +}; + +Singleton_Hungry* Singleton_Hungry::instance = new Singleton_Hungry; + +#endif //__SINGLETON_H__ \ No newline at end of file diff --git a/06.Singleton/2.Code/main.cpp b/06.Singleton/2.Code/main.cpp new file mode 100644 index 0000000..d20ee4a --- /dev/null +++ b/06.Singleton/2.Code/main.cpp @@ -0,0 +1,111 @@ +#include +#include "Singleton.h" + +/*单例模式简单实现*/ +/* +int main() +{ + Singleton *s1 = Singleton::getInstance(); + Singleton *s2 = Singleton::getInstance(); + + system("pause"); + return 0; +} +*/ + +#ifdef win32 +/*非线程安全 单例模式*/ +#include +#include + +//多线程,线程数目:5 +#define THREAD_NUM 5 + +unsigned int __stdcall CallSingleton(void *pPM) +{ + Singleton *s = Singleton::getInstance(); + int nThreadNum = *(int *)pPM; + Sleep(50); + //printf("线程编号为%d\n", nThreadNum); + return 0; +} + + +int main() +{ + HANDLE handle[THREAD_NUM]; + + //线程编号 + int threadNum = 0; + while (threadNum < THREAD_NUM) + { + handle[threadNum] = (HANDLE)_beginthreadex(NULL, 0, CallSingleton, &threadNum, 0, NULL); + //等子线程接收到参数时主线程可能改变了这个i的值 + threadNum++; + } + //保证子线程已全部运行结束 + WaitForMultipleObjects(THREAD_NUM, handle, TRUE, INFINITE); + + system("pause"); + return 0; +} +/* for linux platform */ +#else +#define THREAD_NUM 6 +#include +void* callSingleton_Lazy(void*) +{ + Singleton_Lazy *s = Singleton_Lazy::getInstance(); + pthread_t nThreadNum = pthread_self(); + // sleep(50); + printf("线程编号为%lu\n", nThreadNum); + return 0; +} + +void* callSingleton_Hungry(void*) +{ + Singleton_Hungry *s = Singleton_Hungry::getInstance(); + pthread_t nThreadNum = pthread_self(); + // sleep(50); + printf("线程编号为%ld\n", nThreadNum); + return 0; +} + +int main() +{ + pthread_t threads_pool[THREAD_NUM]; + int tids[THREAD_NUM], i; + void* status; + pthread_attr_t attr; + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + for(i = 0; i < THREAD_NUM; i++) + { + if(i < THREAD_NUM / 2) + tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Lazy, (void*)&i); + else + tids[i] = pthread_create(&threads_pool[i], NULL, callSingleton_Hungry, (void*)&i); + if(tids[i]) + { + printf("Error: unable to create thread.\n"); + exit(-1); + } + } + + pthread_attr_destroy(&attr); + for(i = 0; i < THREAD_NUM; i++) + { + tids[i] = pthread_join(threads_pool[i], &status); + if(tids[i]) + { + printf("Error: unable to join.\n"); + exit(-1); + } + } + printf("main exiting.\n"); + return 0; +} + +#endif \ No newline at end of file diff --git a/07.AdapterPattern/2.Code/CMakeLists.txt b/07.AdapterPattern/2.Code/CMakeLists.txt new file mode 100644 index 0000000..4f9c6e0 --- /dev/null +++ b/07.AdapterPattern/2.Code/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.15) +project(BuilderPattern) +set(SRC_LIST main.cpp) + +get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(FOLDER_NAME ${FOLDER_NAME} NAME) +set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR}/${FOLDER_NAME}) +add_executable(app7 ${SRC_LIST}) \ No newline at end of file diff --git a/08.BridgePattern/2.Code/CMakeLists.txt b/08.BridgePattern/2.Code/CMakeLists.txt new file mode 100644 index 0000000..de66fee --- /dev/null +++ b/08.BridgePattern/2.Code/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.10) +project(SimpleFactory) +set(SRC_LIST main.cpp) +get_filename_component(FOLDER_NAME ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) +get_filename_component(FOLDER_NAME ${FOLDER_NAME} NAME) +set(EXECUTABLE_OUTPUT_PATH ${OUTPUT_DIR}/${FOLDER_NAME}/) +add_executable(app8 ${SRC_LIST}) \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index cfb2644..5ba50ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,4 +9,7 @@ add_subdirectory(02.FactoryMethod/2.Code/) add_subdirectory(03.AbstractFactory/2.Code/) add_subdirectory(04.BuilderPattern/2.Code/) add_subdirectory(05.PrototypePattern/2.Code/) +add_subdirectory(06.Singleton/2.Code/) +add_subdirectory(07.AdapterPattern/2.Code/) +add_subdirectory(08.BridgePattern/2.Code/) add_subdirectory(17.IteratorPattern/2.Code/)