Search code examples
c#asp.netfactory-pattern

Advice on implementing Factory Type classes


I have two class that are almost completely identical. They both produce visual charts, the same visual chart in fact. One produces the chart for window, one produces the chart for the web. There is really only one difference in the classes ChartFactoryA uses

using System.Windows.Forms.DataVisualization.Charting;

While ChartFactoryB uses

using System.Web.UI.DataVisualization.Charting;

Basically the two charts take the same data and construct, then return the final chart. I would like some direction on creating one class out of the two so I can move it to my shared library.

This is the public and private interface of the class

public class ChartFactory
{
    private const int MAX_POINTS_DISPLAYED = 5;
    private const int MAX_POINTS_TRACKED = 6;

    public static Chart CreateChart(UserCourse userCourse)

    private static Chart InitilizeChart()

    private static Chart CreateEmptyChart()

    private static ChartArea CreateEmptyChartArea()

    private static void SetUpAxisY(ChartArea chartArea)

    private static void SetUpAxisX(ChartArea chartArea)

    private static Series CreateEmptyDataSeries()

    private static void AttachStripLineToChart(ChartArea chartArea)

    private static void PopulateChartWithData(Chart chart, UserCourse userCourse)

    private static ChartPoint[] InitilizeChartPoints(SessionsReadOnly sessions, int pointCount)

    private static void PopulateChartPointsWithSessions(SessionsReadOnly sessions, int pointCount, ChartPoint[] chartPoints)

    private static void DisplayChartPointsInReverse(Chart chart, ChartPoint[] chartPoints)

}


Solution

  • Use #define

    Sample (Copied from Json.Net project)

    using System;
    using System.Collections.Generic;
    #if !SILVERLIGHT && !PocketPC && !NET20
    using System.ComponentModel.DataAnnotations;
    using System.Configuration;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Web.Script.Serialization;
    #endif
    using System.Text;
    using NUnit.Framework;
    using Newtonsoft.Json;
    using System.IO;
    using System.Collections;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Collections.ObjectModel;
    using Newtonsoft.Json.Linq;
    using System.Linq;
    using Newtonsoft.Json.Converters;
    #if !PocketPC && !NET20 && !WINDOWS_PHONE
    using System.Runtime.Serialization.Json;
    #endif
    using Newtonsoft.Json.Tests.TestObjects;
    using System.Runtime.Serialization;
    using System.Globalization;
    using Newtonsoft.Json.Utilities;
    using System.Reflection;
    #if !NET20 && !SILVERLIGHT
    using System.Xml.Linq;
    using System.Text.RegularExpressions;
    using System.Collections.Specialized;
    using System.Linq.Expressions;
    #endif
    #if !(NET35 || NET20 || WINDOWS_PHONE)
    using System.Dynamic;
    using System.ComponentModel;
    #endif