{"id":4509,"date":"2023-05-16T10:27:41","date_gmt":"2023-05-16T09:27:41","guid":{"rendered":"https:\/\/gdksoftware.com\/knowledgebase\/spring4d-factory-pattern"},"modified":"2023-05-16T18:44:36","modified_gmt":"2023-05-16T17:44:36","slug":"spring4d-factory-pattern","status":"publish","type":"knowledge","link":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern","title":{"rendered":"Spring4D factory pattern"},"content":{"rendered":"<div style=\"padding-top: 12px; padding-bottom: 12px;\">\n<p>Que op\u00e7\u00f5es existem na estrutura do <strong>Spring4D<\/strong> que n\u00e3o conhecemos, mas que s\u00e3o <strong>muito \u00fateis<\/strong>? Bem, na verdade, s\u00e3o muitos. Um deles \u00e9 o <strong>Factory Pattern<\/strong>. Ele \u00e9 muito \u00fatil quando voc\u00ea deseja aplicar o padr\u00e3o\u00a0<a href=\"https:\/\/gdksoftware.com\/knowledgebase\/solid-principles-in-delphi-2-the-open-closed-principle\">open\/closed principle<\/a>. Seu c\u00f3digo est\u00e1 aberto para extens\u00e3o, mas fechado para modifica\u00e7\u00e3o.<\/p>\n<p>Um exemplo cl\u00e1ssico \u00e9 trabalhar com enumera\u00e7\u00f5es ou n\u00fameros que indicam um determinado estado. Isso cria rapidamente todos os tipos de estruturas condicionais que precisam ser adaptadas assim que um novo status ou valor \u00e9 adicionado. No exemplo abaixo, determinado c\u00f3digo \u00e9 executado dependendo do status de uma fatura. Para dividir o c\u00f3digo, foi aplicado o Factory pattern.<\/p>\n<p>Essa \u00e9 a defini\u00e7\u00e3o de Invoice e status:<\/p>\n<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">type<\/span>\r\n  <span style=\"color: #008800; font-style: italic;\">{$SCOPEDENUMS ON}<\/span>\r\n  TInvoiceStatus = (<span style=\"color: #000080; font-weight: bold;\">New<\/span>, Sent, Overdue, Paid);\r\n  <span style=\"color: #008800; font-style: italic;\">{$SCOPEDENUMS OFF}<\/span>\r\n\r\n  TInvoice = <span style=\"color: #000080; font-weight: bold;\">class<\/span>\r\n  <span style=\"color: #000080; font-weight: bold;\">public<\/span>\r\n    <span style=\"color: #000080; font-weight: bold;\">function<\/span> Status: TInvoiceStatus;\r\n  <span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 12px; padding-bottom: 12px;\">Para usar o Factory pattern, inclu\u00edmos a unidade <strong>Spring.DesignPatterns<\/strong>. Em seguida, definimos a Factory pattern. Ela consiste em um valor-chave e uma fun\u00e7\u00e3o que retorna o m\u00e9todo que voc\u00ea deseja executar. Isso parece complexo, mas n\u00e3o \u00e9 t\u00e3o ruim. Em nosso caso, o valor-chave \u00e9 o status do Invoice (TInvoiceStatus). A fun\u00e7\u00e3o que queremos executar tem o Invoice (TInvoice) como par\u00e2metro. A defini\u00e7\u00e3o da Factory pattern fica assim:<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">type<\/span>\r\n  TProcessInvoiceProc = reference <span style=\"color: #000080; font-weight: bold;\">to<\/span> <span style=\"color: #000080; font-weight: bold;\">procedure<\/span>(<span style=\"color: #000080; font-weight: bold;\">const<\/span> Invoice: TInvoice);\r\n\r\n  TInvoiceProcessor = <span style=\"color: #000080; font-weight: bold;\">class<\/span>\r\n  <span style=\"color: #000080; font-weight: bold;\">private<\/span>\r\n    FFactory: TFactory&lt;TInvoiceStatus, TProcessInvoiceProc &gt;;\r\n    <span style=\"color: #000080; font-weight: bold;\">procedure<\/span> InitialiseFactory;\r\n  <span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n\r\n<span style=\"color: #000080; font-weight: bold;\">procedure<\/span> TInvoiceProcessor .InitialiseFactory;\r\n<span style=\"color: #000080; font-weight: bold;\">begin<\/span>\r\n  FFactory := TFactory&lt;TInvoiceStatus, TProcessInvoiceProc &gt;.Create;\r\n\r\n  FFactory.RegisterFactoryMethod(TInvoiceStatus.<span style=\"color: #000080; font-weight: bold;\">New<\/span>, ProcessInvoiceNew);\r\n  FFactory.RegisterFactoryMethod(TInvoiceStatus.Sent, ProcessInvoiceSent);\r\n  FFactory.RegisterFactoryMethod(TInvoiceStatus.Overdue, ProcessInvoiceOverdue);\r\n  FFactory.RegisterFactoryMethod(TInvoiceStatus.Paid, ProcessInvoicePaid);\r\n<span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 12px; padding-bottom: 12px;\">O c\u00f3digo acima j\u00e1 mostra como o Factory pattern \u00e9 criada e inicializada. Associada a cada status est\u00e1 uma fun\u00e7\u00e3o &#8220;Process&#8221;. Essa fun\u00e7\u00e3o retorna a defini\u00e7\u00e3o do TProcessInvoiceProc para executar o c\u00f3digo para esse status. Voc\u00ea define isso da seguinte forma:<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">function<\/span> TInvoiceProcessor .ProcessInvoiceNew: TProcessInvoiceProc;\r\n<span style=\"color: #000080; font-weight: bold;\">begin<\/span>\r\n  Result := <span style=\"color: #000080; font-weight: bold;\">procedure<\/span>(<span style=\"color: #000080; font-weight: bold;\">const<\/span> Invoice: TInvoice)\r\n            <span style=\"color: #000080; font-weight: bold;\">begin<\/span>\r\n              ShowMessage(<span style=\"color: #0000ff;\">'This is a new invoice'<\/span>);\r\n            <span style=\"color: #000080; font-weight: bold;\">end<\/span>\r\n<span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 12px; padding-bottom: 12px;\">\n<p>A aplica\u00e7\u00e3o do Factory pattern \u00e9 f\u00e1cil e ocorre conforme o c\u00f3digo abaixo:<\/p>\n<\/div>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #000080; font-weight: bold;\">procedure<\/span> TInvoiceProcessor .ProcessInvoice(<span style=\"color: #000080; font-weight: bold;\">const<\/span> Invoice: TInvoice);\r\n<span style=\"color: #000080; font-weight: bold;\">begin<\/span>\r\n  <span style=\"color: #000080; font-weight: bold;\">if<\/span> FFactory.IsRegistered(Invoice.Status) <span style=\"color: #000080; font-weight: bold;\">then<\/span>\r\n  <span style=\"color: #000080; font-weight: bold;\">begin<\/span>\r\n    <span style=\"color: #000080; font-weight: bold;\">var<\/span> Process := FFactory.GetInstance(Invoice.Status;)\r\n    Process(Invoice);\r\n  <span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n<span style=\"color: #000080; font-weight: bold;\">end<\/span>;\r\n<\/pre>\n<\/div>\n<div style=\"padding-top: 12px; padding-bottom: 12px;\">Dessa forma, usando o Factory pattern, seu c\u00f3digo \u00e9 f\u00e1cil de dividir e melhorar.<\/div>\n","protected":false},"featured_media":0,"parent":0,"template":"","class_list":["post-4509","knowledge","type-knowledge","status-publish","hentry","knowledge-category-arquivo-delphi"],"acf":{"author":413,"type_hero":"compact","hero_image":4500,"hero_image_position":"","hero_title":"Spring4D factory pattern","hero_content":"","hero_link":null,"hero_show_h1":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Spring4D factory pattern - GDK Software<\/title>\n<meta name=\"description\" content=\"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring4D factory pattern\" \/>\n<meta property=\"og:description\" content=\"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern\" \/>\n<meta property=\"og:site_name\" content=\"GDK Software\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-16T17:44:36+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/spring4d-factory-pattern\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/spring4d-factory-pattern\",\"name\":\"Spring4D factory pattern - GDK Software\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\"},\"datePublished\":\"2023-05-16T09:27:41+00:00\",\"dateModified\":\"2023-05-16T17:44:36+00:00\",\"description\":\"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/spring4d-factory-pattern#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/spring4d-factory-pattern\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\\\/spring4d-factory-pattern#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledgebase\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/base-de-conhecimento\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Delphi\",\"item\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\\\/knowledgebase-category\\\/arquivo-delphi\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring4D factory pattern\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br#website\",\"url\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br\",\"name\":\"GDK Software\",\"description\":\"Zet de stip op je horizon\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gdksoftware.com\\\/pt-br?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Spring4D factory pattern - GDK Software","description":"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern","og_locale":"pt_BR","og_type":"article","og_title":"Spring4D factory pattern","og_description":"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.","og_url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern","og_site_name":"GDK Software","article_modified_time":"2023-05-16T17:44:36+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. tempo de leitura":"2 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern","url":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern","name":"Spring4D factory pattern - GDK Software","isPartOf":{"@id":"https:\/\/gdksoftware.com\/pt-br#website"},"datePublished":"2023-05-16T09:27:41+00:00","dateModified":"2023-05-16T17:44:36+00:00","description":"Que op\u00e7\u00f5es existem na estrutura do Spring4D que n\u00e3o conhecemos, mas que s\u00e3o muito \u00fateis? Bem, na verdade, s\u00e3o muitos.","breadcrumb":{"@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento\/spring4d-factory-pattern#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gdksoftware.com\/pt-br"},{"@type":"ListItem","position":2,"name":"Knowledgebase","item":"https:\/\/gdksoftware.com\/pt-br\/base-de-conhecimento"},{"@type":"ListItem","position":3,"name":"Delphi","item":"https:\/\/gdksoftware.com\/pt-br\/knowledgebase-category\/arquivo-delphi"},{"@type":"ListItem","position":4,"name":"Spring4D factory pattern"}]},{"@type":"WebSite","@id":"https:\/\/gdksoftware.com\/pt-br#website","url":"https:\/\/gdksoftware.com\/pt-br","name":"GDK Software","description":"Zet de stip op je horizon","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gdksoftware.com\/pt-br?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"}]}},"_links":{"self":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge\/4509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/knowledge"}],"about":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/types\/knowledge"}],"acf:post":[{"embeddable":true,"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/team\/413"}],"wp:attachment":[{"href":"https:\/\/gdksoftware.com\/pt-br\/wp-json\/wp\/v2\/media?parent=4509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}