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 IntegerEnd
Interface
Public
Interface IUpc Inherits IBarcode Property ManufactureCode() As String Property ProductCode() As String Property ProductType() As StringEnd
Interface
Public
Interface IEan Inherits IBarcode Property CountryCode() As String Property ManufactureCode() As String Property ProductCode() As StringEnd
Interface
Public
Class BarcodeUPCA12 Implements IUpc Private _productType As String Private _manufacturerCode As String Private _productCode As String Private _checksumDigit As StringSub 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 PropertyEnd
Class
Public
Enum BarcodeTypeUPCA12
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 FunctionEnd
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 17barcode = BarcodeFactory.CreateBarcode(BarcodeType.UPCA12)
Return barcode.CalculateChecksumDigit(sBarcode) End Select End Function Public Function Test() As String Return "Hello" End FunctionEnd
ClassCLIENT USE -------------------------------------------------------------------------------
Imports
BarcodePublic
Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim barcode As New BarcodeEvaluatorMsgBox(barcode.CalculateChecksumDigit(
"1233492213916")) End SubEnd
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.

Help with factory design
Fabrikx74
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.