File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Global VS trees Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Global VS trees" Watch "Global VS trees" New topic
Author

Global VS trees

Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
One of the weaknesses of global variable is, well I do not need to tell you probably already know.

Object Oriented technology solves this problem mostly by implementing a tree structure. This tree can be an Inheritance tree or a Composition tree. The advantage of using this tree-based structure is clear; any variables are not exposed to all the application but stay within a Class Method or function where it is most relevant.

Some variables have no owner and ought not belong to any particular Object or Class in the tree hierarchy; the consequences of doing so are, as the design of the application changes and evolves those variables are forces to change with their parent Class or Object.

You probably know about the disadvantage of overuse of inheritance and the problem they create and composition is a better alternative and is also more dynamic in nature. However, don�t you think tree is a tree and come with all of the disadvantages of a tree structure like what I mentioned above? Do you think that global variables evil is overrated?

To make it so global don�t clash with other modules and classes, all global start with g_ (like g_foo )
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't think I buy the "mostly trees" notion of OO. Object relationships can form lists, queues, maps, unordered bags, circular buffers, overlapping sets, pretty much anything you like. A single object can be a member of all of those things at once. Trees are among the solutions for a pretty large set of problems but they're not the only thing out there by any means.

I've worked with some object models that felt tree-like in that to find any object you had to start from an anchor and navigate your way to it. Maybe that kind of situation gave you the "mostly trees" point of view. But I haven't had that feeling in my Java work.

Inheritance is a tree for sure and breaking the rigidity of a hierarchy is one reason (among many) to favor composition. Implementation of interfaces is not necessarily tree-like, though. By implementing several interfaces is an object a member of several trees?

Read-write globals have a whole bunch of problems, as you hinted. Static variables and singleton objects both let this kind of trouble into your life if you're not careful. You can cause the same problems with uncontrolled access to external data like a database or file system. I haven't missed the concept of global variables in Java so I guess I'm ok with the alternatives.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I agree with Stan: objects form a network of some kind. A tree is just one possible form of that network, and as you noticed, not always the best.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
I was talking more about tree in code structure apposed to data. GUI is mostly built as a tree structure as widgets built on top of widgets.

If you want to change a widget by traversing the tree and the tree needs to change in future you would be stuffed wouldn�t. The alternative is to give these widgets global access. Do you guys have any alternative to this problem?

Hay talking about this has gaves me an idea? The GUI I am using it is possible to give a widget its own ID; I could store these IDs in a container class or something. But wouldn�t this container class have to be global.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Of course my GUI follow the Composite pattern, too, and therefore build a tree, but I always never feel the need to traverse that tree to find a specific component once I've constructed it.

Can you give an example of why you might want to do that?
Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
Shite!! I just forgot the benefits of using more events in application; I often do that. I have to rethink my design a little more. The chances are you are correct and I don't need to treverse any tree.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
For Swing I tend to make the widgets on a "component level" panel member variables of a class. If they happen to be organized on a zillion sub-panels that's not really important. I often reorganize the subpanels and layout managers without affecting the way I access the widgets.

If some of those subpanels happen to be reusable classes I could start to see some tree-like navigation the their widgets. But following Demeter's Rather Good Idea the subpanel really should hide its widgets so you can only navigate as far as the panel.

At work we used a framework built on Visual Age for Java where you wire events in a visual tool. No hierarchy at all. Very cool for three widgets, an absolute nightmare for a hundred. At home I made my own little pub-sub engine so neither end of the conversation has to know about the other. I have one app where each subpanel is showing the progress of its own worker thread and there is no tree navigation down to them ever.
[ March 19, 2005: Message edited by: Stan James ]
Gerald Davis
Ranch Hand

Joined: May 15, 2002
Posts: 872
Mmmm mmm
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Sounds like you found it chewey. I hope it's nutritious, too!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global VS trees