Skip to content

Commit a18ba30

Browse files
committed
add Interpreter and Iterator Pattern
1 parent 6965ece commit a18ba30

15 files changed

Lines changed: 265 additions & 0 deletions

Assets/Interpreter Pattern.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.

Assets/Interpreter Pattern/Structure.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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//-------------------------------------------------------------------------------------
2+
// InterpreterStructrue.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
public class InterpreterStructrue : MonoBehaviour
9+
{
10+
void Start ( )
11+
{
12+
Context context = new Context();
13+
14+
// Usually a tree
15+
ArrayList list = new ArrayList();
16+
17+
// Populate 'abstract syntax tree'
18+
list.Add(new TerminalExpression());
19+
list.Add(new NonterminalExpression());
20+
list.Add(new TerminalExpression());
21+
list.Add(new TerminalExpression());
22+
23+
// Interpret
24+
foreach (AbstractExpression exp in list)
25+
{
26+
exp.Interpret(context);
27+
}
28+
29+
}
30+
}
31+
32+
/// <summary>
33+
/// The 'Context' class
34+
/// </summary>
35+
class Context
36+
{
37+
}
38+
39+
/// <summary>
40+
/// The 'AbstractExpression' abstract class
41+
/// </summary>
42+
abstract class AbstractExpression
43+
{
44+
public abstract void Interpret(Context context);
45+
}
46+
47+
/// <summary>
48+
/// The 'TerminalExpression' class
49+
/// </summary>
50+
class TerminalExpression : AbstractExpression
51+
{
52+
public override void Interpret(Context context)
53+
{
54+
Debug.Log("Called Terminal.Interpret()");
55+
}
56+
}
57+
58+
/// <summary>
59+
/// The 'NonterminalExpression' class
60+
/// </summary>
61+
class NonterminalExpression : AbstractExpression
62+
{
63+
public override void Interpret(Context context)
64+
{
65+
Debug.Log("Called Nonterminal.Interpret()");
66+
}
67+
}

Assets/Interpreter Pattern/Structure/InterpreterStructrue.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/Interpreter Pattern/Structure/TestInterpreterStructrue.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.

Assets/Iterator Pattern.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.

Assets/Iterator Pattern/Structure.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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//-------------------------------------------------------------------------------------
2+
// IteratorStructure.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
public class IteratorStructure : MonoBehaviour
9+
{
10+
void Start ( )
11+
{
12+
ConcreteAggregate a = new ConcreteAggregate();
13+
a[0] = "Item A";
14+
a[1] = "Item B";
15+
a[2] = "Item C";
16+
a[3] = "Item D";
17+
18+
// Create Iterator and provide aggregate
19+
Iterator i = a.CreateIterator();
20+
21+
Debug.Log("Iterating over collection:");
22+
23+
object item = i.First();
24+
while (item != null)
25+
{
26+
Debug.Log(item);
27+
item = i.Next();
28+
}
29+
}
30+
}
31+
32+
/// <summary>
33+
/// The 'Aggregate' abstract class
34+
/// </summary>
35+
abstract class Aggregate
36+
{
37+
public abstract Iterator CreateIterator();
38+
}
39+
40+
/// <summary>
41+
/// The 'ConcreteAggregate' class
42+
/// </summary>
43+
class ConcreteAggregate : Aggregate
44+
{
45+
private ArrayList _items = new ArrayList();
46+
47+
public override Iterator CreateIterator()
48+
{
49+
return new ConcreteIterator(this);
50+
}
51+
52+
// Gets item count
53+
public int Count
54+
{
55+
get { return _items.Count; }
56+
}
57+
58+
// Indexer
59+
public object this[int index]
60+
{
61+
get { return _items[index]; }
62+
set { _items.Insert(index, value); }
63+
}
64+
}
65+
66+
/// <summary>
67+
/// The 'Iterator' abstract class
68+
/// </summary>
69+
abstract class Iterator
70+
{
71+
public abstract object First();
72+
public abstract object Next();
73+
public abstract bool IsDone();
74+
public abstract object CurrentItem();
75+
}
76+
77+
/// <summary>
78+
/// The 'ConcreteIterator' class
79+
/// </summary>
80+
class ConcreteIterator : Iterator
81+
{
82+
private ConcreteAggregate _aggregate;
83+
private int _current = 0;
84+
85+
// Constructor
86+
public ConcreteIterator(ConcreteAggregate aggregate)
87+
{
88+
this._aggregate = aggregate;
89+
}
90+
91+
// Gets first iteration item
92+
public override object First()
93+
{
94+
return _aggregate[0];
95+
}
96+
97+
// Gets next iteration item
98+
public override object Next()
99+
{
100+
object ret = null;
101+
if (_current < _aggregate.Count - 1)
102+
{
103+
ret = _aggregate[++_current];
104+
}
105+
106+
return ret;
107+
}
108+
109+
// Gets current iteration item
110+
public override object CurrentItem()
111+
{
112+
return _aggregate[_current];
113+
}
114+
115+
// Gets whether iterations are complete
116+
public override bool IsDone()
117+
{
118+
return _current >= _aggregate.Count;
119+
}
120+
}

Assets/Iterator Pattern/Structure/IteratorStructure.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.

0 commit comments

Comments
 (0)