Polymorphic object creation without IF condition

2019-09-21 04:46发布

问题:

I have an abstract class like this:

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 inheriting this class, and filling their Type field with a value coming from a 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 those child classes without having a test "if foo == "document" then type = ContentTypesString.DocumentNew" or an equivalent switch case (I really have a lot of cases)

Is there a design pattern that suits my needs?

EDIT : As several people pointed out, i should 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² : The 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));
            }
        }

Record class follow the implementation suggested by @dbraillon Implementation of IQuery interface were added the method :

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

And voilà. Thank you all for the help.

回答1:

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

One of many way to do it - use builder.

Firstly we need to configurate builder:

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

here we specify how to build record with code "document" and "headlines".

To build a record call:

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

Builder code can by something like this (here we look if we know how to build record of the passed type and make 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)
    {
    }
}


回答2:

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


标签: c# oop