AbstractMessageTemplate
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.TemplateMethod
{
/**//// <summary>
/// 模板方法抽象類
/// </summary>
public abstract class AbstractMessageTemplate
{
/**//// <summary>
/// UserId
/// </summary>
protected string _userId;
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="userId">UserId</param>
public AbstractMessageTemplate(string userId)
{
this._userId = userId;
}
/**//// <summary>
/// 驗證用戶是否合法
/// </summary>
/// <returns></returns>
public virtual bool ValidateUser()
{
if (this._userId == "admin")
{
return true;
}
else
{
return false;
}
}
/**//// <summary>
/// 獲取MessageModel列表
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get();
/**//// <summary>
/// 模板方法:先驗證用戶是否合法,再獲取MessageModel列表
/// </summary>
/// <returns></returns>
public List<MessageModel> TemplateMethodGet()
{
if (ValidateUser())
{
return Get();
}
else
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("無權獲取", DateTime.Now));
return l;
}
}
}
}