加入访问者模式,责任链模式

This commit is contained in:
Micooz 2014-10-09 10:37:57 +08:00
parent e1a33136af
commit f01d16a686
12 changed files with 425 additions and 0 deletions

View File

@ -0,0 +1,75 @@
#include <iostream>
using namespace std;
class Handle {
public:
virtual ~Handle() { }
virtual void HandleRequest() = 0;
void SetSuccessor(Handle *succ) {
_succ = succ;
}
Handle* GetSuccessor()const {
return _succ;
}
protected:
Handle() { _succ = nullptr; }
Handle(Handle* succ) {
_succ = succ;
}
private:
Handle* _succ;
};
class ConcreteHandleA :public Handle {
public:
ConcreteHandleA() { }
ConcreteHandleA(Handle* succ)
:Handle(succ) {
}
void HandleRequest() {
if (this->GetSuccessor() != 0) {
cout << "ConcreteHandleA--Successor" << endl;
GetSuccessor()->HandleRequest();
}
else {
cout << "ConcreteHandleA::HandleRequest" << endl;
}
}
};
class ConcreteHandleB :public Handle {
public:
ConcreteHandleB() { }
ConcreteHandleB(Handle* succ)
:Handle(succ) {
}
void HandleRequest() {
if (this->GetSuccessor() != 0) {
cout << "ConcreteHandleB--Successor" << endl;
GetSuccessor()->HandleRequest();
}
else {
cout << "ConcreteHandleB::HandleRequest" << endl;
}
}
};
int main() {
Handle *h1 = new ConcreteHandleA();
Handle *h2 = new ConcreteHandleB(h1);// or h1->SetSuccessor(h2);
h2->HandleRequest();
delete h1;
delete h2;
return 0;
}

View File

@ -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>{6EFCBABA-47B7-4209-A463-7D5128D46B2D}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>ChainOfResponsibilityPattern</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="ChainOfResponsibilityPattern.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -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="ChainOfResponsibilityPattern.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -41,6 +41,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MediatorPattern", "Mediator
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CommandPattern", "CommandPattern\CommandPattern.vcxproj", "{53075450-8586-433A-8624-99EAC58E22AD}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisitorPattern", "VisitorPattern\VisitorPattern.vcxproj", "{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.vcxproj", "{6EFCBABA-47B7-4209-A463-7D5128D46B2D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@ -123,6 +127,14 @@ Global
{53075450-8586-433A-8624-99EAC58E22AD}.Debug|Win32.Build.0 = Debug|Win32
{53075450-8586-433A-8624-99EAC58E22AD}.Release|Win32.ActiveCfg = Release|Win32
{53075450-8586-433A-8624-99EAC58E22AD}.Release|Win32.Build.0 = Release|Win32
{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}.Debug|Win32.ActiveCfg = Debug|Win32
{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}.Debug|Win32.Build.0 = Debug|Win32
{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}.Release|Win32.ActiveCfg = Release|Win32
{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}.Release|Win32.Build.0 = Release|Win32
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Debug|Win32.ActiveCfg = Debug|Win32
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Debug|Win32.Build.0 = Debug|Win32
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Release|Win32.ActiveCfg = Release|Win32
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Binary file not shown.

View File

@ -0,0 +1,19 @@
#include "Element.h"
#include <iostream>
using namespace std;
Element::Element() {
}
Element::~Element() {
}
void ConcreteElementA::Accept(Visitor* vis) {
vis->VisitConcreteElementA(this);
cout << "visiting ConcreteElementA..." << endl;
}
void ConcreteElementB::Accept(Visitor* vis) {
vis->VisitConcreteElementB(this);
cout << "visiting ConcreteElementA..." << endl;
}

20
VisitorPattern/Element.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "Visitor.h"
class Visitor;
class Element {
public:
Element();
virtual ~Element();
virtual void Accept(Visitor* vis) = 0;
};
class ConcreteElementA :public Element {
public:
void Accept(Visitor* vis);
};
class ConcreteElementB :public Element {
public:
void Accept(Visitor* vis);
};

View File

@ -0,0 +1,26 @@
#include "Visitor.h"
#include <iostream>
using namespace std;
Visitor::Visitor() {
}
Visitor::~Visitor() {
}
void ConcreteVisitorA::VisitConcreteElementA(Element* elm) {
cout << "i will visit ConcreteElementA..." << endl;
}
void ConcreteVisitorA::VisitConcreteElementB(Element* elm) {
cout << "i will visit ConcreteElementB..." << endl;
}
void ConcreteVisitorB::VisitConcreteElementA(Element* elm) {
cout << "i will visit ConcreteElementA..." << endl;
}
void ConcreteVisitorB::VisitConcreteElementB(Element* elm) {
cout << "i will visit ConcreteElementB..." << endl;
}

26
VisitorPattern/Visitor.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "Element.h"
class Element;
class Visitor {
public:
virtual ~Visitor();
virtual void VisitConcreteElementA(Element* elm) = 0;
virtual void VisitConcreteElementB(Element* elm) = 0;
protected:
Visitor();
};
class ConcreteVisitorA :public Visitor {
public:
void VisitConcreteElementA(Element* elm);
void VisitConcreteElementB(Element* elm);
};
class ConcreteVisitorB :public Visitor {
public:
void VisitConcreteElementA(Element* elm);
void VisitConcreteElementB(Element* elm);
};

View File

@ -0,0 +1,15 @@
#include <iostream>
#include "Element.h"
#include "Visitor.h"
using namespace std;
int main() {
Visitor *vis = new ConcreteVisitorA();
Element *elm = new ConcreteElementA();
elm->Accept(vis);
delete elm;
delete vis;
return 0;
}

View File

@ -0,0 +1,90 @@
<?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>{EFF4AD49-9352-47FD-99CE-CF6E91F8DA7D}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>VisitorPattern</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="Element.cpp" />
<ClCompile Include="Visitor.cpp" />
<ClCompile Include="VisitorPattern.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Element.h" />
<ClInclude Include="Visitor.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View 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="源文件">
<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="VisitorPattern.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Visitor.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Element.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Visitor.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Element.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>