如何通过将不同的参数传递给构造函数来允许实例化不同的单例类

我的项目中有一个类,我只想通过构造函数传递特定参数来实例化它一次,但是当我传递不同的参数时,它应该实例化一个新的参数。我如何使用单例设计模式实现这一目标?或者,如果单例无法实现,您能否建议另一种设计模式?


class Program

{

static void Main()

{

    SiteStructure s = SiteStructure.Instance;

}

}


public sealed class SiteStructure

{

static readonly SiteStructure _instance = new SiteStructure();

public static SiteStructure Instance

{

    get

    {

        return _instance;

    }

}

SiteStructure()

{

    // Initialize.

}

}


catspeake
浏览 253回答 2
2回答

皈依舞

您必须修改_instance变量的初始化方式,使用接受您想要传入的参数值的函数。此外,_instance变量不能再readonly像它需要在新函数内部初始化一样。[TestMethod]public void CreateSingletonInstance(){&nbsp; &nbsp; SiteStructure s = SiteStructure.GetInstance("Abc123");&nbsp; &nbsp; Debug.Print(s.Parameter); // outputs Abc123&nbsp; &nbsp; SiteStructure s2 = SiteStructure.GetInstance("Is it really a singleton?");&nbsp; &nbsp; Debug.Print(s2.Parameter); // outputs Is it really a singleton?&nbsp; &nbsp; SiteStructure s3 = SiteStructure.GetInstance("Abc123");&nbsp; &nbsp; Debug.Print(s3.Parameter); // outputs Abc123&nbsp; &nbsp; Assert.AreNotEqual(s, s2); // Check to make sure they are different instances&nbsp; &nbsp; Assert.AreEqual(s, s3); // Check to make sure they are the same instance}public sealed class SiteStructure{&nbsp; &nbsp; static Dictionary<string, SiteStructure> _siteStructures = new Dictionary<string, SiteStructure>();&nbsp; &nbsp; static object _instance_Lock = new object();&nbsp; &nbsp; public static SiteStructure GetInstance(string parameter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!_siteStructures.ContainsKey(parameter))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lock (_instance_Lock)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!_siteStructures.ContainsKey(parameter))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _siteStructures.Add(parameter, new SiteStructure(parameter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return _siteStructures[parameter];&nbsp; &nbsp; }&nbsp; &nbsp; private SiteStructure(string parameter)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Initialize.&nbsp; &nbsp; &nbsp; &nbsp; Parameter = parameter;&nbsp; &nbsp; }&nbsp; &nbsp; public string Parameter { get; set; }}

慕的地10843

using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;namespace Rextester{&nbsp; &nbsp; public class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var g = SiteStructure.Instance(4);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public sealed class SiteStructure {&nbsp;&nbsp;public static SiteStructure Instance()&nbsp;&nbsp;{ return new SiteStructure();&nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; public static SiteStructure Instance (int x)&nbsp; &nbsp; &nbsp; &nbsp; { return new SiteStructure (x);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;SiteStructure() { }SiteStructure(int x) { Console.WriteLine("Hello"); }}}
打开App,查看更多内容
随时随地看视频慕课网APP