设计模式、用Delphi描述-->Visitor模式

Delphi设计模式重构:策略模式替代复杂条件分支的工程实践.pdf 文档支持目录章节跳转同时还支持阅读器左侧大纲显示和章节快速定位,文档内容完整、条理清晰。文档内所有文字、图表、函数、目录等元素均显示正常,无任何异常情况,敬请您放心查阅与使用。文档仅供学习参考,请勿用作商业用途。 如果你正在寻找一种高效、强大的编程语言来开发跨平台应用,那么Delphi绝对值得考虑!它拥有直观的可视化开发环境,让代码编写变得轻松简单。凭借着快速的编译速度和卓越的性能优化,Delphi能够帮助开发者迅速构建出高质量的应用程序。无论是Windows、macOS、iOS还是Android平台,Delphi都能完美支持,真正实现一次编写,多平台部署。选择Delphi,开启高效开发之旅! 立即下载

 

 

 

Visitor 模式

起源

Delphi中的Visitor模式在基本Visitor模式进行了扩展。更多Visitor模式的资料请参 [Gam+, pages 331..344].

目的

表示一个作用于某个对象结构的中和元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。

 [Gam+, page 331].

动机

考虑一个面向对象的建模工具,比如说‘Rational Rose、ModelMaker’,它将一个模型表示为类和类的成员。

在建模工具上提供了许多操作成员功能,比如:列表类的所有成员、生成类的代码框架、反向工程等。

这些操作大多对不同的成员进行不同的操作。它将成员分成字段(fields)、方法(methods)、

属性(properties)。因些我们必须建立专门处理字段的类,专门处理methods的类等等。成员类的集合当然依赖被编译的语言。但对于一给定语言变化不大。

 

 

 

如图显示了部分成员类的框架。问题产生了,如果我将所有这些操作分散到不同的成员类,

将会导致整个系统难于理解,修改,维护。将类代码生成与类成员检查放在一起,将产生混乱。些外加入新的操作时要重新编译的有的类(至少也重新编译所有的相关的系)。有个办法:你可能独立的增加一个新的操作,并这个成员类独立如作用于其上的操作。

要实现上述两个目标,我们可以将每个类中相关操作包装在一上独立的对象(称为visitor)

并在遍历类成员列表时将此对象传递给当前成员。当一个成员‘接受’ 访问,该成员向访问者发送包含自身信息的请求。该成员请自本身作为一个参数。访问者执行这些操作。

例如:一个不使用访问者的代码生成器可能会通成员类的抽象的方法:TMember.WriteInterfaceCode(Output: TStream)生成代码。每一个成员都会调用WriteInterfaceCode生成适当的输出代码。如果通过访问者来生成代码,则会创建一个TinterfaceCodeVisitor对象,并在成员列表上调用参数为访问对象的AcceptVisitor方法。每一个在员在实现AcceptVisitor将会回调visitor:一个字段将调用访问者的VisitField方法,而一个方法则调用VisitMethod方法。这样,以前类Tfield的WriteInterfaceCode操作现在成为TinterfaceCodeVisitor的VisitField操作。

 

为使访问者不仅仅只做代码生成,我们需要所有的成员列表的访问者有一个抽象的父类TmemberVisitor。TmemberVisitor必须为每一个成员定义一种方法。一个需要将成员输出为HTML格式的应用将定义TmemberVisitor新的子类,并不再需要在成员类中增加与特定应用相关的代码。Visitor模式将每个操作封装在一个相关的Visitor中

 


 

 

 

使用Visitor模式,必须定义两个层次的类:一个应于接受操作的元素(Tmember层次)另一个定义于对元素的操作(TmemberVisitor 层次)。增加一个新的操作时只需给访问者层次增加一个新的子类。我可能简单的定义新的TmemberVisitor子类以增加新的功能。

 

应用

下面的代码演示上面描述的类Tmember的Visitor模式的应用

 

