Henry98
Henry98

Reputation: 571

How do you use anonymous objects with the factory pattern?

I have a method as so:

public class FooFactory {
    public Foo createNewFoo(){
        return new foo();
    }
}

Now if I do this:

FooFactory fooFactory = new FooFactory();
Foo foo = FooFactory.createNewFoo();

It'll work perfectly fine. However, if I try to do this :

new Foo() = FooFactory.createNewFoo();

It doesn't seem to work at all. It says "variable expected".

I understand that new Foo() in itself, creates a new Foo object, but even if I use the factory, it should just override the anonymous object with a new Foo object.

I've also tried creating an ArrayList that holds Foo's and doing

arrayList.add(new Foo());
arrayList.get(0) = FooFactory.createNewFoo();

It still says "variable expected". Why is it saying that?

Foo foo = new Foo();
Foo otherFoo = foo;

This works perfectly fine, so I don't understand why I can't make the factory work with an anonymous object.

I tried searching for this online, but I got no search results, which tells me that I'm probably making some ridiculous mistake/using the factory pattern wrong.

Upvotes: 3

Views: 1056

Answers (3)

Greg
Greg

Reputation: 946

Factory patterns often involve a static create method. This means at a high level that the create method is not specific to a single factory object instance, but are called in the context of the class itself. The factory, instead, pumps out instance objects of the type you want it to create.

Well, usually factories will pump out objects that implement an interface you care about. You are not supposed to care about the concrete types of objects that the factory pumps out, in that case. In this way, factories facilitate interface based programming. But I digress.

Bottom line, if you are calling a static method on a class, you don't need to instantiate an object of that class type first.

The stuff about the left hand side of the equals operator needing to be a reference is better explained by the other answers.

Upvotes: 0

Beri
Beri

Reputation: 11620

You are getting Java wrong here. In order to create and hold an object, you need an Reference (on your left) and a command that will produce your object (on your right). You can do this either by new keyword, or by a method call (can be a factory method).

Foo f1 = new Foo();
Foo f2 = FooFactory.createNewFoo();

Secondly

arrayList.get(0) = FooFactory.createNewFoo();

is wrong, because you are retrieving an object, not a reference, and then you want to assign an object to an object, not a reference.

arrayList.add(0, FooFactory.createNewFoo()); 

This will add object in position 0 with Foo object created by factory method.

Upvotes: 0

durron597
durron597

Reputation: 32343

Equals is an assignment operator.

targetOfMyAssignment = thingImAssigning;

new Foo() is statement that creates an object. It is a producer. You can't assign anything to it, it's not a variable reference. Variable references, like Foo foo =, are consumers. arraylist.get(0) is also a producer. that statement, just like a constructor, provides a value, but it is not a reference for you to assign something to. arraylist.add(object) is also a consumer.


I think you also misunderstand what an anonymous type is; an anonymous type is one where you override some or all of it's behavior in-line, by specifying the new behavior after the class declaration with {}. For example:

Runnable r = new Runnable() {
  public void run() {
    // code
  }
};

You need an anonymous type because Runnable is an interface, there's no behavior defined for run(), so Runnable r = new Runnable(); won't compile.

Upvotes: 5

Related Questions