代码重构-5 取消类的私有变量(实例变量)
class WX_Service{
private readonly WinnerService _winnerService = new WinnerService();
...
}
这种方式不符合设计模式思路,耦合太厉害,而且没有用到就开始占用实例了
改进为 类工厂方法
class WX_Service{
WinnerServiceInstancePool _winnerServiceInstancePool;
WinnerServiceInstancePool winnerServiceInstancePool
{
get
{
if (_winnerServiceInstancePool== null)
_winnerServiceInstancePool = new WinnerServiceInstancePool();
return _winnerServiceInstancePool;
}
}
}
public class WinnerServiceInstancePool
{
private WinnerService _instance;
public WinnerService Instance
{
get
{
if (_instance == null)
_instance = new WinnerService();
return _instance;
}
}
}