面向對象
設計模式
===
-
簡單工廠模式
設計一個商品類和蘋果手機類,同時設計一個蘋果手機專賣店類。蘋果手機專賣店類可以下訂單和出售手機。運行效果
ip=IphoneShop() ip.order() ip.order() ip.order() print('庫存中共有:',IphoneShop.iphone_count,'臺手機') ip.sail() ip.sail() ip.sail() ip.sail() 運行結果: iphone X 正在出庫 iphone X 正在出庫 iphone X 正在出庫 庫存中共有: 3 臺手機 售出一臺手機,庫存還有2臺手機 售出一臺手機,庫存還有1臺手機 售出一臺手機,庫存還有0臺手機 手機已經(jīng)售完!!請進貨代碼如下
class Goods(object): def __init__(self,brand,price): self.price=price self.brand=brand def outGoods(self): print(self.brand,'正在出庫') class Iphone(Goods): def __init__(self,brand,price,name): super(Iphone, self).__init__(brand,price) self.name=name print(self.name, '正在出庫') def outGoods(self): print(self.name,'正在出庫') class Sasung(Goods): def __init__(self,brand,price,name,pencil): super(Sasung, self).__init__(brand,price) self.name=name self.pencil=pencil print(self.name, '正在出庫') def outGoods(self): print(self.name,'正在出庫') def createMobile(brand): if brand=='apple': return Iphone('apple',5600,'iphone X') elif brand=='sasung': return Sasung('sasung',4600,'galaxy s6','black pencil') class IphoneShop(object): iphone_count = 0 def __init__(self): self.iphone_count = 0 def order(self): IphoneShop.iphone_count += 1 return createMobile('apple') def sail(self): if IphoneShop.iphone_count>0: IphoneShop.iphone_count -= 1 print('售出一臺手機,庫存還有{0}臺手機'.format(IphoneShop.iphone_count)) else: print('手機已經(jīng)售完!!請進貨') ip=IphoneShop() ip.order() ip.order() ip.order() print('庫存中共有:',IphoneShop.iphone_count,'臺手機') ip.sail() ip.sail() ip.sail() ip.sail()工廠模式:
class Goods(object): def __init__(self,brand,price): self.price=price self.brand=brand def outGoods(self): print(self.brand,'正在出庫') class Iphone(Goods): def __init__(self,brand,price,name): super(Iphone, self).__init__(brand,price) self.name=name print(self.name, '正在出庫') class Sasung(Goods): def __init__(self,brand,price,name,pencil): super(Sasung, self).__init__(brand,price) self.name=name self.pencil=pencil print(self.name, '正在出庫') # def createMobile(brand): # if brand=='apple': # return Iphone('apple',5600,'iphone X') # elif brand=='sasung': # return Sasung('sasung',4600,'galaxy s6','black pencil') class MobileFactory(object): def createMobile(self,brand): if brand=='apple': return Iphone('apple',5600,'iphone X') elif brand=='sasung': return Sasung('sasung',4600,'galaxy s6','black pencil') class Shop(object): iphone_count = 0 def __init__(self): self.factory=MobileFactory() def order(self,brand): IphoneShop.iphone_count += 1 return self.factory.createMobile(brand) def sail(self): if IphoneShop.iphone_count>0: IphoneShop.iphone_count -= 1 print('售出一臺手機,庫存還有{0}臺手機'.format(IphoneShop.iphone_count)) else: print('手機已經(jīng)售完!!請進貨') ip=Shop() ip.order('apple') ip.order('sasung') ip.order('apple') print('庫存中共有:',IphoneShop.iphone_count,'臺手機') ip.sail() ip.sail() ip.sail() ip.sail() -
工廠方法模式
簡單工廠模式中MobileFactory對象由Shop類初始化來創(chuàng)建,這就決定了Shop只能創(chuàng)建MobileFactory對象。我們把MobileFactory對象交給Shop子類來創(chuàng)建,這樣就擴展了Shop的功能。如下:class Shop(object): def createGoods(self,brand): pass def order(self,brand): pass def sail(self): pass class MobileShop(Shop): Mobile_count=0 def createGoods(self,brand): MobileShop.Mobile_count +=1 return MobileFactory().createMobile(brand) def order(self,brand): mobile=self.createGoods(brand) print(mobile.name,'手機到貨,庫存為', MobileShop.Mobile_count) return mobile def sail(self): if MobileShop.Mobile_count>0: MobileShop.Mobile_count -= 1 print('售出一臺手機,庫存還有{0}臺手機'.format(MobileShop.Mobile_count)) else: print('手機已經(jīng)售完??!請進貨') ip=MobileShop() ip.order('apple') ip.order('sasung') ip.order('apple') ip.sail() ip.sail() ip.sail() ip.sail() -
單例模式預備 new
__new__()必須要有返回值,返回實例化出來的實例,需要注意的是,可以return父類__new__()出來的實例,也可以直接將object的__new__()出來的實例返回。
init()有一個參數(shù)self,該self參數(shù)就是new()返回的實例,init()在new()的基礎上可以完成一些其它初始化的動作,init()不需要返回值。
若new()沒有正確返回當前類cls的實例,那init()將不會被調用,即使是父類的實例也不行
我們可以將類比作制造商,new()方法就是前期的原材料購買環(huán)節(jié),init()方法就是在有原材料的基礎上,加工,初始化商品環(huán)節(jié)。
class A(object): def __new__(cls, name,age): print('new') return object.__new__(cls) def __init__(self,name,age): self.name=name self.age=age a1=A('tom',12) a2=A('tt',16) a3=A('ff',16) -
單例模式
class A(object): __instance=None __first=False def __new__(cls, name,age): if not cls.__instance: cls.__instance=object.__new__(cls) return cls.__instance def __init__(self,name,age): if not A.__first: self.name=name self.age=age A.__first=True a1=A('tom',12) a2=A('tt',16) a3=A('ff',16) print(id(a1))