无IF条件的多态对象创建

时间:2017-04-07 12:56:42

标签: c# oop

我有一个这样的抽象类:

public abstract class Records
    {
        public string Type;
        public string Source;
        public int Value;

        protected Records(string type, string source, int value)
        {
            Type = type;
            Source = source;
            Value = value;
        }
}

我想创建许多继承此类的类,并使用来自静态类的值填充其Type字段,如下所示:

public static class ContentTypesString
    {
        public static string DocumentNew { get { return "Document - New this Month"; }}

        public static string HeadlinesNew { get { return "Headlines - New this Month"; }}

       etc...  
    }

我希望能够创建这些子类而无需测试“if foo == "document" then type = ContentTypesString.DocumentNew”或等效的切换案例(我确实有很多案例)

是否有适合我需求的设计模式?

编辑:有几个人指出,我应该展示我是如何创建我的实例的。

     private delegate SPListItemCollection Query(SPWeb web, DateTime startDate, DateTime endDate);
     private readonly Query _queries;

       #region Constructors

      public QueryHandler(SPWeb web, DateTime startTimeSelectedDate, DateTime endTimeSelectedDate)
          {
            if (web == null) throw new ArgumentNullException("web");

            _web = web;
            _startTimeSelectedDate = startTimeSelectedDate;
            _endTimeSelectedDate = endTimeSelectedDate;
            RecordsList = new List<Records>();

            // Query Invocation List
            _queries = NumberPagePerMonthQuery.PreparedQuery;
            _queries += NumberDocumentsPerMonthQuery.PreparedQuery;
            _queries += NumberHeadlinesPerMonthQuery.PreparedQuery;
            _queries += NumberLeaderboxPerMonthQuery.PreparedQuery;
            _queries += NumberNewsPerMonthQuery.PreparedQuery;
            _queries += NumberPagesModifiedPerMonthQuery.PreparedQuery;
            _queries += NumberPicturesPerMonthQuery.PreparedQuery;
            _queries += NumberTeasingPerMonthQuery.PreparedQuery;
        }

        #endregion Constructors

        #region Public Methods

        // what about NullReferenceException ? C#6 : item?.Foreach(item => {}); ?
        /*** NO C#6 compiler in VS2012... ***/
        public void Queries()
        {
            foreach (var del in _queries.GetInvocationList())
            {
                var queryresult =
                    (SPListItemCollection) del.DynamicInvoke(_web, _startTimeSelectedDate, _endTimeSelectedDate);

                RecordsList.Add(new Records(del.Method.Name, _web.Title, queryresult.Count));
            }
        }

EDIT²: 我选择的解决方案

  public List<IQuery> QueryList { get; } // no delegate anymore, and static classes became implementations of IQuery interface.

       #region Constructors

      public QueryHandler(SPWeb web, DateTime startTimeSelectedDate, DateTime endTimeSelectedDate)
          {
            if (web == null) throw new ArgumentNullException("web");

            _web = web;
            _startTimeSelectedDate = startTimeSelectedDate;
            _endTimeSelectedDate = endTimeSelectedDate;
            RecordsList = new List<Records>();

            QueryList = new List<IQuery>
            {
                new NumberDocumentsPerMonthQuery(),
                new NumberHeadlinesPerMonthQuery(),
                new NumberLeaderboxPerMonthQuery(),
                new NumberNewsPerMonthQuery(),
                new NumberPagePerMonthQuery(),
                new NumberPagesModifiedPerMonthQuery(),
                new NumberPicturesPerMonthQuery(),
                new NumberTeasingPerMonthQuery()
            };

        }

        #endregion Constructors

        #region Public Methods

        // what about NullReferenceException ? C#6 : item?.Foreach(item => {}); ?
        /*** NO C#6 compiler in VS2012... ***/
          public void Queries()
        {
            foreach (var query in QueryList)
            {
                var queryresult = query.PreparedQuery(_web, _startTimeSelectedDate, _endTimeSelectedDate);
                RecordsList.Add(query.CreateRecord(_web.Title, queryresult.Count));
            }
        }

记录类遵循@dbraillon建议的实现 IQuery接口的实现添加了方法:

public Records CreateRecord(string source, int value)
        {
            return new ModifiedPagesPerMonthRecord(source, value); //or another child of Record class. 
        }

瞧瞧。谢谢大家的帮助。

2 个答案:

答案 0 :(得分:0)

这就是你需要的吗?

public abstract class Records
{
    public string Type;
    public string Source;
    public int Value;

    protected Records(string type, string source, int value)
    {
        Type = type;
        Source = source;
        Value = value;
    }
}

public class DocumentRecords : Records
{
    public DocumentRecords(string source, int value)
        : base(ContentTypesString.DocumentNew, source, value) // use here
    {
    }
}

public class HeadlinesRecords : Records
{
    public HeadlinesRecords(string source, int value)
        : base(ContentTypesString.HeadlinesNew, source, value) // use here
    {
    }
}

public static class ContentTypesString
{
    public static string DocumentNew { get { return "Document - New this Month"; } }

    public static string HeadlinesNew { get { return "Headlines - New this Month"; } }
}

答案 1 :(得分:0)

您希望通过对象类型的字符串代码和参数来创建记录集合。

执行此操作的众多方法之一 - 使用构建器。

首先,我们需要配置构建器:

        var builder = new RecordBuilder()
            .RegisterBuilder("document", (source, value) => new Document(source, value))
            .RegisterBuilder("headlines", (source, value) => new Headlines(source, value));

这里我们指定如何用代码构建记录&#34;文档&#34;和#34;标题&#34;。

建立记录通话:

        builder.Build("document", "source", 1);

构建器代码可以是这样的 (这里我们看看我们是否知道如何构建传递类型的记录并制作它):

public class RecordBuilder
{
    public Records Build(string code, string source, int value)
    {
        Func<string, int, Records> buildAction;

        if (recordBuilders.TryGetValue(code, out buildAction))
        {
            return buildAction(source, value);
        }

        return null;
    }

    public RecordBuilder RegisterBuilder(string code, Func<string, int, Records> buildAction)
    {
        recordBuilders.Add(code, buildAction);
        return this;
    }

    private Dictionary<string, Func<string, int, Records>> recordBuilders = new Dictionary<string, Func<string, int, Records>> ();
}


public class Document : Records
{
    public Document(string source, int value) : base(ContentTypesString.DocumentNew, source, value)
    {
    }
}

public class Headlines : Records
{
    public Headlines(string source, int value) : base(ContentTypesString.HeadlinesNew, source, value)
    {
    }
}