Forráskód Böngészése

加入中介者模式

Micooz 9 éve
szülő
commit
a81b406ac2

+ 6 - 0
DesignPattern.sln

@@ -37,6 +37,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ObserverPattern", "Observer
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MementoPattern", "MementoPattern\MementoPattern.vcxproj", "{9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}"
 EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MediatorPattern", "MediatorPattern\MediatorPattern.vcxproj", "{57CFD205-0966-4F90-86D6-A9647DCA693E}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Win32 = Debug|Win32
@@ -111,6 +113,10 @@ Global
 		{9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Debug|Win32.Build.0 = Debug|Win32
 		{9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Release|Win32.ActiveCfg = Release|Win32
 		{9727E0D3-66C5-4812-B13C-DC4C1F06ACAD}.Release|Win32.Build.0 = Release|Win32
+		{57CFD205-0966-4F90-86D6-A9647DCA693E}.Debug|Win32.ActiveCfg = Debug|Win32
+		{57CFD205-0966-4F90-86D6-A9647DCA693E}.Debug|Win32.Build.0 = Debug|Win32
+		{57CFD205-0966-4F90-86D6-A9647DCA693E}.Release|Win32.ActiveCfg = Release|Win32
+		{57CFD205-0966-4F90-86D6-A9647DCA693E}.Release|Win32.Build.0 = Release|Win32
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

BIN
DesignPattern.v12.suo


+ 77 - 0
MediatorPattern/MediatorPattern.cpp

@@ -0,0 +1,77 @@
+#include <iostream>
+#include <string>
+using namespace std;
+class Person;
+
+class Mediator {
+public:
+    virtual void Send(const string& msg, Person* person) = 0;
+
+    virtual ~Mediator() { }
+
+    void BuildRelation(Person *a, Person *b) {
+        m_a = a;
+        m_b = b;
+    }
+
+protected:
+    Person *m_a;
+    Person *m_b;
+};
+
+class ConcreteMediator :public Mediator {
+public:
+    void Send(const string& msg, Person* person) {
+        if (person == m_a) {
+            cout << msg << "--Mediator--PersonB" << endl;
+        }
+        else if (person == m_b) {
+            cout << msg << "--Mediator--PersonA" << endl;
+        }
+    }
+};
+
+
+class Person {
+public:
+    Person(Mediator*md):_md(md) { }
+    virtual void SendMessage(const string& msg) = 0;
+    virtual ~Person() { }
+protected:
+    Mediator* _md;
+};
+
+class ConcretePersonA :public Person {
+public:
+    ConcretePersonA(Mediator* md):Person(md) { }
+
+    void SendMessage(const string& msg) {
+        _md->Send(msg, this);
+    }
+
+};
+
+class ConcretePersonB :public Person {
+public:
+    ConcretePersonB(Mediator* md):Person(md) { }
+
+    void SendMessage(const string& msg) {
+        _md->Send(msg, this);
+    }
+};
+
+int main() {
+    Mediator *mediator = new ConcreteMediator();
+    Person *personA = new ConcretePersonA(mediator);
+    Person *personB = new ConcretePersonB(mediator);
+
+    mediator->BuildRelation(personA, personB);
+    personA->SendMessage("PersonA");
+    personB->SendMessage("PersonB");
+
+    delete personA;
+    delete personB;
+    delete mediator;
+
+    return 0;
+}

+ 84 - 0
MediatorPattern/MediatorPattern.vcxproj

@@ -0,0 +1,84 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="12.0" 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>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{57CFD205-0966-4F90-86D6-A9647DCA693E}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>MediatorPattern</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <PlatformToolset>v120</PlatformToolset>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <PlatformToolset>v120</PlatformToolset>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </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>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>false</LinkIncremental>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClCompile Include="MediatorPattern.cpp" />
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>

+ 22 - 0
MediatorPattern/MediatorPattern.vcxproj.filters

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="源文件">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="头文件">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
+    </Filter>
+    <Filter Include="资源文件">
+      <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="MediatorPattern.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>