I use the factory class to instantiate another class, such as the Product class.
How to set private variables in class Product from class factory? Should I do this?
What I plan to do is public setter methods, and then freeze or lock the instance as soon as I finish with it. But I feel that this is the wrong way to do something.
How do you approach this problem?
EDIT:
Yes, I would like to use the @derekerdmann immutable object method. But I should probably give more information first.
I am writing a parser in php for an HTML-like language, so you get nodes, which in turn can contain other nodes. Thus, the factory is a parser that creates a hierarchy of nodes. If you're interested, here is the code http: // http: //bazaar.launchpad.net/~rhlee/band-parser/dev/view/head: /src/bands.php
The fact is, I don’t know what the child nodes will be until I get off the rest of the document. Therefore, I cannot pass this to the constructor.
Sometimes I think that although I want it to be read only after parsing, why should it be? I take for example php DOMDocument parser. You can parse the HTML file and then change the structure. However, this is so that you can replay HTML with new changes. My parser is a one-way parser, so there is no need to edit the structure after parsing.
source
share