Results 1 to 6 of 6

Thread: [RESOLVED] Help with factory design pattern

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] Help with factory design pattern

    Hi Folks

    This turned into a pretty long post so the TLDR version is: I'm a noob at using OOP patterns. Should I, and how can I, adapt my code (below) to use the factory pattern?

    The longer version:
    Im a noob at OOP concepts in practice (I usually do C firmware for 90% of my work), but have learned VB, as I went along and as I needed to. I am trying to use the language more correctly, wrt patterns and practices, etc, as I find time to fit researching them into my workflow. Prob not ideal, but its the time I have available. Consequently I am unsure if I should use a pattern to simplify the current situation I have.

    Background:
    At the moment I have a VB.Net 4.5 2010 forms application that talks to a physical sensor device. This device creates and stores records, where each record may be created due to certain differing conditions and each contains a number of measurement samples and some parameter information.

    Currently a user would set the device started from a GUI button, which also starts a timer. As the timer elapses the handler checks if there are any new records on the device, if there are it gets the data from each record on the device. It then continues on until the user indicates, via the GUI, that the device should stop.

    I have used a class Sensor to handle the all of the too-ing and fro-ing so that the GUI only handles the buttons, the timer and the ultimate display of the retrieved data.

    The Sensor class currently contains a Record class. When the GUI's timer elapsed handler wants to operate it asks for a new record by calling Sensor.GetRecord(mode As Sensor.RecordingMode) which sets instantiates a Record object and reads the parametric information from the record on the device. The GUI then starts a loop that calls Sensor.GetSample() to get a number of samples from the record on the device, adding each one to the Record object (Record.AddSample(sample As Sensor.sample)). When all the samples are read the GUI adds the record to a list of records in the Sensor object, Sensor.AddRecord(record As Sensor.Record)

    The parametric information read in GetRecord() is the same for all records. The number of samples a record contains however, changes depending on the Mode, it could be 512 or 256 samples.

    The Sensor and Record classes
    VB Code:
    1. Class Sensor
    2.     Class Sample
    3.         ' Simple class that holds sample data
    4.     End Class
    5.  
    6.     Class Record
    7.         Private _mode As RecordingMode
    8.         Private _samples As List(Of Sample)
    9.         Private _capacity As UInteger
    10.  
    11.         Public Property Supply As Double ' Example parametric information
    12.         Public Property Temperature As Double
    13.  
    14.         Public Property Mode() As Sensor.Mode
    15.             Get
    16.                 Return _mode
    17.             End Get
    18.             Set(value As Sensor.Mode)
    19.                 _mode = value
    20.                 If _mode = Mode.Foo Then
    21.                     _capacity = 512
    22.                 Else
    23.                     _capacity = 256
    24.                 End If
    25.             End Set
    26.         End Property
    27.  
    28.         Public ReadOnly Property Samples As List(Of Sensor.Sample)
    29.             Get
    30.                 Return _samples
    31.             End Get
    32.         End Property
    33.  
    34.         Public Sub New(mode As Sensor.Mode)
    35.             Me.Mode = mode
    36.             _samples = New List(Of Sample)(_capacity)
    37.         End Sub
    38.  
    39.         Public Sub AddSample(ByVal sample As Sensor.Sample)
    40.             ' Do some data manipulation here... then:
    41.             Me.Samples.Add(sample) 'Add the sample record to the record
    42.         End Sub
    43.     End Class
    44.  
    45.     Public Function ReadMode() As Sensor.Mode
    46.         ' Read mode from device's current record
    47.     End Function
    48.     Public Function ReadSupply() As Double
    49.         ' Read supply from device's current record
    50.     End Function
    51.     Public Function ReadTemperature() As Double
    52.         ' Read temperature from device's current record
    53.     End Function
    54.     Public Function ReadSample() As Sensor.Sample
    55.         ' Read sample data from device's current record
    56.     End Function
    57.  
    58.     Public Function GetRecord() As Record
    59.         Dim mode As Sensor.Mode = ReadMode() 'Check the mode of the device
    60.         Dim Record As New Record(mode) 'Create a new record with the correct sample capacity
    61.        
    62.         ' Do some work here to get the device to load the record from storage
    63.  
    64.         Record.Supply = ReadSupply()
    65.         Record.Temperature = ReadTemperature()
    66.         Return Record
    67.     End Function
    68. End Class

    The code for the GUI timer elapsed handler is something like:
    VB Code:
    1. Dim Record As Sensor.Record = Sensor.GetRecord()
    2.     While  Samples LeftInRecord
    3.         Dim Sample As Sensor.Sample = ReadSample()
    4.         Record.AddSample(Sample)
    5.         ' Do some work to display the data here...
    6.     End While
    7.     Sensor.AddRecord(Record)

    I have been trying to tidy up how this happens and make it more flexible as the Record class and GetRecord() are all within the Sensor class and file and bugs are creeping in to how the list of records is set up and handled. As I'm using a "Get" function I thought maybe this would be somewhere I could use a factory pattern, thus I duly checked MSDN for "factory pattern". This only turned up retired content: https://msdn.microsoft.com/en-us/library/ee817667.aspx which, for lack of anything else concrete, I decided to attempt to follow.

    The Questions:
    1: Is this a suitable place to use the factory pattern?
    I'm in two minds because it seems like the Sensor class is assembling the record class for the GUI, but apart from the Mode parameter and how it affects the samples capacity, the object produced does not change; it is always a Record

    2: Is there a better place to learn the recommended way to implement and use this pattern? As mentioned the MSDN article is retired (it's from 2002!) and doesn't go into a lot of detail on why certain things are done.

    3: How would the actions in my current code be split up to properly use the factory pattern? I'm somewhat confused with all the different classes and abstract classes the article shows and just not sure which parts should do what in my context

    Thanks!
    Last edited by wolf99; Sep 24th, 2015 at 06:00 AM.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width