Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thoughts of Encapsulation 2

Status
Not open for further replies.

golcarlad

Programmer
Nov 23, 2004
232
0
0
GB
Just thinking about code design etc, and would like to know your opinions.

Sometimes I have a class - say a helper class that has a whole load of useful but generic STATIC methods inside - is it good practice to call this static methods from your objects?, or is this in effect breaking encapsulation?
 
That's an interesting question. I suppose that it would (go against the principle of encapsulation). I can see there might be times when this would be helpful.

My initial thought is, you don't want to build that extra dependency into your class if you can avoid it. If you really need the methods available within your objects, I would code them there, or look for a way to use inheritance to make them available (I take it you have circumstances making this option impossible ;-) )

I'm pretty absent minded though, I would do this to protect myself from myself bringing a class into a project (and forgetting to bring the helper with it). And forget about changing anything in the helper class!

I'll be curious to hear what the real oo experts have to say on this one.

Alex

[small]----signature below----[/small]
With all due respect, Don Bot, I don't think we should rely on an accident happening. Let's kill him ourselves.

Ignorance of certain subjects is a great part of wisdom
 
Inheritance just to include generic library functions? No! Don't do it!

In answer to your question, putting static library functions in a separate class does not break encapsulation at all. The idea of encapsulation is that when you are looking at code in a class you can see all the points where that a classes members are modified without going out of the class. By calling a library function you are not breaking this principal as you will be able to see in the code that a member is being passed to the library function. For an example of a whole load of functions like this take a look at the System.Math class.

With static methods that are in the same class as your object, you will notice that you can't access the instance variables from the static methods anyway (you get a compiler error). So you still have to pass the data in a function call. As a result encapsulation is still not broken.

Lastly putting your generic library functions in a base class that everything inherits from is a sure way to madness. Sooner or later you will find that you want to inherit from a class that a 3rd party wrote, but you can't because that would now require multiple inheritance. For example you will more than likely inherit from System.Windows.Forms.Control at some point.
 
Thnaks guys for your input - I think that confirms a few things for me - at least I know that I am more or less adopting the correct approach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top