Thứ Tư, 26 tháng 9, 2012

Interpreter pattern

Interpreter Pattern : Người phiên dịch



Interpreter Pattern giúp người lập trình có thể "xây dựng" những đối tượng "động" bằng cách đọc mô tả về đối tượng (file XML hoặc gì đó) rồi sau đó "xây dựng" đối tượng đúng theo mô tả đó.

Metadata (ngôn ngữ mô tả) --> [Interpreter Pattern] --> Đối tượng tương ứng.


Mẫu gồm 2 thành phần chính có liên hệ với nhau là Context Expression
  • Context chính là phần chứa thông tin biểu diễn mẫu chúng ta cần xây dựng
  • Expression là phần chứa các phép logic để đưa thông tin của context thành đối tượng cụ thể


Ví dụ 1: Biểu mẫu của ngày thay đổi dựa trên từng loại thể hiện (Ví dụ dd/mm/yy hay dd/mm/yy - hh:mm:ss). Giải quyết vấn đề làm sao xuất ra biểu mẫu đúng với từng loại thể hiện

Phương pháp thông thường : Dùng các câu lệnh rẽ nhánh if .... then ... else

 if(IsWordDate)  
 {  
      formattedDate = date.DayOfYear + "th day, " +  
      date.DayOfWeek + ", " +  
      ConvertMonth(date.Month) + " " +  
      ConvertDay(date.Day) + ", " +  
      date.Year;  
 }  
 else if (IsCalendarDate)  
 {  
      formattedDate = date.Month + "-" +  
      date.Day + "-" +  
      date.Year;  
 }  
 else if (IsGregorianDate)  
 {  
      formattedDate = date.Month + "-" +  
      date.Day + "-" + date.Year + " " +  
      date.Hour + ":" + date.Minute + ":" +  
      date.Millisecond;  
 }  
   

Giải pháp : Sử dụng mẫu Interpreter và các Expression Engine để quản lý sự khác nhau giữa các mẫu ngày bằng Expression Type.

Cài đặt theo mô hình:

   
 // Context  
 Class Context  
 {  
      private string _formattedDate;  
      private DateTime _date;  
        
      public Context(DateTime dateToFormat)  
      {  
           _date = dateToFormat;  
      }  
        
      public DateTime Date  
      {  
           get{return _date;}  
      }  
        
      public string FormattedDate  
      {  
           get{return _formattedDate;}  
           set{_formattedDate = value;}  
      }  
 }  
   


 //Interpreters  
 abstract class AbstractDateExpression  
 {  
      public abstract void Interpret(Context context);  
 }  


   
 class WordDateExpression : AbstractDateExpression  
 {  
      public override void Interpret(Context context)  
      {  
           context.FormattedDate = date.DayOfYear + "th day, " +  
           date.DayOfWeek + ", " +  
           ConvertMonth(date.Month) + " " +  
           ConvertDay(date.Day) + ", " +  
           date.Year;  
      }  
 }  


   
 class CalendarDateExpression : AbstractDateExpression  
 {  
      public override void Interpret(Context context)  
      {  
           context.FormattedDate = date.Month + "-" +  
                date.Day + "-" + date.Year;  
      }  
 }  
   


   
   
 class GregorianDateExpression : AbstractDateExpression  
 {  
      public override void Interpret(Context context)  
      {  
           context.FormattedDate = date.Month + "-" +  
                date.Day + "-" + date.Year + " " +  
                date.Hour + ":" + date.Minute + ":" +  
                date.Millisecond;  
      }  
 }  

Kết quả và test thử

 AbstractDateExpression exp = new WordDateExpression();  
 exp.Interpret(context);  
   
 // ---Result   
 // Format for WordDateExpression: 202th day, Friday, July  
   
 exp = new CalendarDateExpression();  
 exp.Interpret(context);  
   
 // ---Result  
 // Formate for ClaendarDateExpression : 7-21-2006  
   
   
 exp = new GregorianDateExpression();  
 exp.Interpret(context);  
   
 // ---Result  
 // Format for GregorianDateExpression: 7-21-2006 15:47:95  

Vậy là thông qua Interpreters chúng ta đã có thể đưa ra kết quả mong muốn ứng với từng context...
Sử dụng mẫu interpreters còn có thể giúp chúng ta tạo lập các đối tượng thông qua file XML chứa metadata. Ví dụ thường gặp trong game đó là việc load các máp động, giao diện game, thay đổi hình nền game, ... thông qua các file đặc tả nằm độc lập (vì nếu tích hợp mỗi lần cập nhật map mới sẽ đẩy xuống client 1 lượng dữ liệu khổng lồ).

Không có nhận xét nào:

Đăng nhận xét