- if else
- switch case
بله مورد اولی که به ذهن خود من رسید، استفاده از if else هست. شاید خروجی مناسبی را از نظر کدنویسی داشته باشد؛ ولی خوانایی مناسبی را ندارد. حالا چطور اثبات کنیم خوانایی و قابلیت توسعهی پایینی را دارد؟
فرض کنید شما برنامه را نوشتهاید و تحویل مدیر خود دادهاید. بعد از دو ماه به شما گفته میشود که مراحل 1 و 2 را جابجا کنید و یا یک step را اضافه کنید که بعد از مرحله دو (بررسی رمز) است تا یک منطق جدید را دنبال کند. اینجاست که دچار دردسر و اتلاف زمان میشویم؛ چون باید بیزینس را مجددا review کنیم و بدتر از آن کدها را هم تغییر دهیم که امکان رخ دادن خطا به شدت بالا میرود.
- انجام کار در چند مرحله
- حذف پیچیدگیهای پیاده سازی
حالا بیایید با هم با الگوی Chain Of Responsibility، این مثال را پیاده سازی کنیم. منطق کار به صورت زیر است:
به این شکل که مراحل بصورت سلسله مراتبی، تحت successorهای یکدیگر پیش میروند. اگر بخواهم successor را در این مثال توضیح دهم من بهعنوان دانشجو (successor اول) بعد از چک شدن موارد مربوط به دانشجو، درخواست به سمت مسئول مربوطه رفته (successor دوم ) و الی اخر.
public class Customer { public string Password { get; set; } public string Stno { get; set; } public int value { get; set; } public bool Active { get; set; } }
public class RequestContext { public int VamValue { get; set; } public Customer student { get; set; } }
public class ResponseContext { public string Response { get; set; } }
حال طبق شکل بالا باید handler خود را که گرفتن وام است، پیاده سازی نماییم:
public abstract class GetVam { protected readonly GetVam successor; public GetVam(GetVam _getVam) { this.successor = _getVam; } public abstract ResponseContext execute(RequestContext requestContext); }
public class CheckUseractive : GetVam { public CheckUseractive(GetVam _getVam) : base(_getVam) { } public override ResponseContext execute(RequestContext requestContext) { if (requestContext.student.Active == true) { return successor.execute(requestContext); } else { return new ResponseContext { Response = "student is inactive" }; } } }
2-بررسی رمز کاربر :
public class ChechPassword : GetVam { public ChechPassword(GetVam _getVam) : base(_getVam) { } public override ResponseContext execute(RequestContext requestContext) { if (requestContext.student.Password == "123") { return successor.execute(requestContext); } else { return new ResponseContext { Response = "invalid pass", }; } } }
3-بررسی میزان بدهکاری دانشجو :
public class ChechUserBedehkar : GetVam { public ChechUserBedehkar(GetVam _getVam) : base(_getVam) { } public override ResponseContext execute(RequestContext requestContext) { if (requestContext.student.value < requestContext.VamValue) { return successor.execute(requestContext); } else { return new ResponseContext { Response = "you are dont permission" }; } } }
4-و مرحله آخر که در صورتیکه تمامی مراحل قبلی پاس شوند چک کردن مقدار وامی است که به دانشجو باید داده شود :
public class AssignVam : GetVam { public AssignVam(GetVam _getVam) : base(_getVam) { } public override ResponseContext execute(RequestContext requestContext) { return new ResponseContext { Response = "value of vam: " + (requestContext.VamValue - requestContext.student.value).ToString(); }; } }
partial class Program { static void Main(string[] args) { Customer customer = new Customer() { Active = true, Password = "123", Stno = "111", value = 2000 }; RequestContext requestContext = new RequestContext() { student = customer, VamValue = 3000, }; var GetVam = new CheckUseractive(new ChechPassword(new ChechUserBedehkar(new AssignVam(null)))); var res = GetVam.execute(requestContext); Console.Write(res.Response); Console.ReadKey(); } }