+-
c# – 实现策略模式而不是几个if语句
我有很多if语句的方法,其中我根据员工位置过滤SharePoint列表.结果是查询字符串,它作为参数传递给另一个方法QuerySPList.

 public List<Phone> GetListOfPhones(Employee emp)
 {
    List<Phone> records = new List<Phone>();
    string query = string.Empty;

    if (emp.Positions.Count == 1 && emp.Positions[0] == "Regular")
    {
       query = "";// some querystring                              
    }


    if (emp.Positions.Count == 1 && emp.Positions[0] == "OfficeDirector")
    {   
       query = "";// some querystring    
    }

    if (emp.Positions.Count == 1 && emp.Positions[0] == "Admin")
    {
        query = "";// some querystring 
    }              

    if (emp.Positions.Count == 2 && emp.Positions.Contains("Regular") && emp.Positions.Contains("OfficeDirector"))
    {

      query = "";// some querystring 

     }

   var rawItems = QuerySPList(query);

   foreach (SPListItem item in rawItems)
   {
        //business logic 
   }
   return records;
}}

我已经读过,通过实现策略模式我们可以避免使用很多if的乱码,但是,我是开发和设计模式的新手,所以我需要一点帮助,所以如果有人可以指导我应该怎么做做并给我建议,随时回答我的问题.我有想法,但我认为我走错了方向.
想法是创建接口IRoleHandler,而不是将其实现为3个类,例如RoleAdmin,RoleRegular,RoleOfficeDirector.这样的东西:

public interface IRoleHandler<T>
{
    string handleRole(T obj);
}
public class RoleAdmin:IRoleHandler<Employee>
{

    public string handleRole(Employee emp)
    {
        if (emp.Positions.Count == 1 && emp.Positions[0] == "Admin")
        {
            //return some string query
        }

    }
}

然后想法是创建字典,如下所示:

Dictionary<string, IRoleHandler<Employee>> strategyHandlers = new Dictionary<string, IRoleHandler<Employee>>();
        strategyHandlers.Add("Admin", new RoleAdmin());
        strategyHandlers.Add("Regular", new RoleRegularUser());
最佳答案
首先,我认为您需要更少关注战略模式的“内部结构”,而更关心其设计目标/用途.

https://en.wikipedia.org/wiki/Strategy_pattern

the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm’s behavior to be selected at runtime. The strategy pattern

defines a family of algorithms, encapsulates each algorithm, and makes the algorithms interchangeable within that family.

对我来说,看起来你实际上打算根据一些输入值“创建”一个字符串.

因此,您可能希望使用Creational patterns中的一个

我的建议可能是Factory Pattern,或者可能是Builder模式. (两者都在创作模式链接中链接)

private string BuildQueryByEmployee(Employee emp) {
    // create your builder
    EmployeeQueryBuilder mBuilder = new EmployeeQueryBuilder ();
    // add # of positions
    mBuilder.addPositionCount(emp.Positions.Count);
    // add each position
    for (int i =0 ; i < emp.Positions.Count; i++ ){
        mBuilder.addPosition(emp.Positions[i]);
    }
    // 'build' your query. 
    // and return the query as a string.
    return mBuilder.buildQuery(); 
}

然后在EmployeeQueryBuilder类中,您可以处理基于位置数以及它们是什么来构建查询的复杂性.

其次,以下是一些可能对您有用的链接.

> https://en.wikipedia.org/wiki/Software_design_pattern(一般光盘模式)
> https://en.wikipedia.org/wiki/Design_Patterns(“四人帮”一书,基本上是关于设计模式的第一本书,值得阅读几次,并且手动编写每个模式几次,直到你对它们感到满意为止,仅此一项就可以提高你的编码能力如果您以前没有使用过模式,则需要2~3倍,本书中的示例有些过时,因为它的激励因素是编写文本编辑器……但仍然是“经典”,非常值得了解)
> http://www.cs.wustl.edu/~schmidt/POSA/POSA2/(关于必须处理并发的模式的第一本书之一)(POSA =面向模式的软件架构)(现在可能有点过头了,但总的来说’很好知道’

点击查看更多相关文章

转载注明原文:c# – 实现策略模式而不是几个if语句 - 乐贴网