Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Any way to restrict class variables only to some methods?

Thread view: 
Shawn - 27 Oct 2006 19:39 GMT
Hi,

I have a huge class, which has about 30 class variables. And the class
has many methods. Each method doesn't need all the 30 class variables,
only need a subset of them. In another word, say the 30 class variables
can be grouped into 6 groups (say group A, group B, .., group F).

public class MyHugeClass
{
    //group A class variables
    private double a, b, c, d, e;

    //group B class variables
    private double w, t;

    ..

    //group F class variables
    private double t1, t2,t3;

    public void method1()  //only need group A and F variables, but it can
access all, unfortunately
    {
        ...//code
    }

    public void method2()  //only need group B and F variables, but it can
access all, unfortunately
    {
        ...//code
    }

    ...//more methods omitted

} //end of class

The method method1() only need group A and group F class variables,
doesn't need others. The method method2() only need group B and group F
class variables, doesn't need others. But since method1() and method2()
all below to the class, so they have access to all the class variables
(from group A to group F), which is more than they need and could
introduce errors. As you can imagine, this is a huge class. More
restrictions, better, because it could force the errors to be detected.

Thank you very much.
Oliver Wong - 27 Oct 2006 20:03 GMT
> Hi,
>
[quoted text clipped - 41 lines]
>
> Thank you very much.

   There's no way to solve the problem as you've stated. However, if the
"higher level goal" is to force more errors to be detected, perhaps you
could elaborate on what kind of error you suspect might occur, and we can
discuss designs or programming practices which might minimize the chance of
those errors occuring (or at least, being able to detect those errors as
soon as possible).

   - Oliver
Mark Jeffcoat - 27 Oct 2006 21:08 GMT
> The method method1() only need group A and group F class variables,
> doesn't need others. The method method2() only need group B and group F
[quoted text clipped - 3 lines]
> introduce errors. As you can imagine, this is a huge class. More
> restrictions, better, because it could force the errors to be detected.

You're very close to the right answer. You know you've
got a class that's too big, and doing too much. You've
got a decently clear rule for figuring out which methods
belong together (they use the same subset of class variables).

So split it into two classes.

http://www.refactoring.com/catalog/extractClass.html

Signature

Mark Jeffcoat
Austin, TX

Robert Klemme - 28 Oct 2006 13:12 GMT
> You're very close to the right answer. You know you've
> got a class that's too big, and doing too much. You've
> got a decently clear rule for figuring out which methods
> belong together (they use the same subset of class variables).

Absolutely!

> So split it into two classes.

... or rather n classes if there are n sets of variables.

   robert
Patricia Shanahan - 28 Oct 2006 13:36 GMT
>> The method method1() only need group A and group F class variables,
>> doesn't need others. The method method2() only need group B and group F
[quoted text clipped - 12 lines]
>
> http://www.refactoring.com/catalog/extractClass.html

Other refactorings may be needed first to improve the separation. For
example, it may be desirable to split up methods that operate on more
than one logical group of variables, such as method1 and method2.

I would start by thinking about the variables, and select a group of
variables that you know belong together.

Next, decide what operations they should support. Do extractMethod

http://www.refactoring.com/catalog/extractMethod.html

until the only access by the rest of the class to those variables is
through those methods, and those methods do not interact directly with
any other variables in the class.

Once you have a logical group of variables and associated methods such
that the only interactions with the rest of the class are through method
calls, do the extractClass.

Repeat as needed until you no longer feel any urge to limit access
within a class.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.