Skip to content

Commit d98dff5

Browse files
committed
add Interpreter Design Pattern example
1 parent 141b32b commit d98dff5

File tree

6 files changed

+227
-44
lines changed

6 files changed

+227
-44
lines changed

Assembly-CSharp.csproj

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>10.0.20506</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<RootNamespace></RootNamespace>
9-
<ProjectGuid>{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}</ProjectGuid>
10-
<OutputType>Library</OutputType>
11-
<AppDesignerFolder>Properties</AppDesignerFolder>
12-
<AssemblyName>Assembly-CSharp</AssemblyName>
13-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14-
<FileAlignment>512</FileAlignment>
15-
<BaseDirectory>Assets</BaseDirectory>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>10.0.20506</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<RootNamespace></RootNamespace>
9+
<ProjectGuid>{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}</ProjectGuid>
10+
<OutputType>Library</OutputType>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
<AssemblyName>Assembly-CSharp</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<BaseDirectory>Assets</BaseDirectory>
1616
</PropertyGroup>
1717
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18-
<DebugSymbols>true</DebugSymbols>
19-
<DebugType>full</DebugType>
20-
<Optimize>false</Optimize>
21-
<OutputPath>Temp\bin\Debug\</OutputPath>
22-
<DefineConstants>DEBUG;TRACE;UNITY_5_2_1;UNITY_5_2;UNITY_5;ENABLE_NEW_BUGREPORTER;ENABLE_2D_PHYSICS;ENABLE_4_6_FEATURES;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_FRAME_DEBUGGER;ENABLE_GENERICS;ENABLE_HOME_SCREEN;ENABLE_IMAGEEFFECTS;ENABLE_LIGHT_PROBES_LEGACY;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_PLUGIN_INSPECTOR;ENABLE_SHADOWS;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_SPRITES;ENABLE_TERRAIN;ENABLE_RAKNET;ENABLE_UNET;ENABLE_UNITYEVENTS;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ADS;ENABLE_CLOUD_HUB;ENABLE_CLOUD_PROJECT_ID;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_SERVICES_BUILD;ENABLE_CLOUD_LICENSE;ENABLE_EDITOR_METRICS;ENABLE_REFLECTION_BUFFERS;INCLUDE_DYNAMIC_GI;INCLUDE_GI;INCLUDE_IL2CPP;INCLUDE_DIRECTX12;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;ENABLE_LOCALIZATION;ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION;UNITY_WEBPLAYER;ENABLE_SUBSTANCE;WEBPLUG;ENABLE_TEXTUREID_MAP;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_MONO;ENABLE_PROFILER;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN;UNITY_TEAM_LICENSE;UNITY_PRO_LICENSE</DefineConstants>
23-
<ErrorReport>prompt</ErrorReport>
24-
<WarningLevel>4</WarningLevel>
25-
<NoWarn>0169</NoWarn>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>Temp\bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE;UNITY_5_3_3;UNITY_5_3;UNITY_5;ENABLE_NEW_BUGREPORTER;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_FRAME_DEBUGGER;ENABLE_GENERICS;ENABLE_HOME_SCREEN;ENABLE_IMAGEEFFECTS;ENABLE_LIGHT_PROBES_LEGACY;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_PLUGIN_INSPECTOR;ENABLE_SHADOWS;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_SPRITERENDERER_FLIPPING;ENABLE_SPRITES;ENABLE_SPRITE_POLYGON;ENABLE_TERRAIN;ENABLE_RAKNET;ENABLE_UNET;ENABLE_UNITYEVENTS;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ADS;ENABLE_CLOUD_HUB;ENABLE_CLOUD_PROJECT_ID;ENABLE_CLOUD_SERVICES_PURCHASING;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_SERVICES_BUILD;ENABLE_CLOUD_LICENSE;ENABLE_EDITOR_METRICS;ENABLE_EDITOR_METRICS_CACHING;INCLUDE_DYNAMIC_GI;INCLUDE_GI;INCLUDE_IL2CPP;INCLUDE_DIRECTX12;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;ENABLE_LOCALIZATION;ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION;ENABLE_EDITOR_TESTS_RUNNER;UNITY_WEBPLAYER;ENABLE_SUBSTANCE;WEBPLUG;ENABLE_TEXTUREID_MAP;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_MONO;ENABLE_PROFILER;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN;UNITY_TEAM_LICENSE;UNITY_PRO_LICENSE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
<NoWarn>0169</NoWarn>
2626
</PropertyGroup>
2727
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28-
<DebugType>pdbonly</DebugType>
29-
<Optimize>true</Optimize>
30-
<OutputPath>Temp\bin\Release\</OutputPath>
31-
<ErrorReport>prompt</ErrorReport>
32-
<WarningLevel>4</WarningLevel>
33-
<NoWarn>0169</NoWarn>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>Temp\bin\Release\</OutputPath>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
<NoWarn>0169</NoWarn>
3434
</PropertyGroup>
3535
<ItemGroup>
36-
<Reference Include="System" />
36+
<Reference Include="System" />
3737
<Reference Include="System.XML" />
38-
<Reference Include="System.Core" />
39-
<Reference Include="System.Xml.Linq" />
40-
<Reference Include="UnityEngine">
41-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/UnityEngine.dll</HintPath>
42-
</Reference>
43-
<Reference Include="UnityEditor">
44-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/UnityEditor.dll</HintPath>
45-
</Reference>
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="UnityEngine">
41+
<HintPath>D:/Program Files/Unity 5.3.3/Editor/Data/Managed/UnityEngine.dll</HintPath>
42+
</Reference>
43+
<Reference Include="UnityEditor">
44+
<HintPath>D:/Program Files/Unity 5.3.3/Editor/Data/Managed/UnityEditor.dll</HintPath>
45+
</Reference>
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="Assets\Behavioral Patterns\Chain of Responsibility Pattern\Example1\ChainOfResponsibilityExample1.cs" />
@@ -129,21 +129,15 @@
129129
<Compile Include="Assets\Structural Patterns\Proxy Pattern\Exmaple1\ProxyPatternExample1.cs" />
130130
<Compile Include="Assets\Structural Patterns\Proxy Pattern\Structure\ProxyStructure.cs" />
131131
<Reference Include="UnityEngine.UI">
132-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
132+
<HintPath>D:/Program Files/Unity 5.3.3/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
133133
</Reference>
134134
<Reference Include="UnityEngine.Networking">
135-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll</HintPath>
136-
</Reference>
137-
<Reference Include="Mono.Cecil">
138-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/Managed/Mono.Cecil.dll</HintPath>
139-
</Reference>
140-
<Reference Include="UnityEditor.iOS.Extensions.Xcode">
141-
<HintPath>D:/Program Files/Unity 5.2/Editor/Data/PlaybackEngines/iOSSupport/UnityEditor.iOS.Extensions.Xcode.dll</HintPath>
135+
<HintPath>D:/Program Files/Unity 5.3.3/Editor/Data/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll</HintPath>
142136
</Reference>
143137
</ItemGroup>
144138
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
145139
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
146-
Other similar extension points exist, see Microsoft.Common.targets.
140+
Other similar extension points exist, see Microsoft.Common.targets.
147141
<Target Name="BeforeBuild">
148142
</Target>
149143
<Target Name="AfterBuild">

