diff --git a/content/docs/patterns/template-method-pattern/index.md b/content/docs/patterns/template-method-pattern/index.md index 8a5edc92..27663738 100644 --- a/content/docs/patterns/template-method-pattern/index.md +++ b/content/docs/patterns/template-method-pattern/index.md @@ -1,6 +1,6 @@ --- title: "Template Method Pattern" -tags: ["AL"] +tags: ["AL", "Interface", "Readability"] categories: ["Pattern"] --- @@ -144,114 +144,6 @@ codeunit 50002 ExportOrders } ``` - - -## Benefits -The logical flow is very easy to adopt, it is even possible to add entries to the queue while it is processed. - -## Example - -In this short example I show you how to post multiple sales orders and display message after finishing the last post. - - -We have two commands in this example, the "SalesOrderPostCommander" is used to post a sales order and the "MessageCommander" displays a message. -```al -codeunit 50104 "SalesOrderPostCommander" implements ICommand -{ - procedure SetSalesOrderNumber(value : Code[20]) - begin - No := value; - end; - - procedure Execute() - begin - // TODO Post Sales Header - end; - - - var - No : Code[20]; -} - - -codeunit 50103 "MessageCommander" implements ICommand -{ - procedure SetText(value: Text); - begin - t := value; - end; - - procedure Execute() - begin - Message(t); - end; - - var - t: Text; -} -``` - -Using this two codeunits we can now implement a patch posting -```al - -codeunit 50105 PatchPostQueue -{ - procedure PatchPost() - begin - FilterSalesOrdersToPost(); - if not SalesOrders.Findset(false) then - exit(); // Nothing to post - - repeat - AddSalesOrderToQueue(SalesOrder."No."); - until SalesOrders.Next() = 0; - - AddMessageToQueue('Posting Complete'); - ExecuteQueue(); - end; - - local procedure ExecuteQueue() - var - object : interface "ICommand"; - begin - repeat - object := queue.Pop(); - object.Execute(); - until queue.GetSize() = 0; - end; - - local procedure FilterSalesOrdersToPost() - begin - // Filter Sales Orders here - end; - - local procedure AddMessageToQueue(message : Text); - var - t: Codeunit MessageCommander; - object: Interface ICommand; - begin - t.SetText(message); - object := t; - queue.Push(object); - end; - - local procedure AddSalesOrderToQueue(No : Text); - var - SaleOrderCommander: Codeunit SalesOrderPostCommander; - object: Interface ICommand; - begin - SaleOrderCommander.SetSalesOrderNumber(No); - object := SaleOrderCommander; - queue.Push(object); - end; - - - var - SalesOrders : Record "Sales Header"; - queue: Codeunit Queue; -} -``` - ## Benefits Your code gains readability and it is very easy to add new cases for the template. You don't always have to think about the whole logic. You just have to implement the details