Design Pattern----02.Creational.Builder.Pattern (Delphi Sample)

Intent

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.

Problem

An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

Discussion

Separate the algorithm for interpreting (i.e. reading and parsing) a stored persistence mechanism (e.g. RTF files) from the algorithm for building and representing one of many target products (e.g. ASCII, TeX, text widget). The focus/distinction is on creating complex aggregates.

The “director” invokes “builder” services as it interprets the external format. The “builder” creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the “builder”.

Affords finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the control of the “director”.

Structure

The Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets.

Scheme of Builder

Example

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. This pattern is used by fast food restaurants to construct children’s meals. Children’s meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children’s meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.

Example of Builder

Check list

  1. Decide if a common input and many possible representations (or outputs) is the problem at hand.
  2. Encapsulate the parsing of the common input in a Reader class.
  3. Design a standard protocol for creating all possible output representations. Capture the steps of this protocol in a Builder interface.
  4. Define a Builder derived class for each target representation.
  5. The client creates a Reader object and a Builder object, and registers the latter with the former.
  6. The client asks the Reader to “construct”.
  7. The client asks the Builder to return the result.

Rules of thumb

  • Sometimes creational patterns are complementory: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

Applications in Delphi(One)

The functionality used in Delphi’s VCL to create forms and components is similar in concept to the builder. Delphi creates forms using a common interface, through Application.CreateForm and through the TForm class constructor. TForm implements a common constructor using the resource information (DFM file) to instantiate the components owned by the form. Many descendant classes reuse this same construction process to create different representations. Delphi also makes developer extensions easy. TForm’s OnCreate event also adds a hook into the builder process to make the functionality easy to extend.

Implementation Example

The following example includes a class TAbstractFormBuilder and two concrete classes TRedFormBuilder and TBlueFormBuilder. For ease of development some common functionality of the concrete classes has been moved into the shared TAbstractFormBuilder class.


 
  1: type
  2:   TAbstractFormBuilder = class
  3:   private
  4:     FForm: TForm;
  5:     procedure BuilderFormClose(Sender: TObject; var Action: TCloseAction);
  6:   protected
  7:     function GetForm: TForm; virtual;
  8:   public
  9:     procedure CreateForm(AOwner: TComponent); virtual;
 10:     procedure CreateSpeedButton; virtual; abstract;
 11:     procedure CreateEdit; virtual; abstract;
 12:     procedure CreateLabel; virtual; abstract;
 13:     property Form: TForm read GetForm;
 14:   end;
 15: 
 16: type
 17:   TRedFormBuilder = class(TAbstractFormBuilder)
 18:   private
 19:     FNextLeft, FNextTop: Integer;
 20:   public
 21:     procedure CreateForm(AOwner: TComponent); override;
 22:     procedure CreateSpeedButton; override;
 23:     procedure CreateEdit; override;
 24:     procedure CreateLabel; override;
 25:   end;
 26: 
 27: type
 28:   TBlueFormBuilder = class(TAbstractFormBuilder)
 29:   private
 30:     FNextLeft, FNextTop: Integer;
 31:   public
 32:     procedure CreateForm(AOwner: TComponent); override;
 33:     procedure CreateSpeedButton; override;
 34:     procedure CreateEdit; override;
 35:     procedure CreateLabel; override;
 36:   end;

 

At runtime the client application instructs one of the concrete classes to create parts using the public part creation procedures. The concrete builder instance is passed to the folliwing procedure:


 
  1: procedure TForm1.Create3ComponentFormUsingBuilder(ABuilder: TAbstractFormBuilder);
  2: var
  3:   NewForm: TForm;
  4: begin
  5:   with ABuilder do begin
  6:     CreateForm(Application);
  7:     CreateEdit;
  8:     CreateSpeedButton;
  9:     CreateLabel;
 10:     NewForm := Form;
 11:     if NewForm <> nil then NewForm.Show;
 12:   end;
 13: end;

Builder in Delphi(Two)

UML

image

