首页 > 其他分享 >学习高校课程-软件设计模式-建造者模式和原型模式(lec4)

学习高校课程-软件设计模式-建造者模式和原型模式(lec4)

时间:2024-10-24 21:00:31浏览次数:1  
标签:director code objects 软件设计 builder 模式 lec4 construction class

Builder: Problem

Example: a complex object that requires laborious, step-by-step initialization of many fields and nested objects
一个复杂对象的创建通常由多个部分组成,这些部分的组合经常变化

Builder: Solution

Extract the object construction code out of its own class and move it to separate objects called builders

Further extract the series of calls to the builder steps into a separate class called director

进一步将对构建器步骤的一系列调用提取到一个名为 director 的单独类中
– The director defines the order in which to execute the building steps, while the builder provides concrete implementation
– Having a director class is not strictly necessary
– Might be a good place to put various construction routines for reuse
– Completely hiding the details of construction from the client code
– director 定义执行构建步骤的顺序,而构建器提供具体的实现
– 拥有一个 director 类并不是绝对必要的
– 可能是放置各种构建例程以供重用的好地方
– 在客户端代码中完全隐藏构建细节

Builder: Implementation

  1. Clearly define the common construction steps for building all available product representations
  2. Declare these steps in the base builder interface
  3. Create a concrete builder class for each of the product representations and implement their construction steps, and also implement a method for fetching the result of construction
  4. Consider creating a director class (not necessarily)
  5. The client code creates both the builder and the director objects
  6. The construction result can be obtained directly from the director only if all products follow the same interface; otherwise, the client should fetch the result from the builder

1.明确定义构建所有可用产品表示的常见构建步骤
2.在 base builder 界面中声明这些步骤
3.为每个产品表示创建一个具体的 builder 类并实现它们的构造步骤,并实现一个获取构造结果的方法
4.考虑创建一个 director 类(不一定)
5.客户端代码会同时创建 builder 和 director 对象
6.只有当所有产品都遵循相同的接口时,才能直接从 director 获得构造结果;否则,客户端应从生成器中获取结果

Prototype: Problem and Solution

Problem: creating an exact copy of an object
问题:创建对象的精确副本
– Some fields may not be visible from the outside
– The code becomes dependent on that class
– Sometimes you only know the interface but not the concrete class
– 某些字段可能从外部不可见
– 代码变得依赖于该类
– 有时您只知道接口,而不知道具体的类

Solution: the Prototype pattern
– Delegating the cloning process to the actual objects being cloned
– A common interface for all objects supporting cloning, with a singleclone method
– An object supporting cloning is called a prototype
– 将克隆过程委托给正在克隆的实际对象
– 支持克隆的所有对象的通用接口,具有单个
– 支持克隆的对象称为原型

Prototype: Example

Produce exact copies of geometric objects, without coupling the code to their classes
生成几何对象的精确副本,而无需将代码耦合到其类

Note: a subclass may call the parent’s cloning method before copying its own field values to the resulting object.
注意:子类可以调用父级的克隆方法将自己的 Field 值复制到结果对象中。

标签:director,code,objects,软件设计,builder,模式,lec4,construction,class
From: https://www.cnblogs.com/Mephostopheles/p/18499549

相关文章

  • 代理模式
    代理设计模式首先为什么单独介绍这个设计模式,因为这个设计模式实现了SpringAoP的底层原理,后续在Spring的文章中我会提到设计模式回顾模式的起源模式是在特定环境下解决某类重复出现问题的一套成功或有效的解决方案。每个模式是在特定的环境下才有效,学习模式,除了了解模式本身......
  • 基于RFC3394标准的AES-128-ECB模式的密钥封装(Key Wrap)和解封(Key Unwrap)
    密钥封装(KeyWrap):RFC3394默认IV为0xA6,0xA6,0xA6,0xA6,0xA6,0xA6,0xA6,0xA6。使用AES_Encrypt函数对IV和密钥数据块进行加密,并将结果与步数异或。经过6n轮迭代后,将最终的IV和加密后的数据块复制到输出的密文中。密钥解封(KeyUnwrap):从输入的密文中提取了IV和加密的......
  • Keepalived+Nginx 高可用(主从模式)
     1.环境说明vipip操作系统nginx端口主从说明192.168.115.150192.168.115.148CentOSLinuxrelease7.6.1810(Core)8443主节点192.168.115.150192.168.115.149CentOSLinuxrelease7.6.1810(Core)8443从节点2台机器都需部署nginx和keealive服务,ngin......
  • AbMole|哺乳动物 DNA 中无碱基位点的复杂基因组模式研究
    DNA损伤在生物学和疾病中起着至关重要的作用,而无碱基(AP)位点作为一种常见的DNA损伤类型,其基因组学特征仍未被充分探索。来自华侨大学医学院基因组学研究所的YeCai,  1HuifenCao,  2FangWang等多名研究人员发表了题为《Complexgenomicpatternsofabasicsitesinm......
  • 学习高校课程-软件设计模式-工厂模式(lec3)
    FactoryMethod:ProblemExample:alogisticsmanagementapplication示例:物流管理应用程序–Firstversion:handlingtransportationbytrucks,withaTruckclass–Later:newrequeststoincorporatesealogistics,andmore–第一个版本:处理卡车运输,卡车类别–......
  • 绞杀者模式:逐步迁移与替代遗留系统的策略解析
    如果你觉得这篇文章对你有帮助,请不要吝惜你的“关注”、“点赞”、“评价”、“收藏”,你的支持永远是我前进的动力~~~  个人收藏的技术大会分享PDF文档,欢迎点击下载查看!!!在软件工程领域,遗留系统的现代化改造一直是企业面临的挑战之一。本文将介绍一种渐进式的系统迁移策略......
  • 设计模式(六)适配器模式详解
    设计模式(六)适配器模式详解结果型模式:从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题。适配器模式简介定义:适配器模式是一种结构型模式,它不允许不兼容的接口协同工作。通过创建一个适配器类,将一个类的接口转换成客户端期望的另一个接口。Adapter模式......
  • 设计模式(五)原型模式详解
    设计模式(五)原型模式详解原型模型简介定义:原型模型是一种创建型设计模型,它允许通过克隆/复制现有对象来创建新的对象,而无需通过常规的构造函数进行实例化。这种方式的主要优势是在运行时不需要知道具体的类,只需一个实例对象即可。实现方法实现Cloneable接口:在Java中,如......
  • 群控系统服务端开发模式-程序草图设计
    本系统采用PHP开发,采用thinkphp6作为php应用框架。一、总控制    1、Base.php        a、是否是登录后的验证父类,也就是需要登录才能操作的控制都需要继承他;        b、继承BaseController.php;    2、Emptys.php  ......
  • LVS三种模式区别及负载均衡算法
    LVS简介LVS(LinuxVirtualServer)即linux虚拟服务器,是一个虚拟的服务器集群系统正向代理和反向代理 正向代理:只用于代理内部网络对internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到web服务器上的http请求发送到代理服务器,正向代理指的是客户端代理 反向代......