How to combine the factory and the law of Demeter?

Good evening.
Zayuzat once again abstract factory, and suddenly remember that a class method should not refer to objects which were returned by any method. So, to use a factory is forced evil, and code factories, a priori, worse than code that follows the law of Demeter? Even if I just return the object from a function without calling it, this law will violate a different class.

If it's a matter of convenience, the fact that in most cases is objectively better to ignore? Whether factory almost antipatterns that should be avoided to the last? And is there any solution to sit on two chairs?

Thanks in advance.
June 14th 19 at 18:01
1 answer
June 14th 19 at 18:03
Zayuzat once again abstract factory, and suddenly remember that a class method should not refer to objects which were returned by any method.

The factory returns the object's interface, which was specially introduced to provide generalized access to different types of objects created by the factory. Factory users interact with interfaces, not with the objects themselves. Thus the user of the factory does not depend on the modules implementing the specific objects. The law of Demeter is needed in order to reduce the gearing between the modules. Follow the spirit of the law, not the letter.

Find more questions by tags Patterns of designingProgramming