简体   繁体   中英

Initializer using a string for DataMember returning a static object of the class (factory pattern) in C#?

My first question on StackOverflow, I feel intimidated and excited.

I tryied to have an exact behavior of for a class using static object as factory pattern and being serialized with just a type as string. When deserialized, the Initializer should return the static object based on the string.

It is easier to do over an example:

[DataContract]
public class Interpolation
{
    [DataMember]
    public string Type { get; set; }

    public static Interpolation Linear = new Interpolation(...)
}

I'd like to get a Linear interpolation thought different ways:

var interpolation = Interpolation.Linear;

var linear = new Interpolation
{
    Type = "Linear"
};

The first one is a factory pattern (kind of), the second one is used for the deserialization.

I have trying few solutions. Normally I have a generic constructor, and I am using specific parameters to create the static object. It would become:

[DataContract]
public class Interpolation
{
    [DataMember]
    public string Type
    {
        get { return _type; }
        set
        {
            _type = value;
            _interpolation = Select(value);
        }
    }

    private string _type = "Linear"; // Default
    private Func<double, double[], double[], double> _interpolation;

    private Interpolation(Func<double, double[], double[], double> interpolation, string type)        
    {
        _interpolation = interpolation;
        _type = type;
    }

    public static Interpolation Linear = new Interpolation(_linear, "Linear");

    private double _linear(double x, double[] xx, double[] yy)
    {
        ...
    }

This method won't work if there is no generic constructor (the object is too complicated to be created only from parameters). Also the static object Interpolation.Linear already exists, I don't necessarily want to recreate it.

What I would like is

var linear = new Interpolation
{
    Type = "Linear"
};

returning

Interpolation.Linear

A constructor can't return a static object of the class:

public  Interpolation(string type)        
{
    return Interpolation.Linear; // Won't work
}

Maybe by using Reflection... Thanks :)

new is intended to create a new instance. If you are trying to use it to return an existing instance, you are doing it wrong. Just stick with a (kind of) singleton

var interpolation = Interpolation.Linear;

Or use a factory like this

public static class InterpolationFactory
{
    public static Interpolation GetInterpolation(string type, Func<double, double[], double[], double> interpolation = null)
    {
        if (type == "Linear")
        {
            return Interpolation.Linear;
        }
        else
        {
            return new Interpolation(interpolation);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM