netdis
netdis

Reputation: 323

Factory Pattern implementation coupled with reading and writing

I'm trying to design application in right manner, it should

  1. Read invoice data from SQL Server (2 queries depending on type of invoice: sales or purchase)
  2. Process it (Acme may need less fields than SugarCorp and in different formatting)
  3. Output txt or csv (that may change in future)

I found factory pattern helpful so prepeared a UML diagram according to my concern.

UML diagram

Each InvoiceFactoryProvider can generate PInvoice or SInvoice (specific to them). CreatePInvoice() and CreateSInvoice() should call load() and save() methods.

How to couple load() with SQLReader class to get each row as PInvoice object? And save() with my IDataWriter interface. Could you provide some example / advice?

Edit:

After reviewing examples of Bridge Pattern, as Atul suggested, I created a class diagram for this problem using it, which looks like this:

UML diagram - Bridge Pattern

Invoice SQL queries may vary (application may load invoice data from different systems - PollosInvoice or StarInvoice) and how they are processed (different implementations).

In this case I decoupled abstraction - Invoice from its implementation - exporting invoice to certain software (AcmeExporter or SigmaExporter). AcmeExporter and SigmaExport will set their fields according to specification - date of transaction, payment method, invoice type etc. taken from Invoice's DataTable. ExportInvoice() will return DataTable with needed data. InvoiceExporter is also using two interfaces for encoding and file format.

What do you think about it? What kind of flaws / advantages does it have?

Upvotes: 1

Views: 1645

Answers (1)

Atul
Atul

Reputation: 1774

Currently it looks like you are using Abstract Factory design pattern for the creation of your products (invoices). But point to note is that your load and save method are inside product(invoice) so in that its always better to go with Bridge Design Pattern. Your product will use the implementation of Reader and Writer to load and save the records.

Note: You would be able to use AbstractFactory even with this design pattern.

It will looks something like below... (just an Analogy)

enter image description here

Upvotes: 1

Related Questions