I have the issue related with OOP design of my Generator class.
Currently my class contains 2 private fields for storing the results of 2 methods.
public class Generator {
private byte[] field1;
private byte[] field2;
public byte[] GenerateField1()
{
field1 = LibApi.GenerateField1();
return field1;
}
public byte[] GenerateField2(byte[] arg)
{
field2 = LibApi.GenerateField2(arg, field1);
return field2;
}
public string GetField1()
{
// converting field1 to string
return field1;
}
public string GetField2()
{
// converting field2 to string
return field2;
}
}
So I’ve severals questions related to an OOP design and a semantic naming of class/methods in my class.
Issue 1. My private fields are assigned using methods instead of the constructor. Therefore, my object can be instantiated with undefined field1 and field2. This decision allows to call methods GetField1() and GetField2() before these fields’ assignment which can cause an unexpected behavior.
Issue 2. Is it correct to call my class Generator? Should Generator be stateless static class which generate something based on given method parameters? Or the approach of storing internal state (fields) is permissible?
Should I just add guard clause which checks if field1 and field2 are not empty values before executing methods GetField1() and GetField2()?
Or the better option would be move the generation of fields in constructor method to ensure that fields will be assigned at the moment of GetFields methods calling ? However, I’m sure that Generator class’ main function is not intented to be executed implicitly in the constructor during object instantiation. So my opininon is it have to be in separate method like GenerateField2().
The main purpose of my code to generate something based on 2 arguments (field1 and arg parameter) and store both to retrieve whenever I need them: field1 used in generation and field2 – result of this generation.
1