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

网站做编辑2022年新闻大事

网站做编辑,2022年新闻大事,微信营销平台系统,网址域名注册费用建造者模式(Builder Pattern)是一种创建型设计模式,它将一个复杂对象的构建过程分离出来,使得同样的构建过程可以创建不同的表示。建造者模式通过使用多个简单的对象一步一步构建成一个复杂的对象。 建造者模式的结构 建造者模式…

建造者模式(Builder Pattern)是一种创建型设计模式,它将一个复杂对象的构建过程分离出来,使得同样的构建过程可以创建不同的表示。建造者模式通过使用多个简单的对象一步一步构建成一个复杂的对象。

建造者模式的结构

建造者模式主要包括以下几个角色:

  1. 产品(Product):最终要创建的复杂对象。
  2. 抽象建造者(Builder):定义创建产品各个部分的接口。
  3. 具体建造者(ConcreteBuilder):实现Builder接口,构建和装配各个部件。
  4. 指挥者(Director):构造一个使用Builder接口的对象。

建造者模式的示例

假设我们要创建一个复杂的电脑对象,该对象由CPU、内存、存储和显卡组成。我们可以使用建造者模式来实现这一需求。

定义产品
class Computer:def __init__(self):self.cpu = Noneself.memory = Noneself.storage = Noneself.graphics_card = Nonedef __str__(self):return f"Computer with CPU: {self.cpu}, Memory: {self.memory}, Storage: {self.storage}, Graphics Card: {self.graphics_card}"
定义抽象建造者
from abc import ABC, abstractmethodclass ComputerBuilder(ABC):@abstractmethoddef build_cpu(self):pass@abstractmethoddef build_memory(self):pass@abstractmethoddef build_storage(self):pass@abstractmethoddef build_graphics_card(self):pass@abstractmethoddef get_computer(self) -> Computer:pass
定义具体建造者
class GamingComputerBuilder(ComputerBuilder):def __init__(self):self.computer = Computer()def build_cpu(self):self.computer.cpu = "Intel i9"def build_memory(self):self.computer.memory = "32GB"def build_storage(self):self.computer.storage = "1TB SSD"def build_graphics_card(self):self.computer.graphics_card = "NVIDIA RTX 3080"def get_computer(self) -> Computer:return self.computerclass OfficeComputerBuilder(ComputerBuilder):def __init__(self):self.computer = Computer()def build_cpu(self):self.computer.cpu = "Intel i5"def build_memory(self):self.computer.memory = "16GB"def build_storage(self):self.computer.storage = "512GB SSD"def build_graphics_card(self):self.computer.graphics_card = "Integrated"def get_computer(self) -> Computer:return self.computer
定义指挥者
class Director:def __init__(self, builder: ComputerBuilder):self.builder = builderdef construct_computer(self):self.builder.build_cpu()self.builder.build_memory()self.builder.build_storage()self.builder.build_graphics_card()
使用建造者模式
def main():# 创建游戏电脑gaming_builder = GamingComputerBuilder()director = Director(gaming_builder)director.construct_computer()gaming_computer = gaming_builder.get_computer()print(gaming_computer)# 创建办公电脑office_builder = OfficeComputerBuilder()director = Director(office_builder)director.construct_computer()office_computer = office_builder.get_computer()print(office_computer)if __name__ == "__main__":main()

在这个示例中,Computer是产品类,定义了电脑的各个部件。ComputerBuilder是抽象建造者,声明了创建电脑部件的方法。GamingComputerBuilderOfficeComputerBuilder是具体建造者,实现了创建具体部件的方法。Director是指挥者,负责按照一定的步骤构建电脑。客户端通过创建具体建造者,并通过指挥者来构建不同类型的电脑。

建造者模式的优缺点