Assets/Behavioral Patterns/Interpreter Pattern/Example2.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//-------------------------------------------------------------------------------------
2+
// InterpreterExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using System;
6+
using UnityEngine;
7+
using System.Collections;
8+
using System.Globalization;
9+
using System.Reflection;
10+
11+
public class InterpreterExample2 : MonoBehaviour
12+
{
13+
void Start ( )
14+
{
15+
string question1 = "2 Gallons to pints";
16+
AskQuestion(question1);
17+
18+
string question2 = "4 Gallons to tablespoons";
19+
AskQuestion(question2);
20+
}
21+
22+
protected void AskQuestion(string question)
23+
{
24+
ConversionContext context = new ConversionContext(question);
25+
26+
string fromConversion = context.fromConversion; // in this example fromConversion is always the second word
27+
string toConversion = context.toConversion;
28+
double quantity = context.quantity;
29+
30+
// Trying to get a matching class for the word "fromConversion"
31+
try
32+
{
33+
// Getting the type, we also have to define the namespace (in this case InterpreterPattern as defined above)
34+
// and fromConversion should hold the class name (in this case Gallons)
35+
Type type = Type.GetType("InterpreterPattern." + fromConversion);
36+
object instance = Activator.CreateInstance(type);
37+
Expression expression = instance as Expression;
38+
39+
// Get the matching method: e.g. (toConversion = pints)
40+
MethodInfo method = type.GetMethod(toConversion);
41+
string result = (string)method.Invoke(instance, new object[] { quantity });
42+
43+
Debug.Log("Output: " + quantity.ToString() + " " + fromConversion + " are " + result + " " + toConversion);
44+
}
45+
catch (Exception e)
46+
{
47+
Debug.Log(e.Message);
48+
}
49+
}
50+
}
51+
52+
53+
// Context object that does try to make sense of an input string:
54+
public class ConversionContext
55+
{
56+
public string conversionQues { get; protected set; }
57+
58+
public string fromConversion { get; protected set; }
59+
60+
public string toConversion { get; protected set; }
61+
62+
public double quantity { get; protected set; }
63+
64+
protected string[] partsOfQues;
65+
66+
67+
68+
// here happens the sensemaking
69+
public ConversionContext(string input)
70+
{
71+
Debug.Log("Input: " + input);
72+
this.conversionQues = input;
73+
this.partsOfQues = input.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
74+
75+
if (partsOfQues.Length >= 4)
76+
{
77+
78+
fromConversion = GetCapitalized(partsOfQues[1]);
79+
// 1 gallon to pints
80+
toConversion = GetLowerCase(partsOfQues[3]);
81+
82+
// get quantitiy:
83+
double quant;
84+
double.TryParse(partsOfQues[0], out quant);
85+
this.quantity = quant;
86+
}
87+
}
88+
89+
// Some helper methods:
90+
protected string GetCapitalized(string word)
91+
{
92+
word = word.ToLower();
93+
word = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word);
94+
95+
// make sure a 's' is appended
96+
if (word.EndsWith("s") == false)
97+
{
98+
word += "s";
99+
}
100+
101+
return word;
102+
}
103+
104+
protected string GetLowerCase(string word)
105+
{
106+
return word.ToLower();
107+
}
108+
}
109+
110+
111+
112+
113+
// Definition of all the things the concrete expression
114+
// shall be able to convert into
115+
public abstract class Expression
116+
{
117+
public abstract string gallons(double quantity);
118+
119+
public abstract string quarts(double quantity);
120+
121+
public abstract string pints(double quantity);
122+
123+
public abstract string cups(double quantity);
124+
125+
public abstract string tablespoons(double quantity);
126+
}
127+
128+
129+
// concrete class
130+
public class Gallons : Expression
131+
{
132+
#region implemented abstract members of Expression
133+
134+
public override string gallons(double quantity)
135+
{
136+
return quantity.ToString();
137+
}
138+
139+
public override string quarts(double quantity)
140+
{
141+
return (quantity * 4).ToString();
142+
}
143+
144+
public override string pints(double quantity)
145+
{
146+
return (quantity * 8).ToString();
147+
}
148+
149+
public override string cups(double quantity)
150+
{
151+
return (quantity * 16).ToString();
152+
}
153+
154+
public override string tablespoons(double quantity)
155+
{
156+
return (quantity * 256).ToString();
157+
}
158+
159+
#endregion
160+
}

Assets/Behavioral Patterns/Interpreter Pattern/Example2/InterpreterExample2.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Behavioral Patterns/Interpreter Pattern/Example2/InterpreterExample2.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)