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 7025343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:57:43+00:00 2026-05-27T23:57:43+00:00

Is it bad design to pass the containing object as an argument to a

  • 0

Is it bad design to pass the containing object as an argument to a method of a contained object, like in the simplified example below?

class A
{
    private B _containedObject;

    public A(B b)
    {
            _containedObject = b;
            //...
    }

    public void SomeMethod()
    {
            //...
            _containedObject.SomeMethod(this);
            //...
    }
}

class B
{
    public void SomeMethod(A a)
    {
            //do something with a
    }
}

P.s. The above example is simplified to illustrate merely the containment relation and passing the containing object to the contained object, and does not in itself illustrate the purpose of doing so. Rest assured though that there is a purpose.

  • 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-27T23:57:44+00:00Added an answer on May 27, 2026 at 11:57 pm

    There are times when you might want to pass the containing object as a parameter to a contained object. As other posters have noted, it is called “double dispatch”. The Smalltalk language provides a classic example.

    The example is about adding together integers and floats (or other types of numbers). In C++ or Java, your integer class would have polymorphic methods like:

    Integer {
         add(Float) {...}
         add(Integer) {...}
         add(UserDefinedType) {...}
    }
    

    Each add method could implement the different kinds of addition correct. The compiler uses the type information to select the right method. The statement Integer(6).add(Float(1.0)) would invoke the Integer::add(Float) method.

    Smalltalk, unlike C++ and Java, is dynamically typed. A Smalltalk class can only have one method named add.

    Integer::add(Object)

    The compiler does not know the class of “object”. For that reason, Smalltalk cannot implement the add the same way as C++ or Java. Instead, Smalltalk uses double dispatch. It works like this:

       Integer {
              add(Object o) {
                  return o.addToInteger(this)
              }        
        }
    

    The other number classes like Float, Fraction and even Integer implement the method addToInteger().

    Float {
          addToInteger(Object o) {
              //Perform float + in math
              return result
         }
    }
    

    Double dispatch solves the problem of how to implement man operations polymorphically in a dynamically typed language. But there is a more common use: the visitor pattern. I’ll put that in a separate answer.

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

Sidebar

Related Questions

Why is it a bad design for an object to refer to another object
It's considered a bad idea/bad design, have a class with a constructor accepting a
I'm reading a lot about good and bad practices in OOP design. It's nice
Possible Duplicate: What is so bad about Singletons? It's understandable that many design patterns
Why is it bad practice to declare variables on one line? e.g. private String
I'd like to add a generic type method DoSomething<T> , but for backwards compatibility,
Is use of VO´s (POCO) a bad design pattern? Some people say that all
I realize (having read other posts on SO as well) that it's bad design
I'm changing some bad design on a table. The field I'm trying to change
Maybe I have a bad design but I am currently trying to get an

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.