如何通过key获取value值,键找值!多线程单利模式 3月15日第三版
封装不是很好,希望各位路过的高手批评指正 给出更优秀的代码方便后来人
模拟微软风格的写法,语义极其清晰,上代码
public void Test()
{
ProcessDesignStatusEnum status = new ProcessDesignStatusEnum();
string value = status.流程设计.未提交.value;
string key = status.流程设计.未提交.key;
string getValue = status.ConvertKeyToValue.DefectsLevelValue(1);
}
上面的这是使用
如何定义直接看下面的代码
namespace Bll
{
public class BaseStatus
{
static string[,] arr = new string[5, 2];
static string[,] DefectArr2 = new string[4, 2];
static string[,] DefectArr3 = new string[3, 2];
static BaseStatus()
{
//which 1
arr[0, 0] = "未提交";
arr[0, 1] = "1";
arr[1, 0] = "待确认";
arr[1, 1] = "2";
arr[2, 0] = "已确认";
arr[2, 1] = "3";
arr[3, 0] = "待部署";
arr[3, 1] = "4";
arr[4, 0] = "已部署";
arr[4, 1] = "5";
//which 2
DefectArr2[0, 0] = "设计缺陷";
DefectArr2[0, 1] = "1";
DefectArr2[1, 0] = "执行缺陷";
DefectArr2[1, 1] = "2";
DefectArr2[2, 0] = "内部变化";
DefectArr2[2, 1] = "3";
DefectArr2[3, 0] = "外部变化";
DefectArr2[3, 1] = "4";
//which 3
DefectArr3[0, 0] = "一般";
DefectArr3[0, 1] = "1";
DefectArr3[1, 0] = "重要";
DefectArr3[1, 1] = "2";
DefectArr3[2, 0] = "重大";
DefectArr3[2, 1] = "3";
}
public BaseStatus()
{
}
public BaseStatus(int which, int index1D)
{
string[,] array;
switch (which)
{
case 1:
array = arr;
break;
case 2:
array = DefectArr2;
break;
case 3:
array = DefectArr3;
break;
default:
array = null;
break;
}
value = array[index1D,0];
key = array[index1D, 1];
}
/// <summary>
/// 状态名
/// </summary>
public string value
{
get;set;
}
/// <summary>
/// 状态名对应的数字状态
/// </summary>
public string key
{
get;
set;
}
internal string ReturnProcessValue(object key)
{
return ReturnValue(key, 1);
}
internal string ReturnDefectsTypeValue(object key)
{
return ReturnValue(key, 2);
}
internal string ReturnDefectsLevelValue(object key)
{
return ReturnValue(key, 3);
}
internal string ReturnValue(object key, int which)
{
string[,] array;
switch (which)
{
case 1:
array = arr;
break;
case 2:
array = DefectArr2;
break;
case 3:
array = DefectArr3;
break;
default:
array = null;
break;
}
int k = 0;
if (int.TryParse(key.ToString(), out k))
{
}
if (k > array.Length/2 || k < 1)
{
return "错误的状态";
}
return array[k - 1, 0];
}
}
public class MethodClass
{
private MethodClass(){ }
public class 流程设计: BaseStatus
{
private static 流程设计 instance;
private static object _lock = new object();
private 流程设计()
{
}
public static 流程设计 GetInstance()
{
if (instance == null)
{
lock (_lock)
{
if (instance == null)
{
instance = new 流程设计();
}
}
}
return instance;
}
public BaseStatus 未提交
{
get { return new BaseStatus(1,0); }
}
public BaseStatus 待确认
{
get { return new BaseStatus(1, 1); }
}
public BaseStatus 已确认
{
get { return new BaseStatus(1, 2); }
}
public BaseStatus 待部署
{
get { return new BaseStatus(1, 3); }
}
public BaseStatus 已部署
{
get { return new BaseStatus(1, 4); }
}
}
public class 流程改善类型 : BaseStatus
{
private static 流程改善类型 instance;
private static object _lock = new object();
private 流程改善类型()
{
}
public static 流程改善类型 GetInstance()
{
if (instance == null)
{
lock (_lock)
{
if (instance == null)
{
instance = new 流程改善类型();
}
}
}
return instance;
}
public BaseStatus 设计缺陷
{
get { return new BaseStatus(2, 0); }
}
public BaseStatus 执行缺陷
{
get { return new BaseStatus(2, 1); }
}
public BaseStatus 内部变化
{
get { return new BaseStatus(2, 2); }
}
public BaseStatus 外部变化
{
get { return new BaseStatus(2, 3); }
}
}
public class 流程改善等级 : BaseStatus
{
private static 流程改善等级 instance;
private static object _lock = new object();
private 流程改善等级()
{
}
public static 流程改善等级 GetInstance()
{
if (instance == null)
{
lock (_lock)
{
if (instance == null)
{
instance = new 流程改善等级();
}
}
}
return instance;
}
public BaseStatus 一般
{
get { return new BaseStatus(3, 0); }
}
public BaseStatus 重要
{
get { return new BaseStatus(3, 1); }
}
public BaseStatus 重大
{
get { return new BaseStatus(3, 2); }
}
}
public class ConvertKeyToValue : BaseStatus
{
private static ConvertKeyToValue instance;
private static object _lock = new object();
private ConvertKeyToValue() { }
public static ConvertKeyToValue GetInstance()
{
if (instance == null)
{
lock (_lock)
{
if (instance == null)
{
instance = new ConvertKeyToValue();
}
}
}
return instance;
}
public string ProcessValue(object key)
{
return base.ReturnProcessValue(key);
}
public string DefectsTypeValue(object key)
{
return base.ReturnDefectsTypeValue(key);
}
public string DefectsLevelValue(object key)
{
return base.ReturnDefectsLevelValue(key);
}
}
}
public class ProcessDesignStatusEnum
{
public MethodClass.流程设计 流程设计
{
get { return MethodClass.流程设计.GetInstance(); }
}
public MethodClass.流程改善类型 流程改善类型
{
get { return MethodClass.流程改善类型.GetInstance(); }
}
public MethodClass.流程改善等级 流程改善等级
{
get { return MethodClass.流程改善等级.GetInstance(); }
}
public MethodClass.ConvertKeyToValue ConvertKeyToValue
{
get { return MethodClass.ConvertKeyToValue.GetInstance(); }
}
}
}