What is the advantage of using the factory class in the following code?

Here is the code:

SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();

Why use this if you can use something more intuitive, for example:

SAXParser mySAXParser = new SAXParser();
+3
source share
5 answers

Because it allows the framework to transparently provide you with various implementations SAXParser. The implementation used may depend on the configuration parameters, the version of the framework, etc. Using factory allows framework developers, for example. replace the old, inefficient implementation of the parser with another, better one, without breaking the client code.

Note that this SAXParseris an abstract class, so you cannot even create it directly.

+7

SAXParser - , .

Factory , , SAXParser . Factory , , .

+4

SAXParser Factory ?

. : Factory SAXParser, . , , /.

mySAXParserFactory .newInstance() ?

SAXParserFactory, , (IMHO). newInstance() Factory SAXParserFactory. , Factory - , , .

" Factory", . :

+3

Parser, , . , ; factory .

, , . . . ...

+2

" , . mySAXParserFactory newInstance() ?"

, SAXParserFactory. javadoc newInstance(), , factory , API .

" ?"

. Javadoc : " factory". ( )

", : Java, , - (, .newInstance(), )?"

. , , - - . , - XML.

"factory object" "factory" , . , factory " ". ... ... , .

+1

Source: https://habr.com/ru/post/1749543/


All Articles