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

  • SEARCH
  • Home
  • 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 8928193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:22:50+00:00 2026-06-15T08:22:50+00:00

Lets say we have an object (A), which should execute a method of another

  • 0

Lets say we have an object (A), which should execute a method of another object (B) which is not in the scope of the object (A) itself, nor it is in the scope of its members.

The overcome this problem I might one of the following methods:

  • Store a reference to the object with in any object that might use it, might led to a memory leak

  • Create getters for any object that I might need to use and use them like that (which is probably not the best practice).

    anObject.getABC().getDEF().getHIG().DoSomething();
    
  • Define some of the object as a single tone, this might be a good solution but whenever I use a libery overrding it might be a pain, its subclasses wont work with the over ridden one.

  • static members, probably the worst way.

My question: What is the best way to do it, and what should be taken into consideration, while maintain object oriented code?

(The questions refers to general OOP, however I prefer code examples in JAVA)

Examples:

  • users clicks something on the menu this should affect something that is not related to the menu nor anything closely related to it.for

  • A class that has a consume method which should free its memory should access objects that might hold is reference but not directly related to it.For instance, there is a car object which is drawn on a canvas should delete itself from it and from array that hold a list of cars that doesn’t have any relational to the GUI thread.

  • 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-06-15T08:22:51+00:00Added an answer on June 15, 2026 at 8:22 am

    The Law of Demeter says: Don’t talk to strangers.

    If A needs to call a method of B, then B should be a direct dependency of A. If B is in fact not a direct dependency of A, but is a transitive dependency, i.e. A depends on C, which depends on D, which depends on B, then A should call a method of C, which would call a method of D, which would call the method of B:

    A --> C --> D --> B
    

    Reading your examples, it looks like you’re looking for the Observable/Observer method. For example: when you click on a menu, the menu should just call a method of its observers (or listeners, in Swing/AWT terminology), which are instances of an interface. The listener would register itself to the menu, in order to be notified of a menu click.

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

Sidebar

Related Questions

Lets say that I have one component which is doing something with Workbook object
Let's say I have a WCF service which has a method returning object Person.
Lets say i have a ajax method which call a script that checks if
Lets say I have a List<object> which is passed into a class as an
I have a class, lets say CAppPath which has a static method: public: static
I have a method which returns an array of fixed type objects (let's say
Lets say I have an object with two boolean properties: public bool AdvancedMode{ get;
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have a question regarding events in c#. Lets say I have an object
I'm planning to try contours on some image. Lets say I have three object

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.