我希望能够收集多个验证实体,它们都共享相同的界面。这就是我想出的:
public interface Ivalidateable
{
bool IsValid(IValidateParam param);
}
public interface IValidateParam
{
}
public abstract EmployeeStrategy: Ivalidateable
{
public abstract bool IsValid(User user);
}
public abstract SpreadStrategy: IValidateable
{
public abstract bool IsAvlid(Campaign campaign);
}
public class User: IValidateParam
{}
public class Campaign: IValidateParam
{}
public EmployeeTypeStragtegy: EmployeeStrategy, IValidateable
{
public bool IsValid(User user)
{
if (new[]{'e',a','b}.Contains(user.userId.first().toString()))
return true;
return false;
}
}
public TrailSpreadStrategy: SpreadStrategy, IValidateable
{
public bool IsValid(Campaign campaign)
{
//logic goes here
}
}
public EvenpreadStrategy: SpreadStrategy:, IValidateable
{
public bool IsValid(Campaign campaign)
{
//logic goes here
}
}
public class ValidationFactory
{
private static List<IValidateable> stragtegies;
static ValidationFactory
{
strategies = new List<IValidateable>();
strategies.Add(new EmployeeTypeStragtegy());
strategies.Add(new TrailSpreadStrategy());
}
public bool IsValid(//Need to pass User/Campaign)
{
//what do I do here?
}
}
传递用户/活动以便我可以循环策略并检查 IsValid 的最佳方法是什么?