मुझे सिंगलटन पैटर्न और एमईएफ के बारे में एक प्रश्न है। मैं एमईएफ प्लगइन्स को लागू करने में नया हूं और मुझे कोई जवाब नहीं मिला है।सी # सिंगलटन पैटर्न और एमईएफ
क्या एमईएफ लागू प्लगइन के माध्यम से कक्षा का केवल एक उदाहरण प्रदान करना संभव है?
#region Singleton
///
/// This class provide a generic and thread-safe interface for Singleton classes.
///
/// The specialized singleton which is derived
/// from SingletonBase<T>
public abstract class Base where T : Base
{
/* the lock object */
private static object _lock = new object();
/* the static instance */
private static T _instance = null;
///
/// Get the unique instance of .
/// This property is thread-safe!
///
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
/* Create a object without to use new (where you need a public ctor) */
object obj = FormatterServices.GetUninitializedObject(typeof(T));
if (obj != null) // just 4 safety, but i think obj == null shouldn't be possible
{
/* an extra test of the correct type is redundant,
* because we have an uninitialised object of type == typeof(T) */
_instance = obj as T;
_instance.Init(); // now the singleton will be initialized
}
}
}
}
else
{
_instance.Refresh(); // has only effect if overridden in sub class
}
return _instance;
}
}
///
/// Called while instantiation of singleton sub-class.
/// This could be used to set some default stuff in the singleton.
///
protected virtual void Init()
{ }
///
/// If overridden this will called on every request of the Instance but
/// the instance was already created. Refresh will not called during
/// the first instantiation, for this will call Init.
///
protected virtual void Refresh()
{ }
}
#endregion
#region class
public class xy : Base
{
private bool run;
public xy()
{
this.run = false;
}
public bool isRunning()
{
return this.run;
}
public void start()
{
// Do some stuff
this.run = true;
}
}
#endregion
किसी ने मुझे एक उदाहरण प्रदान कर सकते हैं:
मेरा पुराना वर्ग कुछ इस तरह है?
संभावित डुप्लिकेट [क्या एमईएफ सिंगलटन पैटर्न के लिए कोई मूल्य देता है?] (Http://stackoverflow.com/questions/4484619/does-mef-lend-any-value-to-the-singleton-pattern) – IAbstract
क्षमा करें, प्रश्न से संबंधित नहीं है, लेकिन ऐसा लगता है कि आप अनावश्यक रूप से 'Lazy' को पुन: कार्यान्वित कर रहे हैं। कोड की बाहरी रेखाएं! –
binki