unit Pattern;

interface

uses
  Classes;

type

  TProduct = class
  private
    FParts: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(part: string);
    procedure Display;
  end;

  IBuilder = interface
  ['{9769DCD1-A2F5-4105-B28D-8A34DA6B0C12}']
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TBuilder1 = class(TInterfacedObject, IBuilder)
  private
    FProduct: TProduct;
  public
    constructor Create;
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TBuilder2 = class(TInterfacedObject, IBuilder)
  private
    FProduct: TProduct;
  public
    constructor Create;
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TDirector = class
  public
    procedure Construct(builder: IBuilder);
  end;

implementation

{ TProduct }

procedure TProduct.Add(part: string);
begin
  FParts.Add(part);
end;

constructor TProduct.Create;
begin
  inherited;
  FParts := TStringList.Create;
end;

destructor TProduct.Destroy;
begin
  FParts.Free;
  inherited;
end;

procedure TProduct.Display;
var
  I: integer;
begin
  WriteLn('Product Patrs -----------');
  for I := 0 to FParts.Count - 1 do begin
    WriteLn(FParts[I]);
  end;
end;

{ TBuilder1 }

procedure TBuilder1.BuildPartA;
begin
  FProduct.Add('Part A ');
end;

procedure TBuilder1.BuildPartB;
begin
  FProduct.Add('Part B ');
end;

constructor TBuilder1.Create;
begin
  inherited;
  FProduct := TProduct.Create;
end;

function TBuilder1.GetResult: TProduct;
begin
  Result := FProduct;
end;

{ TBuilder2 }

procedure TBuilder2.BuildPartA;
begin
  FProduct.Add('Part X ');
end;

procedure TBuilder2.BuildPartB;
begin
  FProduct.Add('Part Y ');
end;

constructor TBuilder2.Create;
begin
  inherited;
  FProduct := TProduct.Create;
end;

function TBuilder2.GetResult: TProduct;
begin
  Result := FProduct;
end;

{ TDirector }

procedure TDirector.Construct(builder: IBuilder);
begin
  builder.BuildPartA;
  builder.BuildPartB;
  builder.BuildPartB;
end;

end.
-------------------------------------------------------------------------
program Creational.Builder.Pattern;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Pattern in 'Pattern.pas';

var
  b1, b2: IBuilder;
  director: TDirector;
  product1, product2: TProduct;

begin
//  ReportMemoryLeaksOnShutDown := DebugHook  0;
  try
    director := TDirector.Create;
    try
      b1 := TBuilder1.Create;
      b2 := TBuilder2.Create;

      director.Construct(b1);
      product1 := b1.GetResult;
      product1.Display;

      director.Construct(b2);
      product2 := b2.GetResult;
      product2.Display;

      ReadLn;
    finally
      product1.free;
      product2.free;
      director.Free;
    end;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

Download

Delphi Source

aggregate ['æɡriɡət, 'æɡriɡeit]

· vi. 集合;聚集;合计

· vt. 集合;聚集;合计

· n. 合计;集合体;总计

· adj. 聚合的;集合的;合计的

