RUBY設計模式之單例模式

2021-08-30 12:29:52 字數 3138 閱讀 8930

單例singleton,是所有設計模式中最簡單的,但又是問題最多的。其實並不簡單,一定要慎用。

singleton,和全域性變數很相似,所以要盡量的不用它,問題也就不會出來。而不是我們常常說的那樣這個我們可以用

singleton來實現。不出問題的唯一方法就是:別那麼做。

如果要使用了,在使用之前必須知道你要做的東西是否必須要用他來實現,還有就是要進行嚴密的測試。這樣說是有

事實依據的,我們現在跑了三四年的程式中就有好幾個地方用到的,在這期間發生了很多問題都和它有關。其中乙個問題是

想寫乙個singleton,但是沒有安裝標註去寫,也沒有嚴密的測試,結果造成了非常嚴重的後果。

下面是ruby實現singleton一些方法。大家多提意見。

#

# one

#class ******logger

attr_accessor :level

error = 1

warning = 2

info = 3

def initialize

@log = file.open("log.txt","w+")

@level = warning

enddef error(msg)

@log.puts("error:" + msg)

@log.flush

enddef warning(msg)

@log.puts("warning:" + msg) if @level >= warning

@log.flush

enddef info(msg)

@log.puts("info:" + msg) if @level >= info

@log.flush

end# # 建立單例模式,需要將這些方法放在initialize方法之後,要記住ruby是動態的

# @@instance = ******logger.new

def self.instance

return @@instance

endprivate_class_method :new

endlogger1 = ******logger.instance

logger2 = ******logger.instance

******logger.instance.info('computer wins chess game1')

******logger.instance.warning('computer wins chess game2')

## two

#require 'singleton'

class ******logger2

include singleton

attr_accessor :level

error = 1

warning = 2

info = 3

def initialize

@log = file.open("log.txt","w+")

@level = warning

enddef error(msg)

@log.puts("error:" + msg)

@log.flush

enddef warning(msg)

@log.puts("warning:" + msg) if @level >= warning

@log.flush

enddef info(msg)

@log.puts("info:" + msg) if @level >= info

@log.flush

endend

******logger.instance.error('computer wins chess game14444444444')

******logger.instance.warning('computer wins chess game24444444')

## three 使用類作為單例

#class classbasedlogger

error = 1

warning = 2

info = 3

@@log = file.open("log.txt","w+")

@@level = warning

def self.error(msg)

@@log.puts("error:" + msg)

@@log.flush

enddef self.warning(msg)

@@log.puts("warning:" + msg) if @level >= warning

@@log.flush

enddef self.info(msg)

@@log.puts("info:" + msg) if @level >= info

@@log.flush

enddef self.level

@@level

enddef self.level=(new_level)

@@level = new_level

endend

## four 使用模組作為單例,使用模組的好處是不能建立模組的例項

#module modulebasedlogger

error = 1

warning = 2

info = 3

@@log = file.open("log.txt","w+")

@@level = warning

def self.error(msg)

@@log.puts("error:" + msg)

@@log.flush

enddef self.warning(msg)

@@log.puts("warning:" + msg) if @level >= warning

@@log.flush

enddef self.info(msg)

@@log.puts("info:" + msg) if @level >= info

@@log.flush

enddef self.level

@@level

enddef self.level=(new_level)

@@level = new_level

endend

設計模式之單例模式

前一段時間買了一本秦小波寫的 設計模式之禪 網上對這書的評價很高。現在還沒有看很多,但是有些地方頗有感觸,也並不是所有的地方都能看懂,但是會慢慢研究的。自己對於設計模式的感覺就是乙個字 牛!感覺會23種設計模式並且會熟練運用的人,真的就是大師級的牛人了,設計模式是乙個專案主管或者架構師一定要會的東西...

設計模式之單例模式

package com.xie.singleton public class singleton 提供乙個共有的靜態的入口方法 public static singleton getinstance 懶漢式 延遲載入 提供乙個私有的靜態的成員變數,但不做初始化 private static sing...

設計模式之 單例模式

單例模式 singleton 保證乙個類僅有乙個例項,並提供乙個訪問它的全域性訪問點。單例模式 單件模式 使用方法返回唯一的例項 public class singleton private static singleton instance public static singleton geti...