Passing "this" to a constructor, bad form?

Posted on 2003-03-20
Medium Priority
Last Modified: 2008-03-06
I just starting working for a new company after a 9 month period of twidling my thumbs.  They have a poorly created class hierarchy.  I'm trying to clean it up but each class uses so much crap from every other class it's difficult.  One type of class in particular is xxxCmdThread.  There are about 20 seperate class file with different names that use identical code, I'm trying to collapse them into one.  The problem is that each one references pieces of it's creator directly.  These CmdThread classes really only handle shutdowns and do some logging (using the creators log reference).  The CmdThread also changes the member var shutdown flag to true and then reads it's JavaSpace.  Would it be bad form to just pass "this" to the CmdThread and let it invoke these directly or should I look to find another way?  

Jan Gonsalves
Question by:Tridecaphi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 16

Accepted Solution

imladris earned 300 total points
ID: 8174486
Sounds fine to me. At worst you're looking for the lesser evil. Having 20 nearly identical classes is certainly bad code. And passing "this" to a class so that it can invoke methods of another class is not in and of itself a violation of OOP principles. Classes, of necessity call each other methods all the time. The goal in Object Oriented Code is to ensure that the objects call each other only through a well defined interface, which hides their internal details from each other (so that their internals can change without unduely destabilizing the system).
LVL 35

Expert Comment

ID: 8174488
>  The problem is that each one references pieces of it's creator directly.

  What exactly do you mean here? Do you mean there is a Factory class that creates instances of objects and each one of them is using the variables of a superclass?

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question