feat: add Observer Pattern
This commit is contained in:
9
Studio/DesignPatterns/DesignPatterns.cpp
Normal file
9
Studio/DesignPatterns/DesignPatterns.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
// DesignPatterns.cpp : This file contains the 'main' function. Program execution begins and ends there.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
//int main()
|
||||
//{
|
||||
// std::cout << "Hello World!\n";
|
||||
//}
|
153
Studio/DesignPatterns/DesignPatterns.vcxproj
Normal file
153
Studio/DesignPatterns/DesignPatterns.vcxproj
Normal file
@@ -0,0 +1,153 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{57f3c035-9c90-4e9f-934a-032804827444}</ProjectGuid>
|
||||
<RootNamespace>DesignPatterns</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DesignPatterns.cpp" />
|
||||
<ClCompile Include="Observer.cpp" />
|
||||
<ClCompile Include="Singleton.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Observer.h" />
|
||||
<ClInclude Include="Singleton.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
36
Studio/DesignPatterns/DesignPatterns.vcxproj.filters
Normal file
36
Studio/DesignPatterns/DesignPatterns.vcxproj.filters
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="DesignPatterns.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Singleton.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Observer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Singleton.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Observer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
1
Studio/DesignPatterns/Observer.cpp
Normal file
1
Studio/DesignPatterns/Observer.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "Observer.h"
|
106
Studio/DesignPatterns/Observer.h
Normal file
106
Studio/DesignPatterns/Observer.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************
|
||||
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||
* GitHub: https://github.com/XMuli
|
||||
* Description: 观察者模式 Observer Pattern
|
||||
* Reference: 情景 → Subject 发送一个消息,然后所有的 Observer A、B、C
|
||||
* 都有收到,并且对相同的消息,做出各自不同的操作响应
|
||||
* See: https://www.cnblogs.com/suzhou/p/dp16obsvr.html
|
||||
******************************************************************/
|
||||
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
using namespace std;
|
||||
|
||||
class Observer { // 观察者
|
||||
public:
|
||||
virtual void update(string msg) = 0;
|
||||
virtual void doSomeThing() = 0;
|
||||
|
||||
protected:
|
||||
string m_msg;
|
||||
};
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
Subject() = default;
|
||||
virtual ~Subject() = default;
|
||||
virtual void addObsvr(Observer* obj) = 0;
|
||||
virtual void removeObsvr(Observer* obj) = 0;
|
||||
virtual void notify(string msg) = 0;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------
|
||||
class ConcreteSubject: public Subject {
|
||||
public:
|
||||
ConcreteSubject() {};
|
||||
virtual ~ConcreteSubject() { m_obsvrs.clear(); }
|
||||
virtual void addObsvr(Observer* obj) override {
|
||||
m_obsvrs.push_back(obj);
|
||||
}
|
||||
virtual void removeObsvr(Observer* obj) override {
|
||||
m_obsvrs.remove(obj);
|
||||
}
|
||||
virtual void notify(string msg) override {
|
||||
for (const auto& it : m_obsvrs)
|
||||
it->update(msg);
|
||||
|
||||
for (const auto& it : m_obsvrs)
|
||||
it->doSomeThing();
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
list<Observer*> m_obsvrs;
|
||||
};
|
||||
|
||||
class AObserver : public Observer {
|
||||
public:
|
||||
void update(string msg) override { m_msg = msg; }
|
||||
virtual void doSomeThing() override { cout << "AObserver: 对 " << m_msg << " 进行 A 的操作" << endl; }
|
||||
};
|
||||
|
||||
class BObserver : public Observer {
|
||||
public:
|
||||
void update(string msg) override { m_msg = msg; }
|
||||
virtual void doSomeThing() override { cout << "BObserver: 对 " << m_msg << " 进行 B 的操作" << endl; }
|
||||
};
|
||||
|
||||
class CObserver : public Observer {
|
||||
public:
|
||||
void update(string msg) override { m_msg = msg; }
|
||||
virtual void doSomeThing() override { cout << "CObserver: 对 " << m_msg << " 进行 C 的操作" << endl; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
AObserver* a = new AObserver();
|
||||
BObserver* b = new BObserver();
|
||||
CObserver* c = new CObserver();
|
||||
ConcreteSubject* concreSubject = new ConcreteSubject();
|
||||
|
||||
concreSubject->addObsvr(a);
|
||||
concreSubject->addObsvr(b);
|
||||
concreSubject->addObsvr(c);
|
||||
concreSubject->notify("messagebox 1");
|
||||
|
||||
concreSubject->removeObsvr(c);
|
||||
concreSubject->notify("messagebox 2");
|
||||
|
||||
//a->doSomeThing();
|
||||
//b->doSomeThing();
|
||||
//c->doSomeThing();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*****************************打印结果*******************************
|
||||
AObserver: 对 messagebox 1 进行 A 的操作
|
||||
BObserver: 对 messagebox 1 进行 B 的操作
|
||||
CObserver: 对 messagebox 1 进行 C 的操作
|
||||
|
||||
AObserver: 对 messagebox 2 进行 A 的操作
|
||||
BObserver: 对 messagebox 2 进行 B 的操作
|
||||
******************************************************************/
|
1
Studio/DesignPatterns/Singleton.cpp
Normal file
1
Studio/DesignPatterns/Singleton.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "Singleton.h"
|
24
Studio/DesignPatterns/Singleton.h
Normal file
24
Studio/DesignPatterns/Singleton.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*******************************************************************
|
||||
* Copyright (c) 2022~2023 XMuli All rights reserved.
|
||||
* GitHub: https://github.com/XMuli
|
||||
* Description: 单例模式 Singleton Pattern 的最优雅实现
|
||||
* Reference: https://zhuanlan.zhihu.com/p/37469260 单例介绍及好一篇
|
||||
******************************************************************/
|
||||
#pragma once
|
||||
|
||||
// 《Effective C++》提出了一种更优雅的单例模式实现,称为 Meyers' Singleton
|
||||
class Singleton
|
||||
{
|
||||
public:
|
||||
static Singleton& instance() {
|
||||
static Singleton instan;
|
||||
return instan;
|
||||
}
|
||||
|
||||
private:
|
||||
Singleton() {};
|
||||
~Singleton() {};
|
||||
Singleton(const Singleton& obj) = delete;
|
||||
Singleton& operator=(const Singleton& obj) = delete;
|
||||
};
|
||||
|
Reference in New Issue
Block a user