欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

舉例講解Python中的Null模式與橋接模式編程

 更新時(shí)間:2016年02月02日 16:57:21   作者:dongweiming  
這篇文章主要介紹了Python中的Null模式與橋接模式編程,Null模式與橋接模式都屬于Python的設(shè)計(jì)模式編程,需要的朋友可以參考下

Null模式
我想每個(gè)人都有一種經(jīng)歷,為了獲取某屬性,但是有時(shí)候?qū)傩允荖one,那么需要你做異常處理, 而假如你想節(jié)省這樣的條件過濾的代碼,可以使用Null模式以減少對(duì)象是否為None的判斷

python的例子
我舉個(gè)不是很通用的例子,只是為了讓大家理解這個(gè)模式:我有很多類, 但是不是每個(gè)類都有類方法test,所以我調(diào)用類方法就要做個(gè)異常處理,類似這樣

class A(object):
  pass

class B(object):
  b = 1
  @classmethod
  def test(cls):
    print cls.b

def get_test(x):
  try:
    return x.test
  except AttributeError: 
    return None

# 我這里只寫了2個(gè)類,但是其實(shí)有很多類
for i in [A, B]:
  test = get_test(i)
  # 我要判斷以下是否獲得了這個(gè)類方法才能決定是否可以執(zhí)行
  if test:
    test()

但是我用Null方法就可以這樣

class Null(object):

  def __init__(self, *args, **kwargs):
    "忽略參數(shù)"
    return None

  def __call__(self, *args, **kwargs):
    "忽略實(shí)例調(diào)用"
    return self

  def __getattr__(self, mname):
    "忽略屬性獲得"
    return self

  def __setattr__(self, name, value):
    "忽略設(shè)置屬性操作"
    return self

  def __delattr__(self, name):
    '''忽略刪除屬性操作'''
    return self

  def __repr__(self):
    return "<Null>"

  def __str__(self):
    return "Null"

還是上面的功能

class Null(object):

  def __init__(self, *args, **kwargs):
    "忽略參數(shù)"
    return None

  def __call__(self, *args, **kwargs):
    "忽略實(shí)例調(diào)用"
    return self

  def __getattr__(self, mname):
    "忽略屬性獲得"
    return self

  def __setattr__(self, name, value):
    "忽略設(shè)置屬性操作"
    return self

  def __delattr__(self, name):
    '''忽略刪除屬性操作'''
    return self

  def __repr__(self):
    return "<Null>"

  def __str__(self):
    return "Null"

橋接模式
這個(gè)模式其實(shí)就是把產(chǎn)品類的實(shí)現(xiàn)和抽象類分離,能夠靈活的變化,假如你記得狀態(tài)模式,它是修改內(nèi)部屬性, 而橋接模式是指定好內(nèi)部屬性,每個(gè)產(chǎn)品類指定這個(gè)屬性被橋接模式類調(diào)用,適用于產(chǎn)品類可能經(jīng)常調(diào)整變化,這樣還能減少了產(chǎn)品類之間的耦合

python的例子
這里實(shí)現(xiàn)一個(gè)打印操作系統(tǒng)名字的功能

class Bridge(object):

  def __init__(self):
    self.__implementation = None

  def someFunctionality(self):
    raise NotImplemented()

class UseCase1(Bridge):
  # 根據(jù)初始化參數(shù)傳入實(shí)現(xiàn)的產(chǎn)品類
  def __init__(self, implementation):
    self.__implementation = implementation
  # 根據(jù)傳入的產(chǎn)品類的屬性打印結(jié)果
  def someFunctionality(self):
    print "UseCase1: ",
    self.__implementation.anotherFunctionality()


class UseCase2(Bridge):
  def __init__(self, implementation):
    self.__implementation = implementation

  def someFunctionality(self):
    print "UseCase2: ",
    self.__implementation.anotherFunctionality()


class ImplementationInterface:

  def anotherFunctionality(self):
    raise NotImplemented

# 這里其實(shí)才是實(shí)現(xiàn)的產(chǎn)品類
class Linux(ImplementationInterface):

  # 它定義了這個(gè)方法,回應(yīng)操作系統(tǒng)的名字
  def anotherFunctionality(self):
    print "Linux!"


class Windows(ImplementationInterface):
  def anotherFunctionality(self):
    print "Windows."


def main():
  linux = Linux()
  windows = Windows()

  useCase = UseCase1(linux)
  useCase.someFunctionality()

  useCase = UseCase1(windows)
  useCase.someFunctionality()

  useCase = UseCase2(linux)
  useCase.someFunctionality()

  useCase = UseCase2(windows)
  useCase.someFunctionality()


if __name__ == "__main__":
  main()

相關(guān)文章

最新評(píng)論