![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Expert Bug Developer
Join Date: Apr 2008
Posts: 22
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |