Factory methods and private variables

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.

+5
source share
3 answers

A typical way to do this is to create immutable objects . In an immutable object, all private fields are set by passing values ​​to the constructor of the object. Unchangeable == unmodifiable.

, setter .

Java-:

class ImmutableDuck {

    private final String name;
    private final int age;

    /**
     * Constructor - sets the read-only attributes of the duck
     */
    public ImmutableDuck( String name, int age ){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }

    /**
     * Creates a new ImmutableDuck with the current object age 
     * and the given age
     */
    public ImmutableDuck setName( String name ){
        return new ImmutableDuck( name, age );
    }

}
+3

, factory @derekerdmann . , @rhlee, - "" , key. php:

class Thing {
  private $key = '';
  private $name = '';

  public function __construct($key) {
    $this->key = $key;
  }

  public function setName($key, $name) {
    $unlocked = ($this->key == $key);
    if ($unlocked) $this->name = $name;
    return $unlocked;
  }
}
0

- private-private.

Java, "x.y.z" .

Thing.java:

package x.y.z;

public class Thing {

    private int something;

    Thing(final int something) {
        this.something = something;
    }

    void setSomething(final int something) {
        this.something = something;
    }

    public int getSomething() {
        return something;
    }
}

ThingFactory.java:

package x.y.z;

public class ThingFactory {

    public static Thing createThing() {
        Thing thing = new Thing(1337);
        thing.setSomething(9001);
        return thing;
    }
}

/ .

0

All Articles