一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 實例解析Python設計模式編程之橋接模式的運用

實例解析Python設計模式編程之橋接模式的運用

2020-08-15 11:41hitzjm Python

這篇文章主要介紹了Python設計模式編程之橋接模式的運用,橋接模式主張把抽象部分與它的實現部分分離,需要的朋友可以參考下

我們先來看一個例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#encoding=utf-8
#
#by panda
#橋接模式
 
def printInfo(info):
  print unicode(info, 'utf-8').encode('gbk')
 
#抽象類:手機品牌
class HandsetBrand():
  soft = None
  def SetHandsetSoft(self, soft):
    self.soft = soft
   
  def Run(self):
    pass
   
#具體抽象類:手機品牌1
class HandsetBrand1(HandsetBrand):
  def Run(self):
    printInfo('手機品牌1:')
    self.soft.Run()
 
#具體抽象類:手機品牌2
class HandsetBrand2(HandsetBrand):
  def Run(self):
    printInfo('手機品牌2:')
    self.soft.Run()
 
   
#功能類:手機軟件
class HandsetSoft():
  def Run(self):
    pass
 
#具體功能類:游戲  
class HandsetGame(HandsetSoft):
  def Run(self):
    printInfo('運行手機游戲')
     
#具體功能類:通訊錄  
class HandsetAddressList(HandsetSoft):
  def Run(self):
    printInfo('運行手機通信錄')
 
def clientUI():
  h1 = HandsetBrand1()
  h1.SetHandsetSoft(HandsetAddressList())
  h1.Run()
  h1.SetHandsetSoft(HandsetGame())
  h1.Run()
   
  h2 = HandsetBrand2()
  h2.SetHandsetSoft(HandsetAddressList())
  h2.Run()
  h2.SetHandsetSoft(HandsetGame())
  h2.Run()  
  return
 
if __name__ == '__main__':
  clientUI();

可以總結出類圖是這樣的: 

實例解析Python設計模式編程之橋接模式的運用

 

所以,橋接模式的概念在于將系統抽象部分與它的實現部分分離,使它們可以獨立地變化。
由于目標系統存在多個角度的分類,每一種分類都會有多種變化,那么就可以把多角度分離出來,讓它們獨立變化,減少它們之間的耦合。

下面我們再來看一個實例:

基本原理請參考相關書籍,這里直接給實例

假期旅游 從目的地角度可以分為 上海和大連,從方式角度可以分為跟團和獨體

橋接模式把這兩種分類連接起來可以進行選擇。

類圖:

實例解析Python設計模式編程之橋接模式的運用

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
#######################################################
#
# tour.py
# Python implementation of the Class DaLian
# Generated by Enterprise Architect
# Created on:   11-十二月-2012 16:53:52
#
#######################################################
 
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future_builtins import *
  
 
class TravelForm(object):
  """This class defines the interface for implementation classes.
  """
  def __init__(self, form="stay at home"):
    self.form=form
    pass
 
  def GetForm(self):
    return self.form
    pass
  pass
 
class Group(TravelForm):
  """This class implements the Implementor interface and defines its concrete
  implementation.
  """
  def __init__(self, form="by group"):
    super(Group,self).__init__(form)   
    pass
  pass
 
class Independent(TravelForm):
  """This class implements the Implementor interface and defines its concrete
  implementation.
  """
  def __init__(self, form="by myself"):
    super(Independent,self).__init__(form)
    pass
 
class Destination(object):
  """This class (a) defines the abstraction's interface, and (b) maintains a
  reference to an object of type Implementor.
  """
  m_TravelForm= TravelForm()
 
  def __init__(self, info):
    self.info=info
    pass
 
  def GetInfo(self):
    # imp->Operation();
    return print(self.info + " " +self.form.GetForm())
    pass
 
  def SetForm(self, form):
    self.form=form
    pass
 
class DaLian(Destination):
  """This class extends the interface defined by Abstraction.
  """
  def __init__(self, info="Go to DaLian "):
    super(DaLian,self).__init__(info)
    pass
 
class ShangHai(Destination):
  """This class extends the interface defined by Abstraction.
  """
  def __init__(self, info="Go to ShangHai"):
    super(ShangHai,self).__init__(info)
    pass
#客戶端
if(__name__=="__main__"):
  
  destination=ShangHai()
  destination.SetForm(Group())
  destination.GetInfo()
  
  
  destination=DaLian()
  destination.SetForm(Independent())
  destination.GetInfo()

運行結果

實例解析Python設計模式編程之橋接模式的運用

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: luan小说| free嫩白的12sex性自由 | 肉大捧一进一出视频免费播放 | 青草青草久热精品视频在线网站 | 97综合| 91麻豆国产福利精品 | 国产传媒天美果冻 | 逼水真多 | а天堂中文最新版在线 | 91高清国产经典在线观看 | 经典欧美gifxxoo动态图暗网 | 国产精品久久香蕉免费播放 | 欧美亚洲免费 | 日本精品中文字幕在线播放 | 美女隐私部位视频网站 | 国产精品免费小视频 | 精品一区视频 | 国内精品中文字幕 | 狠狠色综合久久婷婷 | www.com在线观看 | 久久噜国产精品拍拍拍拍 | 耽美肉文高h | 亚洲第一色网站 | 国产欧美一区二区三区久久 | 乌克兰bbw| 青青草精品在线观看 | 77色视频在线 | 色人阁图片 | 欧美在线一级片 | 好男人免费高清在线观看2019 | 国产欧美日韩在线不卡第一页 | 午夜熟女插插XX免费视频 | 九九免费精品视频 | 99久久er这里只有精品17 | 青青青草国产线观 | 性印度freehd | 免费观看国产精品 | 国产国语在线播放视频 | 91精品乱码一区二区三区 | 免费观看在线永久免费xx视频 | 四虎在线网址 |