我们有3个班负责处理图像。老板让我把它合并。 我把它合并到一个文件中,但是他说同样的事情,我们需要将它合并到一般逻辑中。 我们如何将这3个吸气剂合并为一般? 它们仅在传递给Type.GetType()的参数类型上有所不同。 我们需要使用一个通用属性和一个通用的getter。
public class BonusImageHandler
{
private static IStorageProvider _BAstorageProvider;
private static IStorageProvider _BAWP8storageProvider;
private static IStorageProvider _M4MstorageProvider;
private static readonly object _lock = new object();
private static IStorageProvider BAStorageProvider
{
get
{
lock (_lock)
{
if (_BAstorageProvider == null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName);
_BAstorageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _BAstorageProvider;
}
}
private static IStorageProvider BAWP8StorageProvider
{
get
{
lock (_lock)
{
if (_BAWP8storageProvider == null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName);
_BAWP8storageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _BAWP8storageProvider;
}
}
private static IStorageProvider M4MStorageProvider
{
get
{
lock (_lock)
{
if (_M4MstorageProvider== null)
{
lock (_lock)
{
var storageProviderType = Type.GetType(BLL.Providers.ConfigurationProvider.Instance.M4MStorageProviderTypeName);
_M4MstorageProvider= (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
return _M4MstorageProvider;
}
}
}
答案 0 :(得分:2)
这样的方法:
public void GetProvider(string typeName, ref IStorageProvider provider)
{
if (provider == null)
{
lock (_lock)
{
if (provider == null)
{
var storageProviderType = Type.GetType(typeName);
provider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
}
}
应该做的伎俩。然后你这样称呼它:
private static IStorageProvider BAWP8StorageProvider
{
get
{
GetProvider(BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName, ref _BAWP8storageProvider);
return _BAWP8StorageProvider;
}
}
虽然我个人会失去_BAWP8StorageProvider类成员,而只是使用属性(这也会删除丑陋的ref
参数)。
答案 1 :(得分:2)
帮助方法:
static IStorageProvider GetStorageProvider(ref IStorageProvider field, string name)
{
lock(_lock)
{
if(field == null)
{
var storageProviderType = Type.GetType(name);
field = (IStorageProvider)Activator.CreateInstance(storageProviderType);
}
}
return field;
}
用法:
private static IStorageProvider BAStorageProvider
{
get
{
return GetStorageProvider(
ref _BAstorageProvider,
BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName);
}
}
答案 2 :(得分:2)
private static IStorageProvider GetProvider(string typeName)
{
return (IStorageProvider)Activator.CreateInstance(Type.GetType(typeName));
}
private static readonly Lazy<IStorageProvider> _BAstorageProvider =
new Lazy<IStorageProvider>(
() => GetProvider(
BLL.Providers.ConfigurationProvider.Instance.BAStorageProviderTypeName),
LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly Lazy<IStorageProvider> _BAWP8storageProvider =
new Lazy<IStorageProvider>(
() => GetProvider(
BLL.Providers.ConfigurationProvider.Instance.BAWP8StorageProviderTypeName),
LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly Lazy<IStorageProvider> _M4MstorageProvider =
new Lazy<IStorageProvider>(
() => GetProvider(
BLL.Providers.ConfigurationProvider.Instance.M4MStorageProviderTypeName),
LazyThreadSafetyMode.ExecutionAndPublication);
private static IStorageProvider BAStorageProvider
{
get
{
return _BAstorageProvider.Value;
}
}
private static IStorageProvider BAWP8StorageProvider
{
get
{
return _BAWP8storageProvider.Value;
}
}
private static IStorageProvider M4MStorageProvider
{
get
{
return _M4MStorageProvider.Value;
}
}
(但我建议使用Dependency Container)