type

  TMember = class (TObject)

  public

    procedure AcceptMemberVisitor(Visitor: TMemberVisitor); virtual;

  end;

 

  TField = class (TMember)

  public

    procedure AcceptMemberVisitor(Visitor: TMemberVisitor); override;

  end;

 

  TMethod = class (TMember)

  public

    procedure AcceptMemberVisitor(Visitor: TMemberVisitor); override;

  end;

 

  TProperty = class (TMember)

  public

    procedure AcceptMemberVisitor(Visitor: TMemberVisitor); override;

  end;

 

  TMemberVisitor = class (TObject)

  public

    procedure VisitField(Instance: TField); virtual;

    procedure VisitMember(Instance: TMember); virtual;

    procedure VisitMethod(Instance: TMethod); virtual;

    procedure VisitProperty(Instance: TProperty); virtual;

  end;

 

implementation

 

{ TMember }

 

begin

  Visitor.VisitMember(Self);

end;

 

{ TField }

procedure TField.AcceptMemberVisitor(Visitor: TMemberVisitor);

begin

end;

 

{ TMethod }

procedure TMethod.AcceptMemberVisitor(Visitor: TMemberVisitor);

begin

  Visitor.VisitMethod(Self);

end;

 

{ TProperty }

procedure TProperty.AcceptMemberVisitor(Visitor: TMemberVisitor);

begin

  Visitor.VisitProperty(Self);

end;

 

{ TMemberVisitor }

procedure TMemberVisitor.VisitField(Instance: TField);

begin

end;

 

procedure TMemberVisitor.VisitMember(Instance: TMember);

begin

end;

 

procedure TMemberVisitor.VisitMethod(Instance: TMethod);

begin

end;

 

procedure TMemberVisitor.VisitProperty(Instance: TProperty);

begin

end;

 

说明:

·      TMember, TField, TMethod 和 Tproperty都实现了AcceptMemberVisitor方法. 这些方法都嵌入模式中

·      TMemberVisitor 类实现了VisitMember, VisitField等方法。TmemberVisitor是一个抽象的类,它所有的方法由具体的子类实现。

下面是一个简单的代码生成器的实现。

代码介绍:

·      TCodeGenerationVisitor 是一个用于实现成员的代码生成器的访问者。

·      访问者定义了一个上下文相关的属性:Output: TTextStream,

·       它必须在VisitXXX调用前被定,如:DrawingVisitor典型的需要一个包括canvas的上下文,来支持画图操作。上下文在遍历整个member对列前赋予了代码生成器。

·      代码生成器将整结的生成的类的所有代码

 

要真正的了解Visitor模式,你可执行这个例子 ,并进一步的学习双分派机制: accept/visit.

 

unit CodeGenerators;

 

interface

 

uses Classes, TextStreams;

 

type

 

  TCodeGenerator = class (TObject)

  public

    procedure Generate(Members: TList; Output: TTextStream);

  end;

 

implementation

 

uses Members;

 

type

  TCodeGenerationVisitor = class (TMemberVisitor)

  private

    FOutput: TTextStream;

  public

    procedure VisitField(Instance: TField); override;

    procedure VisitMethod(Instance: TMethod); override;

    procedure VisitProperty(Instance: TProperty); override;

    property Output: TTextStream read FOutput write FOutput;

  end;

 

 

{ TCodeGenerationVisitor }

procedure TCodeGenerationVisitor.VisitField(Instance: TField);

begin

  Output.WriteLnFmt('  %s: %s;', [Instance.Name, Instance.DataName]);

end;

 

procedure TCodeGenerationVisitor.VisitMethod(Instance: TMethod);

var

  MKStr, DTStr: string;

