Post by rubina9898 on Dec 25, 2023 5:27:57 GMT
A Singleton. An example implementation of a static factory in the case I am discussing could look like this. class Invoice Factory private static generated Invoices Set new Set public static createInvoice params InvoiceParams IInvoice if InvoiceFacto eneratedInvoices.has param nvoiceId throw new Error 'invoiceId already used!' switch case 'PL' return this._createInvoicePL params case 'USA' return this._createInvoiceUSA params case 'SAU' return this._createI nvoiceSAU params default throw new Error 'Unsupported country!' private.
Creat invoice InvoicePL new InvoicePL params return i ate private _create InvoiceUSA params InvoiceParams InvoiceUSA const Phone Number List invoice InvoiceUSA new InvoiceUSA params return invoic eate private _createInvoiceSAU params InvoiceParams InvoiceSAU const invoice InvoiceSAU new InvoiceSAU params return invoi reate const invoice IInvoice InvoiceFacto reateInvoice country 'PL' ...params Having a list of generated invoices in the factory allows you to easily avoid duplicates. An alternative to this solution would be to create a facility responsible for controlling invoice numbers and inject it into factories.
Each factory instance would have to query the Singleton if it wanted to generate an invoice. Static factories are equally useful for various types of classes with helpers. In this case there is no point in creating an instance of the class every time you use it. It is definitely easier to use a static method HelperClass.doX params e.g. Abstract Factory The presented invoice generation system was implemented on a production server and it turned out to work so well that it was decided to expand it. In addition to generating invoices you will need to generate sales reports for individual customers.
Creat invoice InvoicePL new InvoicePL params return i ate private _create InvoiceUSA params InvoiceParams InvoiceUSA const Phone Number List invoice InvoiceUSA new InvoiceUSA params return invoic eate private _createInvoiceSAU params InvoiceParams InvoiceSAU const invoice InvoiceSAU new InvoiceSAU params return invoi reate const invoice IInvoice InvoiceFacto reateInvoice country 'PL' ...params Having a list of generated invoices in the factory allows you to easily avoid duplicates. An alternative to this solution would be to create a facility responsible for controlling invoice numbers and inject it into factories.
Each factory instance would have to query the Singleton if it wanted to generate an invoice. Static factories are equally useful for various types of classes with helpers. In this case there is no point in creating an instance of the class every time you use it. It is definitely easier to use a static method HelperClass.doX params e.g. Abstract Factory The presented invoice generation system was implemented on a production server and it turned out to work so well that it was decided to expand it. In addition to generating invoices you will need to generate sales reports for individual customers.