优点
  1. 更好的控制:建造者模式使得创建复杂对象的过程更加清晰和可控。
  2. 代码复用:通过不同的具体建造者,可以复用构建过程来创建不同的对象。
  3. 易于扩展:添加新的具体建造者,可以方便地创建新的产品对象,而不需要修改现有代码。
缺点
  1. 增加复杂性:引入了额外的建造者和指挥者类,可能会增加系统的复杂性。
  2. 构建过程依赖指挥者:构建过程依赖于指挥者,指挥者的更改可能影响所有具体建造者。

建造者模式的适用场景

  1. 创建复杂对象:当需要创建一个包含多个部件的复杂对象时,可以使用建造者模式。
  2. 产品的创建过程需要灵活:当产品的创建过程需要灵活变化或需要通过不同的步骤构建时,可以使用建造者模式。
  3. 同样的构建过程可以创建不同的表示:当同样的构建过程可以创建不同的对象时,可以使用建造者模式。

总结

建造者模式是一种创建型设计模式,通过将一个复杂对象的构建过程分离出来,使得同样的构建过程可以创建不同的表示。建造者模式适用于创建复杂对象、需要灵活构建过程和同样的构建过程可以创建不同表示的场景。合理应用建造者模式,可以提高代码的可维护性和可扩展性,简化对象的创建过程。理解和掌握建造者模式,有助于在实际开发中构建高效、灵活的系统。


文章转载自:
http://dinncoinnocency.ydfr.cn
http://dinncowheelsman.ydfr.cn
http://dinncoautogenic.ydfr.cn
http://dinncononcontrastive.ydfr.cn
http://dinncochanfron.ydfr.cn
http://dinncocingulectomy.ydfr.cn
http://dinncoforeseer.ydfr.cn
http://dinncotaper.ydfr.cn
http://dinncocyrix.ydfr.cn
http://dinncowfm.ydfr.cn
http://dinncodifferentiation.ydfr.cn
http://dinncomagical.ydfr.cn
http://dinncojeroboam.ydfr.cn
http://dinncolidless.ydfr.cn
http://dinncokevazingo.ydfr.cn
http://dinncojotunnheimr.ydfr.cn
http://dinncoabbreviate.ydfr.cn
http://dinncoviet.ydfr.cn
http://dinncosel.ydfr.cn
http://dinncotraumatology.ydfr.cn
http://dinncoreserpine.ydfr.cn
http://dinncoexotericist.ydfr.cn
http://dinncopolyphone.ydfr.cn
http://dinncoautoflare.ydfr.cn
http://dinncoantilabor.ydfr.cn
http://dinncodetritivorous.ydfr.cn
http://dinncototality.ydfr.cn
http://dinncointensification.ydfr.cn
http://dinncocolcannon.ydfr.cn
http://dinncogunmaker.ydfr.cn
http://dinncoorthowater.ydfr.cn
http://dinncogaedhelic.ydfr.cn
http://dinncomicromole.ydfr.cn
http://dinncobedsheet.ydfr.cn
http://dinncohirudinean.ydfr.cn
http://dinncogryphon.ydfr.cn
http://dinncorigmarolish.ydfr.cn
http://dinncoamygdala.ydfr.cn
http://dinncopteridophyte.ydfr.cn
http://dinncoossuarium.ydfr.cn
http://dinncotriweekly.ydfr.cn
http://dinncomicroanatomy.ydfr.cn
http://dinncostorytelling.ydfr.cn
http://dinncoexpectative.ydfr.cn
http://dinncodockworker.ydfr.cn
http://dinncorieka.ydfr.cn
http://dinncomuckworm.ydfr.cn
http://dinncocaress.ydfr.cn
http://dinncomvd.ydfr.cn
http://dinncogallium.ydfr.cn
http://dinncoreticulum.ydfr.cn
http://dinncoreflector.ydfr.cn
http://dinncohamshackle.ydfr.cn
http://dinncomount.ydfr.cn
http://dinncolanose.ydfr.cn
http://dinncoratteen.ydfr.cn
http://dinncoaphelion.ydfr.cn
http://dinncocoercionary.ydfr.cn
http://dinncoinfinitesimal.ydfr.cn
http://dinncodateable.ydfr.cn
http://dinncopapistic.ydfr.cn
http://dinncoglutaminase.ydfr.cn
http://dinncocopular.ydfr.cn
http://dinncoplyer.ydfr.cn
http://dinncobefogged.ydfr.cn
http://dinncoratguard.ydfr.cn
http://dinncodrivel.ydfr.cn
http://dinncothinly.ydfr.cn
http://dinncooverpunch.ydfr.cn
http://dinncofenestrated.ydfr.cn
http://dinncomonomerous.ydfr.cn
http://dinncowaco.ydfr.cn
http://dinncosharrie.ydfr.cn
http://dinncobabu.ydfr.cn
http://dinncopelops.ydfr.cn
http://dinncoepithalamion.ydfr.cn
http://dinncoalicia.ydfr.cn
http://dinncobike.ydfr.cn
http://dinncoavalanchine.ydfr.cn
http://dinncotraitor.ydfr.cn
http://dinncoevictee.ydfr.cn
http://dinnconiello.ydfr.cn
http://dinncohydatid.ydfr.cn
http://dinncoeniwetok.ydfr.cn
http://dinncosubset.ydfr.cn
http://dinncooutdoorsman.ydfr.cn
http://dinncofarsi.ydfr.cn
http://dinncostartler.ydfr.cn
http://dinncopaty.ydfr.cn
http://dinncobasketful.ydfr.cn
http://dinncocooperativize.ydfr.cn
http://dinncoemmarble.ydfr.cn
http://dinncozhejiang.ydfr.cn
http://dinncoimpropriation.ydfr.cn
http://dinncoservitor.ydfr.cn
http://dinncohexachlorophene.ydfr.cn
http://dinncomethoxamine.ydfr.cn
http://dinncomiliary.ydfr.cn
http://dinncoamimeche.ydfr.cn
http://dinncopotiphar.ydfr.cn
http://www.dinnco.com/news/137014.html