begin

  case Instance.MethodKind of

    mkConstructor: MKStr := 'constructor';

    mkDestructor: MKStr := 'destructor';

    mkProcedure: MKStr := 'procedure';

    mkFuntion: MKStr := 'function';

  end;

  if Instance.MethodKind = mkFunction then

    DTStr := ': ' + Instance.DataName

  else

    DTStr := '';

  {代码不完整,现足以演示Tmethod代码生成 }

  Output.WriteLnFmt('  %s %s%s%s;'

                    [MKStr, Instance.Name, Instance.Parameters, DTStr]);

end;

 

procedure TCodeGenerationVisitor.VisitProperty(Instance: TProperty);

begin

  Output.WriteLnFmt('  property %s: %s read %s write %s;',

                    [Instance.Name, Instance.DataName,

                     Instance.ReadSpecifier, Instance.WriteSpecifier]);

end;

 

{ TCodeGenerator }

procedure TCodeGenerator.Generate(Members: TList; Output: TTextStream);

var

  I: Integer;

begin

  {写入类定义 }

  Output.WriteLine('TSample = class (TObject)');

 

 

  {好! 加入代码生成器的访问者}

  Visitor := TCodeGenerationVisitor.Create;

  Try

    {记住为访问都提供上下文,以便更好的访问VisitXXX方法。}

    for I := 0 to Members.Count - 1 do

      { 代码的具体段,好事情发生了}

      TMember(Members[I]).AcceptMemberVisitor(Visitor);

  finally

    Visitor.Free;

  end;

  {类成员的代码生成完毕}

  Output.WriteLine('end;');

end;

 

Delphi实例

正在组织

//很多摘自《设计模式》,

questasim的傻瓜式安装与仿真教程 一、安装 1、下载软件 链接:https://pan.baidu.com/s/1S05AnO_w-wy6RsAj_kdUXg 提取码:p2nt 2、根据电脑类型是32位还是64位,解压不同安装包。 解压得: 2、运行.exe文件,出现: 问题及解决办法: 这是杀毒软件造成的,因此在安装过程中关闭杀毒软件即可。关闭之后,再次运行.exe文件。 3、点击下一步,进入选择安装位置界面,如下:选择好安装位置后点击下一步。 4、点击同意 5、接下来等待安装结束即可,在安装过程中,会跳出来弹框,询问是否要添 阅读详情

相关推荐

ThingsBoard入门实战(二):ThingsBoard 设备接入

文章目录ThingsBoard 设备接入一、设备配置/设备类型二、设备三、设备接入HTTPCOAPMQTT四、下一步 ThingsBoard 设备接入 设备接入并不一定需要真正的设备,我们可以通过程序。或者命令来模拟设备,向平台发送信息。 这节我们先为用户分配一个设备,然后通过模拟设备命令更新设备的状态(遥测值)。 一、设备配置/设备类型 添加一个叫"路灯"的设备配置。 设备配置听起来比较抽象,可以认为是设备类型或者是设备类型的配置。实际上,ThingsBoard 中很多时候让选择设备类型,就是选的这个。

AIGC搞起 1万+

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook

模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。 1.CoffeineBeverageWithHook {《HeadFirst设计模式》之模板方法模式 } { 编译工具: Delphi7.0 } { E-Mail : xshlife@163.com } unit uCoffeineBeve

djCode 733

HighTec安装(官网免费)编译 嘎嘎详细~

安装软件最好有自己的一套安装规则,工程软件除了安装主体外还会有很多其他的东西,比如license或者还会有一些驱动,如果所有都随意安装会导致你需要卸载,或者更新软件时,有的需要将原来软件卸载干净,如果没有清晰的安装路径那真是灾难啊~请确保此目录中只有个人许可证。这样基本的配置就ok了,下面打开hightec软件检验下license是否识别到了,会让你选个工作区,建议自己建一个,要不你以后就找不到了,,,如下图拉到下面,按照要求填写信息,随便写就行,注意主要的MAC地址要填本机的物理地址,然后点击2即可。

weixin_60793044的博客 1万+

设计模式、用Delphi实现---->Singleton 模式

