IT界討論區(40) 放咗工吹咩IT呀, 我想打機呀
i-vtec 2018-8-30 23:37:53 唔好比T巴誤導
佢由頭到尾想證實static同singleton有關係
static 公廁,server層面全世界都用得 會出錯value

Ads

蒼月A 2018-8-30 23:38:29 我都覺得係

通常Singleton都係一個Single Thread Program又要access一啲共通data先會整
例如Unity3D入面咁

我Application有時都係咁整

就算Multithread入面行Singleton都可以做返個Thread Safe行法
:^(
i-vtec 2018-8-30 23:40:54 T巴個例子都係低質
個i走左入去個static method
之後變左第2個數

正常唔應該咁做
蒼月A 2018-8-30 23:41:53 我覺得呢個未必關return new事
不如話每次pass入去都開個新ref會唔會好啲?

但int反而要知
非典型文青 2018-8-30 23:41:58 岩岩先返到屋企
:^(
:^(

聽日再睇
leucocephalus 2018-8-30 23:42:59 Static 同singletion 兩樣野黎。Load time已經可以有分別。
Trinidad 2018-8-30 23:43:38 sor我1999
:^(


我個問題就係不論係singleton咁factory定公廁static
好似我頭先咁嘅concurrent call function 會唔會出事

可能係偏向thread safe問題
因為我對.net個thread pool了解唔深
唔知咁會唔會有事定點先會有事
蒼月A 2018-8-30 23:44:44
public static bool operation (List<String> c) {
 return c == null;
}

第二個class
bool result = StaticClass.operation(myList.ToList());

應該咁行會冇事?
雖然一個method入面係完全唔應該要求出面做啲咩先係好method
:^(


已經完全唔關Singleton事
i-vtec 2018-8-30 23:46:30 多過一個人會
除非第一個Logout走左/DOM左個object
否則第二個人用第一個條有血M巾著上身

用thread safe仲死多兩錢重
油尖旺豬肉佬 2018-8-30 23:46:39 新貼留名,上個POST真係好撚精彩
:^(
leucocephalus 2018-8-30 23:46:56 以spring為例:Singletion object 如果好似一個helper object咁for handle 唔同user http request, 而個singletion object 有member variable, 咁就出事啦。姐係,singletion 主要for stateless 用途

Ads

Trinidad 2018-8-30 23:47:06 我個人低質
:^(


actual case

有個app有個static helper class static method係

static string formatstr(string input){
return input.Split(‘x’)[0];
}

呢個算係return reference? 咁concurrent call是咪會炒
➿➿➿➿ 2018-8-30 23:47:22 講番singleton
一個app有多個thread/class要share用同一個ref時
而個ref入面既data基本上冇咩事係唔會改
例如一隻lihkg app入面自己既user info, token之類咁
咁先啱用singleton
:^(

而如果要記低其他人既user info
就應該各自有個user object記低
:^(
實驗羊 2018-8-30 23:49:10 要睇C#唔用Return可唔可以改到Variable


int i = 0;

plusOne(i);

i = ?
Trinidad 2018-8-30 23:49:51 大家出手救吓我同我清下concept啦
我係屎我係垃圾
:^(
leucocephalus 2018-8-30 23:51:54 當然唔係,singletion 係shared by everyone, 要記各自ga資料,當然用prototype, 即係new obj
實驗羊 2018-8-30 23:52:17 Yes ar

其實 Singleton 同 Static 係好似,但前者可以 extends。
實驗羊 2018-8-30 23:53:02 佢講緊Client not server
i-vtec 2018-8-30 23:53:47 得呀RUN到呀

但用static錯左 ,你將條List改變左null
油尖旺豬肉佬 2018-8-30 23:53:58 [url]https://openhome.cc/Gossip/DesignPattern/SingletonPattern.htm
[/url]
幾有用
leucocephalus 2018-8-30 23:54:01
:^(
sorry, 做開backend.

Ads

油尖旺豬肉佬 2018-8-30 23:54:38 屌唔撚知點解多左<br>
https://openhome.cc/Gossip/DesignPattern/SingletonPattern.htm
蒼月A 2018-8-30 23:55:15 但唔知係我真心定咩
依然唔覺得i-vtec講autofac係singleton冇問題

查返autofac都係拎嚟做Scope
https://autofaccn.readthedocs.io/en/latest/lifetime/
The scope of a service is the area in the application where that service can be shared with other components that consume it. For example, in your application you may have a global static singleton - the “scope” of that global object instance would be the whole application. On the other hand, you might create a local variable in a for loop that makes use of the global singleton - the local variable has a much smaller scope than the global.

正正係佢講嗰種用法
令到Report分開唔會撞


但我認知中singleton真係會access同一個instance
even web application

即係例如如果我想拎一個Program第一次有人access至到而家嘅時間差
咁User A access個application create Singleton instance嘅時候就會記錄咗佢access嘅時間
時間差係0
之後User B入去, 拎返同一個Singleton instance, 咁佢就可以計返個同第一個User, 即係A嘅時間差

用法好唔一樣
leucocephalus 2018-8-30 23:58:03 岩呀
i-vtec 2018-8-31 00:01:34 你地個autofac
同java果個spring autowired唔同