インターフェースに基づくディスパッチ
Coddyの「GDScript開発者向けC#」コースのレッスン 41/48。
GDScriptの呼び出し元は、しばしばダックタイピングに依存します。型指定のないnotifier変数はどのオブジェクトでも受け入れ、send()が存在しない場合も、その行が実行されるまで失敗しません。C#では明示的なINotifierコントラクトを使用し、コンパイル時にチェックしたうえで、それを介してディスパッチします。ルーターは特定のnotifierを構築するのではなく、渡されたオブジェクトを使用する必要があります。そうすれば、新しい実装も置き換え可能なままです。
インターフェースベースの関数は、同じコントラクトを実装する、互いに無関係な実装を受け入れられます。具象型ごとに切り替えるのではなく、インターフェースのメンバーを呼び出してください。この設計なら、ルーターを変更せずに、呼び出し元がテスト用の実装や新しいnotifierを注入できます。このレッスンでは、サービスへの接続は行いません。
GDScriptの参考例
extends Node
class EmailNotifier:
func send(message: String) -> String:
return "email:" + message
func _ready() -> void:
var notifier = EmailNotifier.new()
print(notifier.send("ok"))C#の補助宣言
interface INotifier { string Send(string message); }
class EmailNotifier : INotifier { public string Send(string message) { return "email:"+message; } }Main内のC#ステートメント
INotifier notifier = new EmailNotifier();
Console.WriteLine(notifier.Send("ok"));これらは、特定の内容に焦点を当てた教材用の抜粋です。課題ファイルには実行可能な完全版のラッパーが含まれています。上記のスニペットには、関連する宣言とステートメントが示されています。
参考出力:
email:okC#の出力:
email:okチャレンジ
簡単EmailNotifier.Send、SmsNotifier.Send、Router.Deliver を実装してください。Channel は email または sms です。各 notifier は、チャネル名とコロンを先頭に付けます。Router は、呼び出し元から渡された任意の INotifier に処理を委譲します。これらはテキスト形式のモデルであり、実際のネットワークメッセージングではありません。
ロックされていない C# ファイルを編集してください。表示されているロック済みの Main.cs は、説明されている行を読み取り、Exercise.Solve を呼び出し、入力が変更されていないことを検証し、最後に改行を1つ付けて結果を出力します。宣言済みの API は維持してください。2つ目の Main を追加したり、診断出力を表示したりしないでください。
ロック済みのランチャーは、Solve ラッパーだけでなく、エクスポートされたヘルパーまたはクラスの API も個別にテストします。
自分で試してみよう
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = input[0]; string before0=Snapshot(a0);
var a1 = input[1]; string before1=Snapshot(a1);
if (new EmailNotifier().Send("") != "email:" || new SmsNotifier().Send("x") != "sms:x" || Router.Deliver(new ProbeNotifier(),"x") != "custom:x") throw new Exception("Notifier API");
var result = Exercise.Solve(a0,a1);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
Console.WriteLine(result);
}
}
class ProbeNotifier : INotifier { public string Send(string message) { return "custom:"+message; } }
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
GDScript開発者向けC#のすべてのレッスン
自分で練習してみよう: C#オンラインコンパイラ