请问逻辑层的类方法为什么要写成静态的啊? 如果单从static 方法与单例模式上做区分,他们应算没有什么区别 只不过单列模式要多敲几个XXXInstance字符罢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 单例模式的好处仅仅是"延迟加载",而不是初始化的时候就加载而且即使是单例模式,也有把私有属性设置成static的方式而且函数本来就不需要做成实例的,除非函数里还要用到非静态的属性 private ProductBLL productBll;private ProductBll PorductBllInstance{ get{ if( this.productBll==null ){ this.productBll=new ProductBll(); } return this.productBll;} }而且这种写法根本不是单例因为每实例化一次,productBll都是不同的对象,它绝对是null,这样写没有任何意义的 这样写法不是单例的问题,是全局访问,因为有很多人喜欢一种写法不管用不用,在字段部分直接先实例化,有时候你就会发现类顶部new了十几个bll,但你要用的方法其实根本用不到这些private ProductBLL productBll = new ProductBll(); 而这种写法其实就相当于Lazy<T>但就个人而言,不喜欢这种写法,个人一直喜欢在用的地方才实例 单例模式延迟加载:static private ProductBLL productBll;private ProductBll PorductBllInstance{ get{ if( this.productBll==null ){ this.productBll=new ProductBll(); } return this.productBll;} }单例模式初始加载:private ProductBLL productBll=new ProductBll();private ProductBll PorductBllInstance{ get{ return this.productBll;} } 晕死,错了单例模式初始化加载:static private ProductBLL productBll=new ProductBll();private ProductBll PorductBllInstance{ get{ return this.productBll;} }如果不静态,就根本不可能是单例 c#如何创建一个窗口但不打开。。。。。。。。。求助中 update c#调用delphi 的dll问题 关于backgroundwoker暂停/继续的问题. DataGridView 出现大红叉问题 问一个奇怪的问题?能不能获得任务栏中打开的文件阿? 大虾求救:关于sql server2000 和C#的问题! C# 获取打印机状态 从数据库提取数据的问题 听君一席话,胜读十年书!有请大虾指点. C#如何执行多句SQL语句组成的脚本 在WINFORM中嵌入word程序
而且即使是单例模式,也有把私有属性设置成static的方式
而且函数本来就不需要做成实例的,除非函数里还要用到非静态的属性
private ProductBLL productBll;
private ProductBll PorductBllInstance{
get{
if( this.productBll==null ){ this.productBll=new ProductBll(); }
return this.productBll;
}
}而且这种写法根本不是单例
因为每实例化一次,productBll都是不同的对象,它绝对是null,这样写没有任何意义的
不管用不用,在字段部分直接先实例化,有时候你就会发现类顶部new了十几个bll,但你要用的方法其实根本用不到这些
private ProductBLL productBll = new ProductBll();
而这种写法其实就相当于Lazy<T>但就个人而言,不喜欢这种写法,个人一直喜欢在用的地方才实例
static private ProductBLL productBll;
private ProductBll PorductBllInstance{
get{
if( this.productBll==null ){ this.productBll=new ProductBll(); }
return this.productBll;
}
}
单例模式初始加载:private ProductBLL productBll=new ProductBll();
private ProductBll PorductBllInstance{
get{
return this.productBll;
}
}
单例模式初始化加载:
static private ProductBLL productBll=new ProductBll();
private ProductBll PorductBllInstance{
get{
return this.productBll;
}
}
如果不静态,就根本不可能是单例