1 interface TestCommand 2 { 3 TestCommand command(string cmd) @safe; 4 TestCommand update() @safe; 5 } 6 string msg; 7 8 TestCommand test1; 9 TestCommand test2; 10 11 test1 = new class TestCommand 12 { 13 TestCommand command(string cmd) @safe 14 { 15 msg = cmd; 16 // not transit 17 return this; 18 } 19 TestCommand update() @safe 20 { 21 msg = ""; 22 // transit to child(test2) 23 return test2; 24 } 25 }; 26 test2 = new class TestCommand 27 { 28 TestCommand command(string cmd) @safe 29 { 30 msg = cmd ~ "!"; 31 // not transit 32 return this; 33 } 34 TestCommand update() @safe 35 { 36 msg = "!"; 37 // transit to parent(test1) 38 return null; 39 } 40 }; 41 42 auto sts = new Flow!TestCommand(test1); 43 44 // not transit 45 sts.command("test"); 46 assert(msg == "test"); 47 48 // transit to child(test2) 49 sts.update(); 50 assert(msg == ""); 51 52 // not transit 53 sts.command("test"); 54 assert(msg == "test!"); 55 56 // transit to parent(test1) 57 sts.update(); 58 assert(msg == "!"); 59 60 // not transit 61 sts.command("test"); 62 assert(msg == "test");
Flow template class
Flow is a template class that holds the state based on the 'state pattern'. The interface of the command set whose behavior changes depending on the state is given by the Commands parameter. The default base class of this template class is Object, but it can be specified by Base. The initial state of the concrete instance of Commands is given to the constructor of this template class, and transition is made according to the return value of each command. If the command returns null, the transition is ended.