Browse Source

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

Micooz 9 years ago
parent
commit
86a8c93701

+ 1 - 0
.gitignore

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

+ 12 - 0
DesignPattern.sln

@@ -45,6 +45,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VisitorPattern", "VisitorPa
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.vcxproj", "{6EFCBABA-47B7-4209-A463-7D5128D46B2D}"
 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
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		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}.Release|Win32.ActiveCfg = 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
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

BIN
DesignPattern.v12.suo


+ 1 - 1
FlyweightPattern/FlyweightPattern.cpp

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

+ 56 - 0
InterpreterPattern/InterpreterPattern.cpp

@@ -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;
+}

+ 84 - 0
InterpreterPattern/InterpreterPattern.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>{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>

+ 22 - 0
InterpreterPattern/InterpreterPattern.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="InterpreterPattern.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>

+ 4 - 0
InterpreterPattern/InterpreterPattern.vcxproj.user

@@ -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>

+ 36 - 0
IteratorPattern/Aggregate.cpp

@@ -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;
+}
+
+

+ 25 - 0
IteratorPattern/Aggregate.h

@@ -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];
+};
+

+ 32 - 0
IteratorPattern/Iterator.cpp

@@ -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());
+}

+ 25 - 0
IteratorPattern/Iterator.h

@@ -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;
+};

+ 16 - 0
IteratorPattern/IteratorPattern.cpp

@@ -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;
+}

+ 90 - 0
IteratorPattern/IteratorPattern.vcxproj

@@ -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>

+ 36 - 0
IteratorPattern/IteratorPattern.vcxproj.filters

@@ -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>