Programming 板


LINE

※ 引述《fourdollars (四元)》之铭言: : ※ 引述《LPH66 (IWH68S0XZ8M89)》之铭言: : : 个人以为这不是Singleton : : 因为从头到尾你的class连一个instance都没有出现过 : : 只是利用了static的共有性质达成目的 : : --- : : 以下节录英文维基: ( http://en.wikipedia.org/wiki/Singleton_pattern ) : : Note the distinction between a simple static instance of a class and a : : singleton: although a singleton can be implemented as a static instance, : : it can also be lazily constructed, requiring no memory or resources until : : needed. Another notable difference is that static member classes cannot : : implement an interface, unless that interface is simply a marker. : : 第一点是说这种class可以在要用到时才占空间 (即没用到就不占) : : 而static实作的则是一开始就占空间了 : 可是下面这行就是在初始的状态下不占空间啊... @_@a : private static Process process = null; : 另外 waitForTermination() 里面也有 process = null; 这里说的不是instance 而是这个class本身 注意它不只说不占记忆体 它是说 "requiring no memory or resources until needed" 以java来说你可以把它写成让classloader会在用到时才把它载入 而static class的话无论如何classloader会在一开始就把它载入了 (维基那页下面有个这样的范例) : : 第二点说的是用static实作的不能实作任何"有东西的"interface : : 这里"有东西的"的意思是这个interface要求要实现某些成员函式 : : 但是用static实作的却不能有任何成员函式 只能有静态(static)函式 : Java 里面的 class function 也可以当做 member function 来使用~ @_@a : SingletonProcess tmp; : tmp.runCommand("dir"); : tmp.waitForTermination(); 话说一般的Singleton不会写成这样吧... 它所谓不能实作"有东西的"interface的意思是 那些interface里定义了实作它的人该要有某些member method 而你的class里只能有static method (而这是因为你必然得要用SingletonProcess.runCommand("dir");这种方式呼叫 这使得runCommand必然得是个static method) 举个和你写的很像的东西的好了: java.lang.Math 它不是Singleton 但其他方面却和你写的东西很像: 呼叫时要用 Math.abs(value); Math.pow(a,b); 等等 而它的这些所有method都是static 当然 就如你所说它也能这样用: Math mymath; c=mymath.pow(a,b); 但这正表明了Math这个class不是一个Singleton : 还是不懂为何不能算是 Singleton ... @_@a 重新看过一次你的code之後 最根本的问题应该是: 你的class能够随时产生实体 而能够随时产生实体的class最一开始就不是Singleton了 就如同java.lang.Math的例子一样 它也不是个Singleton (维基那页开宗明义就说了 "the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object.") -- 'Oh, Harry, dont't you see?' Hermione breathed. 'If she could have done one thing to make absolutely sure that every single person in this school will read your interview, it was banning it!' ---'Harry Potter and the order of the phoenix', P513 --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.250.80 ※ 编辑: LPH66 来自: 140.112.250.80 (01/10 01:27)
1F:推 oohay:解释得好 59.112.227.50 01/10 01:50







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP