Menu
CoddyTech

שליחה מבוססת ממשק

שיעור 41 מתוך 48 בקורס C# למפתחי R של Coddy.

בדרך כלל, קוראים ב-R משתמשים בשליחה באמצעות גנריקה של S3, כגון send(), שמקבלת כל אובייקט שיש למחלקה שלו מתודה, ונכשלת בזמן הריצה עבור אובייקט שאין לו מתודה כזאת. ב-C# משתמשים בחוזה המפורש INotifier, שנבדק בעת הידור הקוד, והשליחה מתבצעת דרכו. הנתב חייב להשתמש באובייקט שסופק לו במקום ליצור רכיב הודעות מסוג מסוים, כדי שמימושים חדשים יוכלו להחליף את הקיימים.

פונקציה המבוססת על ממשק יכולה לקבל מימושים בלתי קשורים של אותו חוזה. יש לקרוא לחבר הממשק במקום לעבור בין טיפוסים קונקרטיים. התכנון הזה מאפשר לקורא להזריק מימוש לבדיקה או רכיב הודעות חדש בלי לשנות את הנתב. בשיעור הזה לא פונים לאף שירות.

הפניה ל-R

send <- function(notifier, message) UseMethod("send")
send.email_notifier <- function(notifier, message) paste0("email:", message)
notifier <- structure(list(), class = "email_notifier")
cat(send(notifier, "ok"), "\n", sep = "")

הצהרות תומכות ב-C#

interface INotifier { string Send(string message); }
class EmailNotifier : INotifier { public string Send(string message) { return "email:"+message; } }

פקודות C# בתוך Main

INotifier notifier = new EmailNotifier();
Console.WriteLine(notifier.Send("ok"));

אלה קטעים ממוקדים להוראה. קובצי האתגר כוללים מעטפות מלאות שניתנות להרצה; הקטעים שלמעלה מציגים את ההצהרות והפקודות הרלוונטיות.

פלט לדוגמה:

email:ok

פלט C#:

email:ok
challenge icon

אתגר

קל

ממשו את EmailNotifier.Send, את SmsNotifier.Send ואת Router.Deliver. הערוץ הוא email או sms. כל notifier מוסיף בתחילת ההודעה את שם הערוץ שלו ונקודתיים. Router מעביר את הקריאה לכל מימוש של INotifier שסיפק הקורא. אלה מודלים לעיצוב טקסט, ולא להעברת הודעות אמיתית ברשת.

ערכו את קובצי C# שאינם נעולים. הקובץ הנעול והגלוי Main.cs קורא את השורות המתוארות, קורא ל-Exercise.Solve, מוודא שהקלט לא השתנה ומדפיס את התוצאה עם תו ירידת שורה אחד בסוף. השאירו את ממשקי ה-API המוצהרים. אל תוסיפו Main שני ואל תדפיסו פלט אבחון.

המשגר הנעול גם בודק באופן עצמאי את ממשק ה-API של פונקציית העזר או המחלקה המיוצאת, ולא רק את מעטפת Solve.

נסו בעצמכם

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; } }
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה C# למפתחי R

תרגלו בעצמכם: קומפיילר C# אונליין