Create polymorphic object without IF clause

I have an abstract class:

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;
        }
}

      

I would like to create many classes that inherit from this class and fill their Type field with the value coming from the static class like this:

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...  
    }

      

I would like to be able to create these child classes without test " if foo == "document" then type = ContentTypesString.DocumentNew

" or equivalent switch code (I do have many cases indeed)

Is there a design template that suits my needs?

EDIT: As several people pointed out, I have to show how I create my instances.

     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² : Solution I chose

  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));
            }
        }

      

The writer class follows the implementation suggested by @dbraillon To implement the IQuery interface, a method has been added:

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

      

And voila. Thanks everyone for the help.

-2


source to share


2 answers


You want to make the collection of records, object type string code and parameters.

One of the many ways to do this is to use a builder.

First, we need to configure the builder:

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

      

here we will show you how to create an entry with the code "document" and "headers".



To create a recording call:

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

      

The Builder code could be something like this (here we are looking to see if we know how to create a past type record and do it):

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)
    {
    }
}

      

0


source


This is what you need?



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"; } }
}

      

0


source







All Articles