Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 28th, 2008, 6:54 PM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 72
Rep Power: 4 kurt is on a distinguished road
Classes : Using data from outside the class

Let's say I have a large system. Then, I have a class (A) that does one particular functionality.

If A operates on a data outside of the class, say a structure that is shared by the whole system -- is that a good design, or generally do we have to restrict class operations to member variables and member functions?
kurt is offline   Reply With Quote
Old May 28th, 2008, 7:00 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Re: Classes : Using data from outside the class

Well, I'm not too familiar with design patterns and such, but what you're describing doesn't sound like a typical solution.

Without you actually posting code, my question would be: Why are you writing a class to operate on the data instead of just writing a function that takes the data as a parameter (or accesses a global variable)?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 28th, 2008, 10:57 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3 Jimbo is on a distinguished road
Re: Classes : Using data from outside the class

It depends on your design. If you're modeling a lawnmower repair shop, odds are that the Mechanic class might have to operate on an object of type Lawnmower. I suppose "good" design would involve passing the Lawnmower object to the Mechanic's method of choice, but you'll rarely be able to construct a fully decoupled system.

Personally, I try to design my classes to take any external data as parameters rather than relying on global objects or obscure data sources. I find that it makes for a more adaptable design, more intuitive API, and less troublesome implementation when something goes awry.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old May 29th, 2008, 12:25 AM   #4
kurt
Programmer
 
Join Date: Oct 2005
Posts: 72
Rep Power: 4 kurt is on a distinguished road
Re: Classes : Using data from outside the class

Thanks, Jessehk and Jimbo.

I totally agree, which is why I asked. Thanks for the input. Anyway, here's an example of what i'm doing.

My class "A" has an API which basically sets a bunch of hardware registers, by writing to the register address. This is the first coupling, but I guess it's not really a big deal, since it's just making some calculations and making writes to registers. All it needs is the main pointer to the first register (the rest are just address additions) which can be passed to the constructor.

The second coupling is, the calculation requires a set of factors, which other subsystems will share the use of. It's a global data, therefore I initially thought of just using it directly in "A"s member function. But since that factor is not frequently set (only set once by other subsystem), I figured I can make a clone of the data as A's member variable. Is that a good idea?
Since the member function that calls the global data is a private member, passing as an argument seems not possible.


Sorry for the vague and wide question, it's the best that I can come out with. It's more of a design style question.

Thanks in advance.
Kurt
kurt is offline   Reply With Quote
Old May 29th, 2008, 10:05 PM   #5
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 22
Rep Power: 0 Sorrofix is on a distinguished road
Re: Classes : Using data from outside the class

I'd recommend making a global object whose data is read-only (accessor functions only), and (be?)friends any classes which have to make modifications to the factors. Cloning data between objects is a bad idea and could quickly get messy and buggy with the updating of two or more copies. If you really must have external data as a member of the class, at least use a pointer. Unless, of course, the factors as a whole pertain mainly to one class, in which case you should add the data as members of said class (and in that case, they should be exclusively members of the class).

...if that made any sense.
Sorrofix is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning data from a class Kilo C# 6 Apr 8th, 2008 5:55 PM
template class brad sue C++ 1 Mar 25th, 2007 6:46 PM
using classes in another class ling_wong C++ 6 Jan 23rd, 2006 10:55 PM
Ruby Classes tutorial bulio Ruby 5 Oct 30th, 2005 12:32 AM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 1:21 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC