If the factory pattern contains validation logic

When using the factory pattern, should the factory itel contain the validation logic or should be left up to the calling classes to perform validation before passing the context data to?

I have a simple factory method, but it relies on a configuration tree that is passed to it to decide which object should be created.

A situation may arise when the XML configuration file can be well formed, but not in the correct format expected by the factory, and I do not know where this should be checked.

+5
source share
3 answers

Why not offer both? In this case, you transfer responsibility to the caller for whether he wants his entry to be confirmed or not.

Apache Commons - InstantiateFactory:

:

InstantiateFactory (java.lang.Class classToInstantiate)

, .

getInstance:

static Factory getInstance (java.lang.Class classToInstantiate, java.lang.Class [] paramTypes, java.lang.Object [] args)

Factory, .

+1

? , , Factory, ?

, , : , Factory, .

Factory Factory, , , , , , : Java - RuntimeExceptions, .

+1

factory, factory itel ?

:

Validate(Config). , Config . Validate Config, . .

There is no separate verification method. Instead, validation occurs within the build method when necessary. The construction method is allowed to refuse and return either a constructed object or a result indicating an error.

The second option can be well implemented using monads with almost zero code and overhead.

0
source

All Articles