Singleton模式起源Delphi的SINGLETON模式是在SINGLETON的基础上进行了扩展。更多SINGLETON模式的资料请参阅 《设计模式84页》目的保证一个类仅有一个实例,并提供一个访问它的全局访问点,一个相对简单的应用型设计模式动机此模式最重要的是保证大量类正确的访问单个实例。尽管一台个系统可能有多台打印机,但系统只允许有且只有一个打印缓存。同样比如:一

programlover的专栏 1176

《涨知识啦29》-DBR的工作原理及其在VCSEL中的作用

分布式布拉格(Distributed Bragg Reflection,简称DBR)反射镜是垂直腔面发射激光器(Vertical-Cavity Surface-Emitting Laser,简称VCSEL)的重要组成部分。典型的DBR反射镜是由1/4ni波长厚度的高折射率和低折射率材料交替组成(ni是该层的折射率)。其反射率取决于组成对数、边界条件以及折射率差。 图1 布拉格反射镜的工作原理 DBR反射镜的工作原理如图1所示。根据反射原理,当光从光疏介质n1射向光密介质n2(折射率n2>n1)时,反

simucal的博客 7983

Coding Plan:程序员真实工作的可执行编码决策框架

Coding Plan 是一种面向工程实践的编码决策方法论,它超越传统学习路径,聚焦于代码在真实业务系统中的上下文对齐、影响控制与可演进性保障。其核心在于将模糊的开发直觉转化为可度量、可验证、可协作的日常动作,涵盖目标对齐、上下文锁定、验证闭环、协作契约和演进预留五大锚点。结合2026年AI辅助编码新范式,它强调将大模型作为‘上下文翻译器’而非代码生成器,并通过分钟级工作块、技术债主动发行、可观测性前置等机制,显著提升交付确定性与系统长期健康度。本文详解该框架的底层逻辑与落地实践。

weixin_33326218的博客 438

《大话设计模式》之 简单工厂模式 Delphi/C shap /JAVA 实现

{计算器的功能实现 实现工厂设计模式 通过面向对象的思想,进行设计,所用的编程思想为对象的三大特性:封装,继承,多态 通过实现一个简单的 计算器的功能(输入两个数,进行加,减,乘,除) 思想思路: 1.设计虚拟父类; 2.子类继承父类 3. 通过虚拟方法,进行进行重载 4.工厂类,实例化对象,采用多态 }unit uOperation;i

tjb_1216的专栏 2314

Tushare Pro的安装使用——“抱歉,您没有访问该接口的权限,权限的具体详情访问:https://tushare.pro/document/1?doc_id=108”问题

最近做毕设关于量化交易的, 因此一直使用tushare作数据源,但是过程中到一些问题,因此这里写篇文章,供大家一起交流下。 Tushare简介 Tushare是一个免费、开源的python财经数据接口包。拥有如股票、基金、期货等行情数据,公司财务、基金经理等基本面数据,以及债券、外汇、行业、大数据、区块链。Tushare返回的绝大部分的数据格式都是pandas DataFrame类型,因此非常方便...

sinat_40875078的博客 1万+

设计模式---Delphi 篇

本文以“观察者”设计模式介绍 “对象--关系映射 ORM ”下面以Delphi  tiOPF 框架实现原理介绍:Abstract the Visitor logicBefore we go any further, we must abstract the Visitor functionality away from the business objects so we never

Mr.Kim.Wu 的日记 1367

【芯片设计- RTL 数字逻辑设计入门 番外篇6 -- 术语 Tile 介绍】

在系统级芯片(System on Chip, SoC)设计中,“tile” 是一个可以指代不同概念的术语,但通常它指的是芯片上的一个独立的功能单元或核心。一个 tile 可以包含一个或多个处理器核心、缓存、内存控制器、输入输出设备、外设接口或其他功能模块。在 SoC 上,多个 tile 可以被组合和连接起来,以形成一个完整的系统。

