实例解析Python设计模式编程之桥接模式的运用
作者:hitzjm 发布时间:2021-06-03 18:48:04
标签:Python,设计模式
我们先来看一个例子:
#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();
可以总结出类图是这样的:
所以,桥接模式的概念在于将系统抽象部分与它的实现部分分离,使它们可以独立地变化。
由于目标系统存在多个角度的分类,每一种分类都会有多种变化,那么就可以把多角度分离出来,让它们独立变化,减少它们之间的耦合。
下面我们再来看一个实例:
基本原理请参考相关书籍,这里直接给实例
假期旅游 从目的地角度可以分为 上海和大连,从方式角度可以分为跟团和独体
桥接模式把这两种分类连接起来可以进行选择。
类图:
# -*- 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()
运行结果


猜你喜欢
- 本文转自微信公众号:"算法与编程之美"一、前言三步搭建MUI页面主框架法包括新建含mui的HTML文件、输入mheade
- 侧边栏在响应式设计中起到很大的作用,当屏幕小到手机的屏幕时,能够自适应屏幕大小的侧边栏固然能够为网站添加色彩,那么在Bootstrap的框架
- 在“循环”一节,我们已经讨论了Python基本的循环语法。这一节,我们将接触更加灵活的循环方式。range()在Python中,for循环后
- 1. 如何停止任务?我们可以通过 asyncio.Task 对象上的 cancel() 方法取消任务。如果任务被取消,cancel() 方法
- 快速搭建scrapy开发环境pythonpippip百度网盘注:不同的电脑上所带有环境不同,安装方式有些许差别1、成功安装python并添加
- 本文讲述了python实现删除文件与目录的方法。分享给大家供大家参考。具体实现方法如下:os.remove(path)删除文件 path.
- 一.基本概念事务是指满足ACID特性的的一组操作,可以通过Commit提交事务,也可以也可以通过Rollback进行回滚。会存在中间态和一致
- 网上找了很多资料,都不理想。其实ubuntu20以后的版本,很多功能都预装好了,安装django也没有以前的版本那么复杂。很简单,只需要几步
- 我们日常用CSS布局的时候,关于图片背景,大部分的人都是一个背景一张图片的,怎么说呢?这是很标准的方法,但是这种普通制作方式下要保存大量图片
- 用django框架来做一些后台管理的web页面简直太方便了,django自带模块级的权限系统,用来做一些内部的系统非常合适,可以大大的减少开
- 使用了Python的 xml.etree.ElementTree 库xml.etree.ElementTree 库简介xml.etree.E
- 建立网站时,有很多理由让你遵循Web标准。企业、开发人员和用户都会从兼容标准的做法中获益。这里列举了一些最令人信服的理由,为什么所有网站都应
- <html xmlns="http://www.w3.org/1999/xhtml"><head>
- Python的集合set原理集合(set)是一个无序的不重复元素序列。可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个
- 本文实例讲述了Python面向对象类的继承。分享给大家供大家参考,具体如下:一、概述面向对象编程 (OOP) 语言的一个主要功能就是“继承”
- Firefox 3.5已经发布了几个月了,且已经历5次小幅更新。而基于Gecko 1.9.2的Firefox 3.6也已经开发数月,现在已经
- Django###request如果说 urls.py 是 Django 中前端页面和后台程序桥梁,那么 request 就是桥上负责运输的
- 本文实例讲述了Python实现购物评论文本情感分析操作。分享给大家供大家参考,具体如下:昨晚上发现了snownlp这个库,很开心。先说说我开
- python解决循环依赖1.概述在使用python开发过程中在引入其他模块时可能都经历过一个异常就是循环引用most likely due
- 一、需要的参数1、通讯用户:touser 或 通讯组:toparty2、企业ID:corpid3、应用ID/密钥:agentId,secre