|
-
Sep 24th, 2015, 05:32 AM
#1
Thread Starter
Hyperactive Member
[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:
Class Sensor Class Sample ' Simple class that holds sample data End Class Class Record Private _mode As RecordingMode Private _samples As List(Of Sample) Private _capacity As UInteger Public Property Supply As Double ' Example parametric information Public Property Temperature As Double Public Property Mode() As Sensor.Mode Get Return _mode End Get Set(value As Sensor.Mode) _mode = value If _mode = Mode.Foo Then _capacity = 512 Else _capacity = 256 End If End Set End Property Public ReadOnly Property Samples As List(Of Sensor.Sample) Get Return _samples End Get End Property Public Sub New(mode As Sensor.Mode) Me.Mode = mode _samples = New List(Of Sample)(_capacity) End Sub Public Sub AddSample(ByVal sample As Sensor.Sample) ' Do some data manipulation here... then: Me.Samples.Add(sample) 'Add the sample record to the record End Sub End Class Public Function ReadMode() As Sensor.Mode ' Read mode from device's current record End Function Public Function ReadSupply() As Double ' Read supply from device's current record End Function Public Function ReadTemperature() As Double ' Read temperature from device's current record End Function Public Function ReadSample() As Sensor.Sample ' Read sample data from device's current record End Function Public Function GetRecord() As Record Dim mode As Sensor.Mode = ReadMode() 'Check the mode of the device Dim Record As New Record(mode) 'Create a new record with the correct sample capacity ' Do some work here to get the device to load the record from storage Record.Supply = ReadSupply() Record.Temperature = ReadTemperature() Return Record End Function End Class
The code for the GUI timer elapsed handler is something like:
VB Code:
Dim Record As Sensor.Record = Sensor.GetRecord() While Samples LeftInRecord Dim Sample As Sensor.Sample = ReadSample() Record.AddSample(Sample) ' Do some work to display the data here... End While 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|