قانون Demeter مربك للغاية لأنني لم أستطع كتابة أساليب من أي وقت مضى

StackOverflow https://stackoverflow.com/questions/5050083

سؤال

يشعر وكأنني لقد جئت إلى طريق مسدود.إذا فهمت ذلك بشكل صحيح، إذا اتبع قانون demeter لا يمكنني أبدا تقديم طريقة تعودكائن ثم رمز العميل يجعل المكالمات إليها.أنا فقط أفكر في نمط المصنع الذي يعيد دائما كائن.نعم، هناك فصول mapper التي ترجع الكائنات.وكيف حول المجموعات؟

هل كانت مفيدة؟

المحلول

You've misunderstood the Law of Demeter and are applying it beyond the point of usefulness:

More formally, the Law of Demeter for functions requires that a method M of an object O may only invoke the methods of the following kinds of objects:

  • O itself
  • M's parameters
  • any objects created/instantiated within M
  • O's direct component objects
  • a global variable, accessible by O, in the scope of M

Factories in particular are used to create an object, and the type of object they create is part of their public interface. Thus, calling methods of an object created by a factory is allowed by the above.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top