CodingCos的博客 3070

STM32F407最小系统硬件设计与CubeMX工程实践

嵌入式最小系统是MCU功能实现的物理基础,其核心涵盖供电网络、时钟架构、复位机制与调试接口四大模块。基于Cortex-M4内核的STM32F407对电源纹波、晶振稳定性及信号完整性提出严苛要求:模拟电源VDDA需<10mV纹波,HSE晶振布线须满足短距直连与匹配电容精准配置,SWD调试接口依赖50Ω阻抗控制与端接防护。这些硬件约束直接映射至STM32CubeMX配置——如RCC时钟树参数必须与实物晶振频率严格一致,GPIO模式需匹配外设电气特性(如I2C必选开漏),否则将引发通信失败或启动异常。典型应用场景

weixin_35592186的博客 173

设计模式、用Delphi描述-->Visitor模式 (转)

设计模式、用Delphi描述--&gtVisitor模式 (转)[@more@] ...

csd3176的博客 96

【OpenCV】第二十三章: 图像特征检测之SURF、FAST、BRIEF、OBR算法

4、由于在检测特征点时是需要对图像中所有的像素点进行检测,然而图像中的绝大多数点都不是特征点,如果对每个像素点都进行上述的检测过程,那显然是种浪费,所以FAST采用一种进行非特征点判别的方法:首先对候选点的周围90度的点(1,9,5,13)四个点进行测试,而且是先测试1和9,如果它们两个符合阈值要求再测试5和13。继SIFT和SURF后,大家在特征点检测的速度上又进行了很多的改进尝试,但速度的提升效果都不是非常明显,直到FAST算法诞生,特征点检测的速度才有了一个质的飞跃。无法计算描述符的特征点将被删除。

因为热爱所以坚持 2158

Delphi面向对象设计:如何构建优雅的类结构

职责明确的边界清晰的扩展方便的使用简单的维护轻松的记住:代码首先是给人读的,其次才是给机器执行的。即使是在强调性能的Delphi开发中,良好的设计也能带来长期的可维护性优势。

mgcps的专栏 359

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Delphi五种设计模式

<br />今天抽空浏览了一下Delphi写的设计模式,网页在如下位置:<br />http://www.delphi-jedi.org/art_patterns.html<br />今天再看。仍然有种感觉。我把Observer模式的代码补充到下面。在Delphi 7已经编译通过。Lazarus应该也没有问题。<br />unit Unit2;<br />interface<br />type<br />TObservable = class;<br />TObserver = class;<br />TO

astra_zhao的专栏 2352

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

设计模式、用Delphi实现---->Decorator 模式

   Decorator 模式起源Delphi的Decorator模式是在Decorator的基础上进行了扩展。更多Decorator模式的资料请参阅 《设计模式115页》目的动态地给一个对象添加一些职责。就增加功能来说,Decorator模式比增加子类更为灵活.动机我们经常要为某一些个别的对象增加一些新的职责,并不是全部的类。假设,我们有一组类用来输出文本行。

programlover的专栏 5175

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

《大话设计模式》之 策略模式 Delphi实现

