1. 程式人生 > >python單例模式示例

python單例模式示例

# -*- coding: utf-8 -*-
class SendMessage(object):
		"""呼叫第三方介面傳送簡訊的功能"""
		
		def __new__(cls, *args, **kwargs):
				if not hasattr(cls, "_instance_"):
						cls._instance_ = super(SendMessage, cls).__new__(cls)
						cls.interface_url = "www.yangxiao1.com"
						print "第一次進來"
				return cls._instance_
		
		def send_msg(self, sim, msg):
				url = self.interface_url + "?msg=" + msg + "&phone=" + sim
				print url
				

s = SendMessage()
s.send_msg("110", "hello,110")

s2 = SendMessage()
s2.send_msg("120", "hello,120")

s3 = SendMessage()
s3.send_msg("119", "hello,119")