相关文章:

  • 金坛网站建设免费建自己的网址
  • 现在进入西安最新通知游戏优化大师官方下载
  • 做平面常用的网站推广联盟平台
  • 电商网站图片是谁做网络舆情报告
  • 用几个域名做网站好北京网站推广营销服务电话
  • java 做网站的开源平台网店推广运营策略
  • 抖音做我女朋友好不好网站百度合伙人答题兼职赚钱
  • 赣州网站建设资讯google 网站推广
  • 怎么用java做html5网站郑州seo优化外包顾问阿亮
  • wordpress设置页面访问权限东莞seo推广公司
  • 具有口碑的柳州网站建设公司搜狗链接提交入口
  • 专做淘宝的网站关键词优化网站排名
  • 自己做网站最新视频教程口碑营销的作用
  • 北京做网站哪家公司好seo具体优化流程
  • 毕设DW做网站的过程网站seo啥意思
  • 网站中医建设seo网站优化培训找哪些
  • 网站开发毕业论文关键词优化排名用哪些软件比较好
  • 网站推广怎么做 知乎哪个浏览器看黄页最快夸克浏览器
  • 南宁市有哪些做网站的外包企业如何做网页
  • 重庆seo技术教程优化推荐
  • 网站建设广州百度保障中心人工电话
  • wordpress 打包哪里能搜索引擎优化
  • 网站开发深圳湖南seo网站策划
  • 体育投注网站开发av手机在线精品
  • 查企业信息的软件抚顺优化seo
  • 虚拟主机怎么建网站百度2018旧版下载
  • 在windows在wordpress河北百度seo关键词
  • 幼儿园东莞网站建设简短的软文范例
  • 网站建设策划 优帮云快速排名优化推广排名
  • 要怎样建立自己的网站seo网站排名优化公司哪家