How do you use anonymous objects with a factory pattern?

I have a way like this:

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

      

Now if I do this:

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

      

Everything will be fine. However, if I try to do this:

new Foo() = FooFactory.createNewFoo();

      

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

I realize that new Foo()

it creates a new Foo object by itself, but even if I use a factory, it should just override the anonymous object with the new object Foo

.

I also tried to create ArrayList

one that contains Foo and does

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

      

It still says "variable expected". Why is this so?

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

      

This works great, so I don't understand why I can't get the factory to work with an anonymous object.

I tried searching for this online, but I didn't have a search result that says I'm probably mistaken / using a factory pattern.

+3


source to share


3 answers


Equals is the purpose of operator .

targetOfMyAssignment = thingImAssigning;

      

new Foo()

is the operator that creates the object. This is the manufacturer . You cannot assign anything to it, it is not a variable reference. Variable references such as Foo foo =

are consumers . arraylist.get(0)

is also a manufacturer. this operator, like a constructor, provides a value, but it is not a reference for you to assign something. arraylist.add(object)

is also a consumer.




I think you also misunderstood what an anonymous type is ; an anonymous type is one where you override some or all of the behavior on a string , defining the new behavior after declaring the class with {}

. For example:

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

      

You need an anonymous type because it Runnable

is an interface, run()

no behavior is defined for, so Runnable r = new Runnable();

it won't compile.

+5


source


You are wrong here. To create and hold an object, you need a link (left) and a command to create your object (right). You can do this either with a new keyword or by calling a method (maybe a factory method).

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

      

Secondly

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

      



is incorrect because you are retrieving an object and not a link and then you want to assign an object to an object and not a link.

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

      

This will add the object at position 0 using the Foo object created by the factory method.

0


source


Factory templates often include a static

create method . This means that the create method is not specific to a single instance of a factory object, but is called in the context of the class itself. Instead, the factory rolls out instances of objects of the type you want to create.

Okay, usually factories will pump out objects that implement the interface you are interested in. You don't have to worry about the specific types of objects that the factory rolls out, in this case. In this way, factories facilitate interface-based programming. But I'm distracted.

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

The stuff about the left side of the equality operator, which should be a reference, is better explained by other answers.

0


source







All Articles