polymorphic [,pɔli'mɔ:fik]

· adj. [生物] 多态的;[生物] 多形的;多形态的;[化学] 多晶形的(等于 polymorphous)

acid ['æsid]

· n. 酸;<俚>迷幻药

· adj. 酸的;讽刺的;刻薄的

peculiar [pi'kju:ljə]

· adj. 特殊的;独特的;奇怪的;罕见的

· n. 特权;特有财产

emphasize ['emfəsaiz]

· vt. 强调,着重

complicated ['kɔmplikeitid]

· adj. 难懂的,复杂的

转载于:https://www.cnblogs.com/xiuyusoft/archive/2011/06/17/2083540.html

代码下载地址: https://pan.quark.cn/s/a4b39357ea24 在计算机视觉技术中,数据集扮演着训练和评估模型的核心角色。Labelme作为一个广受欢迎的开源工具,能够支持用户以交互方式对图像进行标注,而COCO(Common Objects in Context)则是一种被广泛采纳的数据集标准格式,适用于包括物体检测、图像分割在内的多种任务。本文将详细阐述如何将Labelme生成的标注数据转换为COCO数据集的标准格式。 Labelme标注的图像在输出为JSON格式时,会包含以下核心内容: 1. `version`: 指明JSON文件的版本信息。 2. `flags`: 目前未定义或保持为空,预留用于未来的功能扩展。 3. `shapes`: 列表形式存储对象的形状信息,每个形状项包含`label`(对象类别名称),`points`(构成对象边缘的多边形顶点),以及`shape_type`(通常为“polygon)。 4. `imagePath`和`imageData`: 提供原始图像的存储路径和二进制数据,便于后续图像的还原。 5. `imageHeight`和`imageWidth`: 明确标注图像的垂直和水平尺寸。 COCO数据集的标准格式中定义了三种主要的标注类型: 1. Object instances(目标实例):主要用于执行物体检测任务。 2. Object keypoints(目标上的关键点):适用于人体姿态估计相关应用。 3. Image captions(看图说话):用于生成图像的文本描述。 COCO的JSON结构中包含以下基本组成部分: 1. `images`:记录图像的基本属性,包括`height`(高度)、`...
内容概要:本文围绕基于Basisformer模型的时间序列锂离子电池SOC(State of Charge,荷电状态)预测展开研究,利用PyTorch深度学习框架构建并训练模型,旨在提升锂电池SOC估计的准确性与鲁棒性。该方法融合Transformer架构的核心机制,通过引入基函数(Basis)分解策略,有效捕捉电池充放电过程中长时序、非线性动态特征,增强模型对复杂工况的适应能力。研究不仅详细阐述了Basisformer的网络结构设计、注意力机制优化与训练流程,还提供了完整的Python代码实现方案,涵盖数据预处理、模型搭建、损失函数定义、训练验证及结果可视化等环节,便于科研人员快速复现、调优并拓展至其他电池状态预测任务。; 适合人群:具备一定深度学习与Python编程基础,熟悉PyTorch框架,从事电池管理系统(BMS)、新能源汽车、储能系统、智能传感等领域的高校研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于动力电池与储能系统的实时SOC估算模块,提升系统安全性与能量利用效率;②作为学术研究的基础模型,用于复现、改进基于Transformer的时间序列预测方法在电化学系统中的应用;③为数据驱动的电池健康状态(SOH)、剩余使用寿命(RUL)联合估计提供可扩展的技术框架。; 阅读建议:建议读者结合所提供的代码与公开电池数据集(如NASA、CALCE等)进行动手实践,深入理解模型的输入输出结构与时序建模逻辑,同时可尝试引入温度、老化周期等多维特征,或融合物理模型构建混合预测架构,以进一步提升预测精度与泛化能力。
内容概要:本文系统阐述了基于动态规划算法优化插电式混合动力电动汽车(PHEV)能源管理的技术方案,结合Matlab与Simulink工具实现完整的仿真建模与代码开发。通过动态规划这一全局优化方法,在已知驾驶循环条件下,精确求解发动机、电机及电池之间的最优能量分配策略,以实现燃油消耗与排放的最小化目标,解决PHEV多能源路径规划中的复杂决策问题。文中提供了详尽的仿真模型构建流程与算法实现步骤,涵盖车辆动力学建模、能量管理架构设计、状态空间定义、代价函数构造、最优控制律求解及结果可视化分析等关键环节,全面揭示PHEV能量管理系统的内在机制与优化逻辑。; 适合人群:具备一定Matlab/Simulink编程基础,从事新能源汽车、智能控制、电力电子、自动化或交通运输工程等相关领域的研究生、科研人员及工程技术人员,尤其适合专注于车辆能量管理策略、节能控制算法研究的专业人士。; 使用场景及目标:①深入掌握动态规划在混合动力汽车能量管理中的理论基础与工程实现方法;②学习如何在Matlab/Simulink环境中搭建PHEV整车仿真平台并实施多目标优化仿真;③为学术研究、学位论文撰写或实际工程项目提供可复用的算法框架、模型模板与技术支持,支撑后续对等效燃油消耗最小化策略(ECMS)、模型预测控制(MPC)、实时优化算法等的对比研究与性能评估。; 阅读建议:建议读者结合所提供的完整代码与Simulink模型文件,逐模块调试运行,重点理解状态变量离散化处理、前后向递推求解过程、惩罚项设置以及边界条件处理等核心技术细节,同时可进一步拓展应用于不同工况场景、不同车型结构或与其他优化算法(如庞特里亚金极小值原理PMP)的对比验证,从而深化对PHEV能量管理实时性与全局性平衡问题的理解。
内容概要:本文围绕基于多虚拟同步发电机(VSG)的独立微网系统,开展多目标二次控制策略的MATLAB/Simulink建模与仿真研究。通过构建包含多个VSG单元的独立微网系统,设计并实现了能够同时实现频率与电压的无静差恢复、有功/无功功率精确分配以及环流有效抑制的综合控制目标的二次控制方法。研究重点在于控制策略的整体架构设计、关键控制模块的数学建模及其在Simulink环境中的精细化实现,通过大量仿真实验验证了所提控制策略在不同工况下的有效性、动态响应性能及系统鲁棒性。; 适合人群:具备电力系统分析、自动控制理论及现代电力电子技术等专业知识背景,熟悉MATLAB/Simulink仿真工具,从事新能源发电、微电网运行与控制、分布式能源系统集成等相关领域的科研人员、工程技术人员及高校研究生。; 使用场景及目标:① 深入掌握多VSG独立微网系统的建模方法与稳定性分析要点;② 理解并复现兼顾静态精度与动态品质的多目标二次协同控制算法;③ 为新型微网控制保护装置的研发及先进控制策略的工程化应用提供可靠的仿真验证平台和技术储备。; 阅读建议:学习者应在巩固电力系统基础理论的前提下,重点关注控制算法的设计逻辑、各控制环节间的耦合关系以及Simulink模块的搭建技巧,建议通过调整系统参数、设置不同的负载投切与故障扰动工况进行反复仿真,以深刻理解控制策略的内在机理与适应能力。
【通用视觉框架】基于Qt+Halcon开发的仿Visionmaster的通用视觉框架软件,全套源码,开箱即用 1.1 背景 ​ 本项目软件开发意图为实现对Halcon、Opencv算子及其它视觉软件的便捷使用,由于Halcon和Opencv使用相比VisionPro较为麻烦,故此本软件仿照海康VisionMaster的流程图式操作,实现对Halcon、Opencv及其它视觉软件的二次开发。 2.1 软件概述 本软件使用Qt框架进行开发,实现对视觉流程的自由搭配,市场上对标海康威视的VisionMaster; 本软件使用插件化开发框架,可使用提供的二次开发库自行添加新功能算子和新模块(将生成的插件放置到对应目录下即可); 2.2 功能概述: 视觉流程图式编程:实现对视觉/数据处理算子的自由编程,从而实现各类复杂的视觉需求 项目读取保存:将编程的视觉项目进行保存或者读取 图像显示:主界面中可以显示及监控视觉算子的图像处理情况 日志消息显示:显示软件运行过程中出现的日志消息 多语言:可进行多种语言切换 2.3 开发平台 主开发语言:Qt(C++) C++语言标椎:C++17 开发环境:Window/Linux 编程平台:Qt Creator 编译器: |版本 | MSVC | Qt 6.4.0 MSVC2019 64bit | | Mingw | Qt 6.4.0 MinGW 64-bit | 视觉工具:Halcon19.11 Progress X64 资源介绍请查阅:https://blog.csdn.net/m0_37302966/article/details/146980317 更多视觉框架资源:https://blog.csdn.net/m0_37302966/article/details/146583453
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值