Help with factory design  
Author Message
vb2005





PostPosted: Architecture General, Help with factory design Top

Hi,
I am working on a small project where I need to implement barcode. I was considering the factory pattern since I am dealing with multiple barcodes UPCA, EAN13 etc. I am really having a problem getting this pattern. Here is my code

Public Interface IBarcode

Function CalculateChecksumDigit(ByVal sBarcode As String) As Integer

ReadOnly Property CheckSumDigit() As Integer

End Interface

Public Interface IUpc

Inherits IBarcode

Property ManufactureCode() As String

Property ProductCode() As String

Property ProductType() As String

End Interface

Public Interface IEan

Inherits IBarcode

Property CountryCode() As String

Property ManufactureCode() As String

Property ProductCode() As String

End Interface

Public Class BarcodeUPCA12

Implements IUpc

Private _productType As String

Private _manufacturerCode As String

Private _productCode As String

Private _checksumDigit As String

Sub New()

_productType = String.Empty

_manufacturerCode = String.Empty

_productCode = String.Empty

_checksumDigit = String.Empty

End Sub

Public Function CalculateChecksumDigit(ByVal sBarcode As String) As Integer Implements IUpc.CalculateChecksumDigit

_productType = sBarcode.Substring(0, 1)

_manufacturerCode = sBarcode.Substring(1, 5)

_productCode = sBarcode.Substring(6, 5)

do calc....

Return Me._checksumDigit

End Function

Public ReadOnly Property CheckSumDigit() As Integer Implements IUpc.CheckSumDigit

Get

Return _checksumDigit

End Get

End Property

Public Property ManufactureCode() As String Implements IUpc.ManufactureCode

Get

Return _manufacturerCode

End Get

Set(ByVal value As String)

_manufacturerCode = value

End Set

End Property

Public Property ProductCode() As String Implements IUpc.ProductCode

Get

Return _productCode

End Get

Set(ByVal value As String)

_productCode = value

End Set

End Property

Public Property ProductType() As String Implements IUpc.ProductType

Get

Return _productType

End Get

Set(ByVal value As String)

_productType = value

End Set

End Property

End Class

Public Enum BarcodeType

UPCA12

EAN13

End Enum

Public Class BarcodeFactory

Public Shared Function CreateBarcode(ByVal barcodeType As BarcodeType) As IBarcode

Select Case barcodeType

Case Barcode.BarcodeType.UPCA12

Return New BarcodeUPCA12

End Select

Return Nothing

End Function

End Class

Public Class BarcodeEvaluator

Public Shared barcode As IBarcode

Public Function CalculateChecksumDigit(ByVal sBarcode As String) As Integer

Select Case sBarcode.Length

Case 14 Or 17

barcode = BarcodeFactory.CreateBarcode(BarcodeType.UPCA12)

Return barcode.CalculateChecksumDigit(sBarcode)

End Select

End Function

Public Function Test() As String

Return "Hello"

End Function

End Class

CLIENT USE -------------------------------------------------------------------------------

Imports Barcode

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim barcode As New BarcodeEvaluator

MsgBox(barcode.CalculateChecksumDigit("1233492213916"))

End Sub

End Class

First off it doesn't work. Second - Is this how it should be done

Please advice. I am really trying to get this pattern thing.



Architecture3  
 
 
Sarit Kommineni





PostPosted: Architecture General, Help with factory design Top

I think you need to do as below.

1. Define a single interface IBarCode

2. Implement a single Provider which creates the instance of the barcode you are looking for.

3. Implement the UPCA, EAN13 as Classes which implement the methods in the interface specific to the standards and in addition implement the properties and classes specific to the standard.

Hope this helps.