Menu
CoddyTech

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

الدرس 36 من 48 في دورة ‏C# لمطوري Luau على Coddy.

أنواع Luau هيكلية: أي جدول يحتوي على حقل apply بنوع الدالة الصحيح يفي بمتطلبات PricePolicy، وعند وقت التشغيل يكون الاستدعاء مجرد تطبيق لأسلوب «التعامل مع البنية» على حقل في جدول. واجهات C# اسمية: يجب أن يصرّح الصنف بأنه ينفّذ IFormatter، ولا تكفي مطابقة الطريقة وحدها. حافظ على حقن التبعيات أثناء ترجمة التنفيذ إلى أصناف C# صريحة.

تسمّي الواجهة عقدًا يحدّد الأعضاء المطلوبة، وتعلن الأصناف التي تنفّذها عن هذه العلاقة. يخزّن التركيب مكوّنًا مساعدًا ويفوّض إليه العمل. وهذا يُبقي Checkout مستقلًا عن صنف خصم معيّن. يحقن برنامج التشغيل سياسته الخاصة، لذا لا يمكن تلبية API بترميز حساب VIP مباشرةً في Checkout.

مرجع Luau

type PricePolicy = { apply: (self: PricePolicy, price: number) -> number }
local Checkout = {}
Checkout.__index = Checkout
function Checkout.new(policy: PricePolicy)
    return setmetatable({ policy = policy }, Checkout)
end
function Checkout:total(price: number): number
    return self.policy:apply(price)
end
print(Checkout.new({ apply = function(_, price) return price end }):total(40))

تعريفات C# المساعدة

interface IFormatter { string Format(string value); }
class Upper : IFormatter { public string Format(string value) { return value.ToUpperInvariant(); } }
class Display { private IFormatter formatter; public Display(IFormatter formatter) { this.formatter = formatter; } public string Render(string value) { return formatter.Format(value); } }

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

Console.WriteLine(new Display(new Upper()).Render("pen"));

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

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

40

مخرجات C#:

PEN
challenge icon

التحدي

سهل

نفّذ StandardPolicy وVipPolicy وCheckout في ملفاتها الخاصة. تُعيد Standard السعر؛ وتُعيد VIP نسبة 90 بالمئة باستخدام الحساب بأعداد صحيحة. الأسعار غير سالبة ومضاعفات للعشرة. يجب أن يفوّض Checkout إلى IPricePolicy المحقونة، بما في ذلك السياسات المخصّصة التي يوفّرها المستدعون.

عدّل ملفات C# غير المقفلة. يقرأ Main.cs المقفل والظاهر الأسطر الموصوفة، ويستدعي Exercise.Solve، ويتحقق من عدم تعديل المدخلات، ويطبع النتيجة مع سطر أخير واحد. أبقِ واجهات البرمجة المعلنة. لا تضف Main ثانية ولا تطبع مخرجات تشخيصية.

يختبر المشغّل المقفل أيضًا واجهة برمجة التطبيقات المساعدة أو واجهة برمجة التطبيقات الخاصة بالفئة المصدّرة بشكل مستقل، وليس غلاف 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 = I(input[0]); string before0=Snapshot(a0);
var a1 = input[1] == "true"; string before1=Snapshot(a1);
if (new StandardPolicy().Apply(40) != 40 || new VipPolicy().Apply(100) != 90 || new Checkout(new ProbePolicy()).Total(10) != 17) throw new Exception("Policy 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 ProbePolicy : IPricePolicy { public int Apply(int price) { return price+7; } }
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس ‏C# لمطوري Luau

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