Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:32+00:00 2026-05-26T05:35:32+00:00

Law of Demeter expects to make the loosest coupling between classes. This implies that

  • 0

Law of Demeter expects to make the loosest coupling between classes.

This implies that 90% of all getters/setters exposing in class must be “deleted” and replaced by “behavior-contained” method. Indeed, it corresponds to “tell, don’t ask” philosophy because client is not expected to treat behavior itself through help of poor getter/setter methods.
This reduces duplicated code also if the same action is used elsewhere.

This implies huge classes with many many behavioral methods and overuse of delegation if we want to respect Single-Responsibility principle.

On the other hand, visitor pattern definition is :

Visitor lets you define a new operation without changing the classes of the elements on which it operates.

So, at first sight, it seems to be the contrary of Law of Demeter expectations :

  • One (Visitor) implies class structure to provide getter/setter so that Visitor can modify object’s states without touching to class itself.

  • Other (Demeter) encourages to enclose all behavioral codes directly related to object in the same class.

So my question is :

When can we consider that a class is closed for modification and thus stop adding behavioral methods on it and so prefer to add them in a newly created Visitor with the great risk that client use getters/setters instead of behavioral methods already exposed before in initial class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:35:32+00:00Added an answer on May 26, 2026 at 5:35 am

    The short answer is no.

    First, I don’t think “tell, don’t ask” says that you should remove all your getters and setters, but that you should remove them if they don’t add any value or expose the internal state. So, as an example, getters should try as much as possible to return immutable data. An example with setters is adjusment or policy objects, these are objects that are not needed for an instance to work properly, but if provided, they change the behaviour.

    Second, I’ve never seen a description of the visitor pattern that implies that the visited object should have getters and setters, so the visitor can modify them. As with any other object, the idea is to use the visited object public API to do any extension. Implying otherwise definitely goes against encapsulation too.

    On another subject, I’m a bit confused about your last paragraph, as I don’t know if you’re talking about the open/closed principle, or about how to build a feature using the visitor pattern.

    One note, I think the key is to understand that SOLID, the Law of Demeter and all other practices are good practices rather than best practices (‘best practices’ is a marketing term). If you take any of these practices to the extreme, they will probably end up hurting the readability or maintainability of the code.

    (by the way, nice question :D)


    The benefit of the open/closed principle applies mostly to code that is going to be used by other people in ways that we can’t really anticipate (frameworks are an example of this). So if you’re writing a framework, you’ll need to add extension points and use a language feature to prevent the class be inherited (such as final in java or sealed in C#) or by just letting the developer override certain methods. The idea is to prevent a naive user from overriding an important bit of an object and making the framework break in unexpected ways. Some languages/frameworks laugh at this (e.g Ruby/Rails) and they encourage the user to open the classes to add or modify features (with quite some success).

    If you’re writing an application (and you own the code), I would suggest you not pay too much attention to the open/closed principle, and focus on applying the Law of Demeter (to a sane extent :D).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Law of Demeter indicates that you should only speak to objects that you
I've been reading up on this Law of Demeter thing, and it (and pure
I have few questions. First of all does code below violates law of Demeter?
how to evade Silverlight cross-domain security law? This point is one that I need
The Law of Demeter (really should be the suggestion of Demeter) says that you
I basically need the answer to this SO question that provides a power-law distribution
The wikipedia article about Law of Demeter says: The law can be stated simply
The following code breaks the Law of Demeter : public class Student extends Person
I want to implement a version of Benford's law ( http://en.wikipedia.org/wiki/Benford%27s_law ) that basically
According to the Law of Demeter , can you call methods on returned objects?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.