c# - Singleton implementation check -
hi interested in implementing method returns singleton object.i have created implementation based on example found on msdn not sure if implementation corect.
the code runs fine not sure how check if it's same object instance.
here code:
public class fileshareaccessfactory : ifileshareaccessfactory { private volatile static ifileshareaccess m_fileshareaccess; private static object m_syncroot = new object(); public ifileshareaccess getfileshareaccessinstance(icontextfactory contextfactory, ilogger logger) { if (m_fileshareaccess == null) { lock (m_syncroot) { if (m_fileshareaccess == null) { m_fileshareaccess = new fileshareaccess(contextfactory, logger); } } } return m_fileshareaccess; } }
as double-checked implementations go, yes - that'll work ok. doesn't need volatile
, since synchronized double-check deal small number of "it false null
read". personally, i'd more concerned fact doesn't seem respect api - i.e. if ask if instance specifying particular context-factory , logger, gives me used unrelated context-factory , logger. frankly, there ioc/di containers offload to.
Comments
Post a Comment