Menu
CoddyTech

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

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

تُعرّف أصناف Dart الواجهات ضمنيًا، وتُعلن implements عن عقد. أما واجهات C# فتُعلَن بشكل منفصل. يدعم كلاهما حقن سياسة؛ ويجب أن يستدعي إتمام الشراء النسخة المقدَّمة لكي تعمل التطبيقات البديلة.

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

مرجع Dart

abstract class Policy { int apply(int price); }
class Standard implements Policy { int apply(int price)=>price; }
class Checkout { final Policy policy; Checkout(this.policy); int total(int price)=>policy.apply(price); }
void main() {
print(Checkout(Standard()).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 في ملفاتها الخاصة. تُعيد السياسة القياسية السعر؛ وتُعيد سياسة VIP ‏90 بالمئة باستخدام الحساب الصحيح. الأسعار غير سالبة ومضاعفات العشرة. يجب أن يفوّض Checkout إلى IPricePolicy المُمرَّرة إليه، بما في ذلك السياسات المخصصة التي يوفّرها المستدعون.

عدّل ملفات 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 = 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# لمطوري Dart

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