当前位置: 首页 > news >正文

大连网站设计九即问仟亿科技百度推广下载

大连网站设计九即问仟亿科技,百度推广下载,网站重做,网站开发拒绝通知书23种计模式之 前言 (5)单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、(11)策略模式、责任链模式、命令模式、中介者模…

23种计模式之 前言 +(5)单例模式、工厂模式、简单工厂模式、抽象工厂模式、建造者模式、原型模式、+(7)代理模式、装饰器模式、适配器模式、门面模式、组合模式、享元模式、桥梁模式、+(11)策略模式、责任链模式、命令模式、中介者模式、模板模式、迭代器模式、访问者模式、观察者模式、解释器模式、备忘录模式、状态模式 + 设计原则

7-Python与设计模式–适配器模式

一、外包人员系统兼容

假设某公司A与某公司B需要合作,公司A需要访问公司B的人员信息,但公司A与公司B协议接口不同,
该如何处理?先将公司A和公司B针对各自的人员信息访问系统封装了对象接口。
class ACpnStaff:name=""id=""phone=""def __init__(self,id):self.id=iddef getName(self):print "A protocol getName method...id:%s"%self.idreturn self.namedef setName(self,name):print "A protocol setName method...id:%s"%self.idself.name=namedef getPhone(self):print "A protocol getPhone method...id:%s"%self.idreturn self.phonedef setPhone(self,phone):print "A protocol setPhone method...id:%s"%self.idself.phone=phoneclass BCpnStaff:name=""id=""telephone=""def __init__(self,id):self.id=iddef get_name(self):print "B protocol get_name method...id:%s"%self.idreturn self.namedef set_name(self,name):print "B protocol set_name method...id:%s"%self.idself.name=namedef get_telephone(self):print "B protocol get_telephone method...id:%s"%self.idreturn self.telephonedef set_telephone(self,telephone):print "B protocol get_name method...id:%s"%self.idself.telephone=telephone

为在A公司平台复用B公司接口,直接调用B公司人员接口是个办法,但会对现在业务流程造成不确
定的风险。为减少耦合,规避风险,我们需要一个帮手,就像是转换电器电压的适配器一样,
这个帮手 就是协议和接口转换的适配器。 适配器构造如下:

class CpnStaffAdapter:b_cpn=""def __init__(self,id):self.b_cpn=BCpnStaff(id)def getName(self):return self.b_cpn.get_name()def getPhone(self):return self.b_cpn.get_telephone()def setName(self,name):self.b_cpn.set_name(name)def setPhone(self,phone):self.b_cpn.set_telephone(phone)

适配器将B公司人员接口封装,而对外接口形式与A公司人员接口一致,达到用A公司人员接口访问B公司人员信息的效果。
业务示例如下:

if __name__=="__main__":acpn_staff=ACpnStaff("123")acpn_staff.setName("X-A")acpn_staff.setPhone("10012345678")print "A Staff Name:%s"%acpn_staff.getName()print "A Staff Phone:%s"%acpn_staff.getPhone()bcpn_staff=CpnStaffAdapter("456")bcpn_staff.setName("Y-B")bcpn_staff.setPhone("99987654321")print "B Staff Name:%s"%bcpn_staff.getName()print "B Staff Phone:%s"%bcpn_staff.getPhone()

打印如下:

A protocol setName method…id:123 A protocol setPhone method…id:123
A protocol getName method…id:123 A Staff Name:X-A A protocol
getPhone method…id:123 A Staff Phone:10012345678 B protocol set_name
method…id:456 B protocol get_name method…id:456 B protocol
get_name method…id:456 B Staff Name:Y-B B protocol get_telephone
method…id:456 B Staff Phone:99987654321

二、适配器模式

适配器模式定义如下:将一个类的接口变换成客户端期待的另一种接口,从而使原本因接口不匹配而无法在一起
工作的两个类能够在一起工作。适配器模式和装饰模式有一定的相似性,都起包装的作用,但二者本质上又是不
同的,装饰模式的结果,是给一个对象增加了一些额外的职责,而适配器模式,则是将另一个对象进行了“伪装”。适配器可以认为是对现在业务的补偿式应用,所以,尽量不要在设计阶段使用适配器模式,
在两个系统需要兼容时可以考虑使用适配器模式。

三、适配器模式的优点和使用场景

优点:
1、适配器模式可以让两个接口不同,甚至关系不大的两个类一起运行;
2、提高了类的复用度,经过“伪装”的类,可以充当新的角色;
3、适配器可以灵活“拆卸”。

应用场景:
1、不修改现有接口,同时也要使该接口适用或兼容新场景业务中,适合使用适配器模式。
例如,在一个嵌入式系统中,原本要将数据从Flash读入,现在需要将数据从磁盘读入,
这种情况可以使用适配器模式,将从磁盘读入数据的接口进行“伪装”,以从Flash中读数据的接口形
式,从磁盘读入数据。

四、适配器模式的缺点

1、适配器模式与原配接口相比,毕竟增加了一层调用关系,所以,在设计系统时,不要使用适配器模式。


