A
A
Alexey Nikolaev2017-10-09 18:42:59
Programming
Alexey Nikolaev, 2017-10-09 18:42:59

How to combine the factory and the law of Demeter?

Good evening.
Zayuzal once again an abstract factory, and suddenly remembered that a class method should not refer to objects that any method returned. It turns out that using factories is a necessary evil, and code with factories is a priori worse than code that follows the law of Demeter? After all, even if I just return an object from the function without calling it, another class will violate this law.
If this is a matter of convenience, what is objectively better to ignore in most cases? Are factories almost an anti-pattern to be avoided to the last? And is there a solution that allows you to sit on two chairs?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2017-10-09
@jcmvbkbc

Zayuzal once again an abstract factory, and suddenly remembered that a class method should not refer to objects that any method returned.

The factory returns an object interface that was specifically introduced to provide generic access to the different types of objects that the factory creates. Factory users only interact with these interfaces, not with the objects themselves. That. factory users do not depend on modules that implement specific objects. The Law of Demeter is just what is needed in order to reduce the engagement between the modules. Follow the spirit of the law, not the letter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question