|
Post by rubina9898 on Dec 25, 2023 0:27:50 GMT -5
Additional countries the factory should be expanded with additional methods and a condition in the method createInvoice. Factory method The simple factory presented in the earlier part of the entry has a fundamental problem. In addition to generating invoice elements specific to the customer's country of origin there is a portion of the invoice that will be identical regardless of the customer's country of origin. The Manufacturing Method design pattern will be used to further simplify the code. Thanks to the generation method it is possible to define a common part for the generated invoice and parts specific to specific subclasses. abstract class Invoice protected abstract format Phone Number List invoice IInvoice IInvoice public create IInvoice return this.format this._generateInvoice class InvoicePL extends Invoice protected format invoice IInvoice IInvoice invoice. Direction 'ltr' invoice.currency 'PLN' invoice.dateFormat 'DD MM YYYY' return invoice class InvoiceFactory public createInvoice params InvoiceParams IInvoice switch params.country case 'PL' return this._createInvoicePL params case 'USA' return this._createInvoiceUSA params case 'SAU' return this._createInvoiceSAU params default throw new Error 'Unsupported country!' private createInvoice.
|
|