加入解释器模式,迭代器模式

This commit is contained in:
Micooz 2014-10-09 11:46:41 +08:00
parent f01d16a686
commit 86a8c93701
15 changed files with 440 additions and 1 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
Debug/ Debug/
Release/
*.sdf *.sdf
*.opensdf *.opensdf
*.suo *.suo

View File

@ -45,6 +45,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisitorPattern", "VisitorPa
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.vcxproj", "{6EFCBABA-47B7-4209-A463-7D5128D46B2D}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.vcxproj", "{6EFCBABA-47B7-4209-A463-7D5128D46B2D}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IteratorPattern", "IteratorPattern\IteratorPattern.vcxproj", "{EE079CA8-B725-4D1B-AC3D-0F3228268767}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "InterpreterPattern", "InterpreterPattern\InterpreterPattern.vcxproj", "{BB4E709E-915E-4709-9636-5B882F31814C}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32 Debug|Win32 = Debug|Win32
@ -135,6 +139,14 @@ Global
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Debug|Win32.Build.0 = 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.ActiveCfg = Release|Win32
{6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Release|Win32.Build.0 = Release|Win32 {6EFCBABA-47B7-4209-A463-7D5128D46B2D}.Release|Win32.Build.0 = Release|Win32
{EE079CA8-B725-4D1B-AC3D-0F3228268767}.Debug|Win32.ActiveCfg = Debug|Win32
{EE079CA8-B725-4D1B-AC3D-0F3228268767}.Debug|Win32.Build.0 = Debug|Win32
{EE079CA8-B725-4D1B-AC3D-0F3228268767}.Release|Win32.ActiveCfg = Release|Win32
{EE079CA8-B725-4D1B-AC3D-0F3228268767}.Release|Win32.Build.0 = Release|Win32
{BB4E709E-915E-4709-9636-5B882F31814C}.Debug|Win32.ActiveCfg = Debug|Win32
{BB4E709E-915E-4709-9636-5B882F31814C}.Debug|Win32.Build.0 = Debug|Win32
{BB4E709E-915E-4709-9636-5B882F31814C}.Release|Win32.ActiveCfg = Release|Win32
{BB4E709E-915E-4709-9636-5B882F31814C}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Binary file not shown.

View File

@ -27,7 +27,7 @@ public:
} }
void Operation(const string& state) { void Operation(const string& state) {
cout << "ConcreteFlyweight " << GetState() << " \ " << state << endl; cout << "ConcreteFlyweight " << GetState() << " \\ " << state << endl;
} }
}; };

View File

@ -0,0 +1,56 @@
#include <iostream>
#include <string>
using namespace std;
class Context { };
class Expression {
public:
virtual ~Expression() { }
virtual void Interpret(const Context& c) = 0;
};
class TerminalExpression :public Expression {
public:
TerminalExpression(const string& statement) {
_statement = statement;
}
void Interpret(const Context& c) {
cout << this->_statement << " -- TerminalExpression" << endl;
}
private:
string _statement;
};
class NonterminalExpression :public Expression {
public:
NonterminalExpression(Expression* expression, int times) {
_expression = expression;
_times = times;
}
void Interpret(const Context& c) {
for (int i = 0; i < _times; i++) {
_expression->Interpret(c);
}
}
private:
Expression *_expression;
int _times;
};
int main() {
Context *c = new Context();
Expression *tp = new TerminalExpression("echo");
Expression *ntp = new NonterminalExpression(tp, 4);
ntp->Interpret(*c);
delete ntp;
delete tp;
delete c;
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>{BB4E709E-915E-4709-9636-5B882F31814C}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>InterpreterPattern</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="InterpreterPattern.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="InterpreterPattern.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

View File

@ -0,0 +1,36 @@
#include <iostream>
using namespace std;
#include "Aggregate.h"
#include "Iterator.h"
Aggregate::Aggregate() {
}
Aggregate::~Aggregate() {
}
ConcreteAggreaget::ConcreteAggreaget() {
for (int i = 0; i < SIZE; i++) {
_objs[i] = i;
}
}
//Iterator* ConcreteAggreaget::CreateIterator() {
// return new ConcreteIterator(this);
//}
Object ConcreteAggreaget::GetItem(int idx) {
if (idx < this->GetSize()) {
return _objs[idx];
}
else {
return -1;
}
}
int ConcreteAggreaget::GetSize() {
return SIZE;
}

View File

@ -0,0 +1,25 @@
#pragma once
class Iterator;
typedef int Object;
class Aggregate {
public:
Aggregate();
virtual ~Aggregate();
//virtual Iterator* CreateIterator() = 0;
virtual Object GetItem(int idx) = 0;
virtual int GetSize() = 0;
};
class ConcreteAggreaget :public Aggregate {
public:
enum { SIZE = 3 };
ConcreteAggreaget();
//Iterator* CreateIterator();
Object GetItem(int idx);
int GetSize();
private:
Object _objs[SIZE];
};

View File

@ -0,0 +1,32 @@
#include "Iterator.h"
Iterator::Iterator() {
}
Iterator::~Iterator() {
}
ConcreteIterator::ConcreteIterator(Aggregate *ag, int idx = 0) {
_ag = ag;
_idx = idx;
}
Object ConcreteIterator::CurrentItem() {
return _ag->GetItem(_idx);
}
void ConcreteIterator::First() {
_idx = 0;
}
void ConcreteIterator::Next() {
if (_idx < _ag->GetSize()) {
_idx++;
}
}
bool ConcreteIterator::IsDone() {
return (_idx == _ag->GetSize());
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "Aggregate.h"
class Iterator {
public:
Iterator();
virtual ~Iterator();
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() = 0;
virtual Object CurrentItem() = 0;
};
class ConcreteIterator :public Iterator {
public:
ConcreteIterator(Aggregate *ag, int idx /* = 0 */);
void First();
void Next();
bool IsDone();
Object CurrentItem();
private:
Aggregate* _ag;
int _idx;
};

View File

@ -0,0 +1,16 @@
#include <iostream>
#include "Aggregate.h"
#include "Iterator.h"
using namespace std;
int main() {
Aggregate* ag = new ConcreteAggreaget();
Iterator *it = new ConcreteIterator(ag, 0);
for (; !(it->IsDone()); it->Next()) {
cout << it->CurrentItem() << endl;
}
delete it;
delete ag;
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>{EE079CA8-B725-4D1B-AC3D-0F3228268767}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>IteratorPattern</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="Aggregate.cpp" />
<ClCompile Include="Iterator.cpp" />
<ClCompile Include="IteratorPattern.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Aggregate.h" />
<ClInclude Include="Iterator.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="IteratorPattern.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Aggregate.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Iterator.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Aggregate.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Iterator.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>