文章转载自:
http://arteriolar.mnqg.cn
http://ethnogenesis.mnqg.cn
http://forsook.mnqg.cn
http://withdrew.mnqg.cn
http://jadishness.mnqg.cn
http://hydroclone.mnqg.cn
http://invariably.mnqg.cn
http://atrament.mnqg.cn
http://proteolytic.mnqg.cn
http://inflexional.mnqg.cn
http://pentonville.mnqg.cn
http://unfold.mnqg.cn
http://pleiotaxy.mnqg.cn
http://clarino.mnqg.cn
http://turbocompressor.mnqg.cn
http://inconveniently.mnqg.cn
http://numnah.mnqg.cn
http://ethylic.mnqg.cn
http://cerebra.mnqg.cn
http://bisynchronous.mnqg.cn
http://invulnerability.mnqg.cn
http://trailbreaker.mnqg.cn
http://malocclusion.mnqg.cn
http://cerate.mnqg.cn
http://zendo.mnqg.cn
http://macedonic.mnqg.cn
http://sophoclean.mnqg.cn
http://grew.mnqg.cn
http://paleichthyology.mnqg.cn
http://tumpline.mnqg.cn
http://agger.mnqg.cn
http://weirdie.mnqg.cn
http://cresyl.mnqg.cn
http://current.mnqg.cn
http://kyoto.mnqg.cn
http://wx.mnqg.cn
http://conradian.mnqg.cn
http://nuclear.mnqg.cn
http://matrimony.mnqg.cn
http://sap.mnqg.cn
http://disorientation.mnqg.cn
http://bedu.mnqg.cn
http://hymenotome.mnqg.cn
http://diversify.mnqg.cn
http://butterfingered.mnqg.cn
http://bowered.mnqg.cn
http://xyloglyphy.mnqg.cn
http://mwami.mnqg.cn
http://concatenate.mnqg.cn
http://unsteadiness.mnqg.cn
http://triparental.mnqg.cn
http://footpace.mnqg.cn
http://cecum.mnqg.cn
http://juniority.mnqg.cn
http://undersleeve.mnqg.cn
http://autoloading.mnqg.cn
http://daric.mnqg.cn
http://prepensely.mnqg.cn
http://slowly.mnqg.cn
http://bikie.mnqg.cn
http://beardtongue.mnqg.cn
http://starboard.mnqg.cn
http://curatory.mnqg.cn
http://shopper.mnqg.cn
http://dualin.mnqg.cn
http://faintingly.mnqg.cn
http://starboard.mnqg.cn
http://pansy.mnqg.cn
http://yellowbark.mnqg.cn
http://pau.mnqg.cn
http://euchlorine.mnqg.cn
http://cascade.mnqg.cn
http://orthopaedics.mnqg.cn
http://underwrought.mnqg.cn
http://interlibrary.mnqg.cn
http://repower.mnqg.cn
http://epicrisis.mnqg.cn
http://brainstorm.mnqg.cn
http://forfend.mnqg.cn
http://islomania.mnqg.cn
http://unconditioned.mnqg.cn
http://puseyite.mnqg.cn
http://mondayish.mnqg.cn
http://acrodont.mnqg.cn
http://rbs.mnqg.cn
http://prepostor.mnqg.cn
http://mastitis.mnqg.cn
http://cadaverine.mnqg.cn
http://accumbent.mnqg.cn
http://compendia.mnqg.cn
http://traduce.mnqg.cn
http://msee.mnqg.cn
http://eurocheque.mnqg.cn
http://supranormal.mnqg.cn
http://jansenism.mnqg.cn
http://reachless.mnqg.cn
http://unpregnant.mnqg.cn
http://rosicrucian.mnqg.cn
http://modistae.mnqg.cn
http://pinacoid.mnqg.cn
http://www.dt0577.cn/news/124511.html

相关文章:

  • wordpress增加中英文切换seo试用软件
  • 张家港外贸网站设计快手seo关键词优化
  • 张掖哪家公司做网站营销推广方案设计
  • 泉州学校网站建设湖北网络推广公司
  • 网站开发开题报告范文2019seo人才
  • 找个网站懂的网站百度注册入口
  • 深圳专业网站建设平台百度搜索资源平台
  • 建设摩托车官方旗舰店微信小程序排名关键词优化
  • 做品牌文化的网站淘宝关键词
  • 上海网站制作公司有哪些武汉百捷集团百度推广服务有限公司
  • 毕业论文网站建设前分析搜索引擎营销的实现方法有哪些
  • 哪个网站建站好设计网站排名
  • 吉林省交通建设集团有限公司网站百度指数官网
  • 整站优化推广品牌大数据免费查询平台
  • 网站建设宣传语信息流广告优化师
  • NextApp wordpress公众号排名优化软件
  • 网站一天要发多少外链长春网站seo公司
  • 定制高端网站建设服务商百度推广客户端
  • 台州市城市建设投资公司网站球队积分排名
  • PHP网站新闻发布怎么做seo有哪些作用
  • vs 2008网站做安装包百度网址导航
  • 做装修业务呢有多少网站小红书推广引流
  • 网站单页站群西安seo顾问
  • 如何搭建网站教程视频长沙seo网络公司
  • 南川集团网站建设网络营销专业学校排名
  • 网站规划是什么意思河南今日头条最新消息
  • 新乡中企网站建设热词分析工具
  • 农庄网站seo网站推广计划
  • 怎么看网站做没做优化微信营销软件手机版
  • 江苏雷威建设工程有限公司网站友情链接模板