Nawzir Kadiwal Nawzir Kadiwal - 1 month ago 13
ASP.NET (C#) Question

How to implement singleton pattern in subclass

I am practicing .net in visual studio 2010. I am making an application for bank accounts with sub types and AccountState with subtype i.e.:

Bronze State
,
Gold State
,
Silver State
,
Platinum State
. Each account type are differed by upper limit and lower limit.

Public class AccountState
{
public double upperlimit{get;set}
public double lowerlimit{get;set}
}
public class BronzeState : AccountState
{
private static BronzeState bronzeState;
private BronzeState(){}
puGetInstance()
{
if (bronzeState != null)
return bronzeState;
else
{
bronzeState = new BronzeState();
return bronzeState;
}
}
//Same goes for GoldState, SilverState, PlatinumState


How can i set AutoImplemented properties upperlimit and lower limit for all Account State and keeping Singleton in mind

Answer

You have the singleton implementation correct. Now that you have that, the rest is very much like regular inheritance. Remember that you can reference inherited methods, properties and fields using the "this" operator.

Following closely with your existing code, to set the values of the inherited properties from the sub-class, you need to, for example:

this.upperlimit = 5000;

The question then is where you should set the limit in the sub-class. The constructor would be a likely place to ensure that the upper limit is guaranteed to be set before use. This would look like:

private BronzeState()
{
    this.upperlimit = 5000;
}

One thing I will note, although it isn't directly answering the question, is you should consider who you want to be able to change the limits of an AccountState. Right now, your limits can be seen and set by any other class. It seems like you would only want them changeable by the sub-classes.

To do this, you will want to use a protected setter, like so:

public double upperlimit { get; protected set; }

That ensures that the value can be seen by anyone, but can only be set by the parent class or sub-classes.

The full picture would then be:

public class AccountState
{
    public double UpperLimit { get; protected set; }
    public double LowerLimit { get; protected set; }
}

public class BronzeState : AccountState
{
    private BronzeState()
    {
        this.UpperLimit = 5000;
        this.LowerLimit = 1000;
    }

    public static BronzeState GetInstance()
    {
        ...
    }
}

Without knowing too much about the specifics of your scenario, I would also consider using abstract properties in this scenario. For example:

public abstract class AccountState
{
    public abstract double UpperLimit { get; }
    public abstract double LowerLimit { get; }
}

public class BronzeState : AccountState
{
    public override double UpperLimit
    {
        get { return 5000; }
    }

    public override double LowerLimit
    {
        get { return 1000; }
    }

    public static BronzeState GetInstance()
    {
        ...
    }
}

This design has the advantage of forcing the sub-class to define the upper and lower limits, as well as encourages them not to change, if that is so desirable. Choosing which design is better would depend on the broader scenario this is fitting into.


In either of the examples above, to access the values from outside the class would look like:

double limit = BronzeState.GetInstance().UpperLimit;

You can read more about inheritance in C# (including the things I mentioned above) on MSDN.

Comments