Interpreter 根据定义的一组合成规则,可以利用解释器模式生成可执行对象
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Interpreter6


{7
public abstract class Command8

{9
public Command()10

{11
_result = 0;12
}13

14
public Command(int result)15

{16
_result = result;17
}18

19
public int _result;20

21
public abstract void Excute();22
}23
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Interpreter6


{7
public class AddOneCommand : Command8

{9
public override void Excute()10

{11
_result += 1;12
Console.WriteLine(_result.ToString());13
}14
}15
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Interpreter6


{7
public class AddTwoCommand : Command8

{9
public override void Excute()10

{11
_result += 2;12
Console.WriteLine(_result.ToString());13
}14
}15
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Interpreter6


{7
public class CommandSequence : Command8

{9
private IList<Command> list = new List<Command>();10

11
public IList<Command> Commands12

{13
get14

{15
return list;16
}17
set18

{19
list = value;20
}21
}22

23
public override void Excute()24

{25
foreach (Command c in list)26

{27
c.Excute();28
}29
}30
}31
}1
Interpreter.Command addone = new AddOneCommand();2
Interpreter.Command addtwo = new AddTwoCommand();3
CommandSequence sequence = new CommandSequence();4
sequence.Commands.Add(addone);5
sequence.Commands.Add(addtwo);6
sequence.Commands.Add(addtwo);7
sequence.Commands.Add(addone);8
sequence.Commands.Add(addtwo);9
sequence.Excute();10
Console.ReadKey();

浙公网安备 33010602011771号