API 扩展中的对象链接与方法
Object Chaining vs. Methods in API extension
所以我正在根据自己的需要查看 extending/adapting 和 API。我说的是 Lego Mindstorms C# API。我正在围绕它构建自己的 API 类型(基于适配器模式),以便我可以以更好的 OO 方式对机器人进行编程。
这是关于 API 工作原理的 link:Lego Mindstorms EV3 C# API
但现在我被 C# API 处理命令的一种非常奇怪的方式困住了。
Definitely not the OO-way...
示例:要向砖块发送命令,您需要有一个砖块实例来发送命令。但是 DirectCommand 实例与砖块无关。
await brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50, 5000);
所以我想做的是让 brick 和 DirectCommand 松耦合。
这是另一个例子: 执行一批命令。你必须写出所有的命令,然后执行某个方法。在当前 API 中,无法循环遍历数组并将它们添加到堆栈元素,以便稍后执行它们。
brick.BatchCommand.TurnMotorAtSpeedForTime(OutputPort.A, 50, 1000, false);
brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, 50, 1000, false);
brick.BatchCommand.PlayTone(50, 1000, 500);
await brick.BatchCommand.SendCommandAsync();
所以我想做的事情是:
创建一个类似 PlayTone(..) 的命令,将其添加到命令数组列表中,然后遍历它...
List<Command> toBeExecuted = new List<Command>;
toBeExecuted.Add(DirectCommand.PlayTone(50, 1000, 500));
brick.DirectCommand(toBeExecuted[0]);
所以如果有人能提供帮助...我会很高兴:)
不完全是它们的设计目的,但是您可以将它们作为任务列表排队,然后传递给它吗?
像这样:
static void Main(string[] args)
{
//---- queue commands into a "batch"
List<Task> toBeExecuted = new List<Task>();
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
//---- elsewhere
Task.WaitAll(toBeExecuted.ToArray()); //fire off the batch
await brick.BatchCommand.SendCommandAsync(); //send to brick
}
将 dothing() 替换为您要排队的批处理命令:
.Add(Task.Run(() => brick.BatchCommand...()));
所以我正在根据自己的需要查看 extending/adapting 和 API。我说的是 Lego Mindstorms C# API。我正在围绕它构建自己的 API 类型(基于适配器模式),以便我可以以更好的 OO 方式对机器人进行编程。
这是关于 API 工作原理的 link:Lego Mindstorms EV3 C# API
但现在我被 C# API 处理命令的一种非常奇怪的方式困住了。
Definitely not the OO-way...
示例:要向砖块发送命令,您需要有一个砖块实例来发送命令。但是 DirectCommand 实例与砖块无关。
await brick.DirectCommand.TurnMotorAtPowerAsync(OutputPort.A, 50, 5000);
所以我想做的是让 brick 和 DirectCommand 松耦合。
这是另一个例子: 执行一批命令。你必须写出所有的命令,然后执行某个方法。在当前 API 中,无法循环遍历数组并将它们添加到堆栈元素,以便稍后执行它们。
brick.BatchCommand.TurnMotorAtSpeedForTime(OutputPort.A, 50, 1000, false);
brick.BatchCommand.TurnMotorAtPowerForTime(OutputPort.C, 50, 1000, false);
brick.BatchCommand.PlayTone(50, 1000, 500);
await brick.BatchCommand.SendCommandAsync();
所以我想做的事情是:
创建一个类似 PlayTone(..) 的命令,将其添加到命令数组列表中,然后遍历它...
List<Command> toBeExecuted = new List<Command>;
toBeExecuted.Add(DirectCommand.PlayTone(50, 1000, 500));
brick.DirectCommand(toBeExecuted[0]);
所以如果有人能提供帮助...我会很高兴:)
不完全是它们的设计目的,但是您可以将它们作为任务列表排队,然后传递给它吗?
像这样:
static void Main(string[] args)
{
//---- queue commands into a "batch"
List<Task> toBeExecuted = new List<Task>();
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
toBeExecuted.Add(Task.Run(() => dothing()));
//---- elsewhere
Task.WaitAll(toBeExecuted.ToArray()); //fire off the batch
await brick.BatchCommand.SendCommandAsync(); //send to brick
}
将 dothing() 替换为您要排队的批处理命令:
.Add(Task.Run(() => brick.BatchCommand...()));