Factory template in java

I worked with java code that supposedly uses the Factory pattern, but I'm not completely convinced of the pattern.

My code does this:

// the factory
class SomeFactoryImpl {
   Set<SomeClass> getSomeListOfObjects();
}

And somewhere in the code:

{ ...
    SomeFactory factory = new SomeFactoryImpl();
    Set<SomeClass> list = factory.getSomeListOfObjects();
}

What I'm thinking about, if Factory classes do not have a static create () method, then you need to create a factory instance, which IMO should be as complex as creating an instance of the object itself.

I do not consider it an argument that such a Factory can return collections of created objects, is good enough. I believe there may be cleaner workarounds if you need to create a Factory instance before actually creating objects from the factory.

I feel that it is better if the create method is a static method of the Factory class. But I am also sure that my opinion is not entirely β€œright.”

SO , Factory , ?

, , : FactoryMethodPattern, FactoryMethod CreationMethod .

+5
5

factory .

,

{ ...
    SomeFactory factory = new SomeFactoryImpl();
    Set<SomeClass> list = factory.getSomeListOfObjects();
}

:

public ThisClass(SomeFactory someFactory) {
    this.factory = someFactory;
}   

...

{ ...
    Set<SomeClass> list = factory.getSomeListOfObjects();
}

:

  • factory, SomeFactoryImpl, SomeFactory.
  • factory, , , , , , . factory , .
  • , , , , , - , - , .

, ...

+5

, - , , factory . , ( ).

, factory ( ...), , , factory -Instance .

+1

, factory, :

" objet, , . factory ."

, , SomeFactoryImpl , - . , , , , . , swing, , VM. JPanel, , , , , factory factory ( JPanel, ). , . (, wikipedia, ) factory, , factory , " ", .

, , , . , , , factory , .

- , , ( , ), . istantiate objects.. - ( ).

+1

factory

  • factory, ( ..), , . , RedWidgetFactory, BlueWidgetFactory. () .
  • , factory ( ), . , , , ( ). , , (, factory , )
0

IMO, , , GoF Factory, . , GoF (SomeFactory, SomeFactoryImpl) (SomeClass), .

API, , , , , . :

  • ( "factory", ), factory (, DriverManager JDBC) .
  • - .

# 1, JDBC, :

  • Driver Factory
  • Connections, Statements ..
  • DriverManager ( GoF) - , factory URL- JDBC,

( DriverManager , getConnection(...), ).

, , , JDBC,

new OracleDriver().connect(...)

, , factory.

, , , .

, .

0

All Articles