java - How do you use anonymous objects with the factory pattern? -
i have method so:
public class foofactory { public foo createnewfoo(){ return new foo(); } } now if this:
foofactory foofactory = new foofactory(); foo foo = foofactory.createnewfoo(); it'll work fine. however, if try :
new foo() = foofactory.createnewfoo(); it doesn't seem work @ all. says "variable expected".
i understand new foo() in itself, creates new foo object, if use factory, should override anonymous object new foo object.
i've tried creating arraylist holds foo's , doing
arraylist.add(new foo()); arraylist.get(0) = foofactory.createnewfoo(); it still says "variable expected". why saying that?
foo foo = new foo(); foo otherfoo = foo; this works fine, don't understand why can't make factory work anonymous object.
i tried searching online, got no search results, tells me i'm making ridiculous mistake/using factory pattern wrong.
equals assignment operator.
targetofmyassignment = thingimassigning; new foo() statement creates object. it producer. can't assign it, it's not variable reference. variable references, foo foo =, consumers. arraylist.get(0) producer. statement, constructor, provides value, not reference assign to. arraylist.add(object) consumer.
i think misunderstand anonymous type is; anonymous type 1 override or of it's behavior in-line, specifying new behavior after class declaration {}. example:
runnable r = new runnable() { public void run() { // code } }; you need anonymous type because runnable interface, there's no behavior defined run(), runnable r = new runnable(); won't compile.
Comments
Post a Comment