{  策略模式   :用来封装算法,处理各种变法,而简单工厂模式,则是解决创建对象的问题  策略(Strategy)模式的用意是定义一组算法(algorithms),并将每个算法封装到具有共同接口的独立的类中,  从而使它们可以相互替换。  策略模式让算法变化独立于使用它的客户端   使用情景,商场促销,进行商品打折,应用不同的算法,故采用策略模式。 1。一

tjb_1216的专栏 1338

设计模式、用Delphi实现---->Template Method模式

<!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--><script type="text/javascript"

zgqtxwd的专栏 605

Delphi模式编程之策略模式(下)

Delphi模式编程之策略模式(续) 刘 艺1.3                         策略模式在酒店管理系统中的应用在酒店管理系统中,通常客房的价格不是一成不变的。对于住宿的淡季和旺季、老客户和新客户、散客和团队,都应该有不同的销售策略。显然,销售策略决定了报价。但是基于销售策略的报价体系又不能绑定于某一具体的客户端,因为只有把基于销售策略的报价体系独立出来,才能保证

newdream的专栏 1443

用 Delphi 学设计模式(二) 之 工厂方法篇

“开闭原则”:一个模块应该易于扩展,免于修改 问题:请考虑上一章的例子中,如果添加一个新的具体水果类“西瓜”需要做哪些工作。 本章完成以下内容: 1、代码用支持中文的 Delphi 2005 编译并通过,并去除了其中一些无关紧要的地方,如异常处理等 ; 2、重新设计一个情景,分别用“简单工厂模式”和“工厂方法模式”两种方法实现,请体...

weixin_30606669的博客 174

设计模式学习笔记5:Singleton模式及其Delphi实现

Singleton(单件)模式是一种很有用的设计模式。它的意图的是:仅仅创建一个类的实例,并提供一个访问它的全局访问点。全局变量使的一个对象易被访问,但不能防止你实例化多个对象。单件模式的目的就是确保在程序的生命周期内只有一个实例存在。多实例实现了数据的并行访问,而单例模式是把它变成串行访问。所以这模式的用途非常的广泛,如处理数据库连接的访问等等。Delphi实现Singleton也有许多方案,在

每天一小步 1409

Delphi版设计模式之单例一

可能Delphi王朝真的没落了,在网上查了很久都是一些重复、陈旧、残缺不全的帖子,搞的像古代的武功秘籍一样,每次只能获取上篇或者下篇,而这些上下还有可能是残篇 设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。 引自菜鸟教程 从上面的话中不难看出,所谓的设计模式是针对一些常见问题总结出来的一套比较优质的解决方案,所以

舞动的代码 890

《大话设计模式》之 策略模式 Delphi实现

{ 策略模式 :用来封装算法,处理各种变法,而简单工厂模式,则是解决创建对象的问题 策略(Strategy)模式的用意是定义一组算法(algorithms),并将每个算法封装到具有共同接口的独立的类中, 从而使它们可以相互替换。 策 略模式让算法变化独立于使用它的客户端 使用情景,商场促销,进行商品打折,应用不同的算法,故采用策略模式。 1。一个抽取类 2

easyboot的专栏 1304

Delphi版设计模式

用delphi诠释设计模式

QAQ的小耗子的博客 553

Delphi语言的编程范式

Delphi语言是Object Pascal的一个扩展,结合了结构化编程、面向对象编程(OOP)和事件驱动编程等多种编程范式。以其简洁明了的语法和强大的类库,Delphi使开发人员可以高效地开发Windows应用程序、Web应用程序以及移动应用程序。Object Pascal在Pascal语言的基础上增添了面向对象编程的特性,例如类、继承、多态和封装等。这些特性使得程序的结构更清晰、可维护性更高。同时,Object Pascal也支持异常处理、集合、接口等特性。

2501_90394924的博客 949

Delphi版设计模式之单例二

​接上篇Delphi版设计模式之单例一,细心的朋友可能发现我在上一篇文章中重写类NewInstance和FreeInstance函数,原因是:【在delphi中编译器对构造函数的保护级别进行了处理,即便设为private,编译器仍然会将其修正为public,所以覆盖基类中的NewInstance类方法,系统在每次构造对象时都会调用这个类方法,通过重载它就可以实现对构造函数的控制】,同时在上篇文章中有一段核心代码 我以注释的形式标注这里在并发环境下存在安全隐患,那么现在我们就通过多线程的方式研究一下这个问.

舞动的代码 579
上一篇: 设计模式、用Delphi实现---->Builder模式
下一篇: 设计模式、用Delphi实现---->Decorator 模式
programlover
博客等级 码龄26年 1粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值