Menu
CoddyTech

التوجيه عبر الواجهات

الدرس 41 من 48 في دورة C# لمطوري C++ على Coddy.

تتيح المراجع إلى الأصناف الأساسية المجرّدة في C++ والمراجع إلى الواجهات في C# سلوكًا قابلًا للاستبدال. لا تستخدم C# الوراثة المتعددة للأصناف في C++ لهذا العقد. يجب أن يستدعي الموجّه كائن الواجهة المقدَّم، لا أن ينشئ بنفسه أداة إشعار محددة.

يمكن لدالة تعتمد على واجهة أن تقبل تطبيقات غير مرتبطة للعقد نفسه. استدعِ عضو الواجهة بدلًا من التبديل بين الأنواع الملموسة. يتيح هذا التصميم للجهة المستدعية حقن تطبيق للاختبار أو أداة إشعار جديدة دون تغيير الموجّه. لا يتم الاتصال بأي خدمة في هذا الدرس.

مرجع C++

#include <algorithm>
#include <array>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
struct Notifier { virtual string send(const string& message) const=0; virtual ~Notifier()=default; };
struct EmailNotifier: Notifier { string send(const string& message) const override { return "email:"+message; } };
int main() {
EmailNotifier concrete; Notifier& notifier=concrete; cout << notifier.send("ok") << "\n";
}

تصريحات مساندة في C#

interface IMeasure { int Measure(string value); }
class TextLength : IMeasure { public int Measure(string value) { return value.Length; } }

تعليمات C# داخل Main

IMeasure measure = new TextLength();
Console.WriteLine(measure.Measure("word"));

هذه مقتطفات تعليمية مركّزة. تتضمن ملفات التحدي أغلفة تنفيذية كاملة؛ وتعرض المقاطع أعلاه التصريحات والتعليمات ذات الصلة.

المخرجات المرجعية:

email:ok

مخرجات C#:

4
challenge icon

التحدي

سهل

نفّذ EmailNotifier.Send وSmsNotifier.Send وRouter.Deliver. قيمة القناة هي email أو sms. تضيف كل أداة إشعار بادئةً تتضمن اسم قناتها ونقطتين. يفوّض 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# لمطوري C++

تدرّب بنفسك: مترجم C# عبر الإنترنت