Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 16th, 2007, 5:05 PM   #1
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
programming design

Hello

I have a programming design question..
Lets say I have 3 classes like below:

class parser {
};

class user {
};

class session {
public:
	std::list<user> m_list;
};


Session class is a 'main class' where most of the work is being done (in loop/s)..
It has a list of user objects, where each user needs access to 1 particular parser object (parser class).

I dont want each user to have its own parser object since that would be unneccessary..

I wonder what is the best approach to make class user (user objects) be able to access data in one parser object?
I know most of you would just pass parser object's reference to user class constructor, and then access data this way.

Is there any better way/design pattern to achieve that?
Maybe I shouldn't bother because this might be the best/easiest way?
Or is there more 'professional' way?

Another thing I'm unsure about..

In case passing reference of parser object to user objects is the best way, where would you guys define parser object?

Does this seem okay:

class session {
public:
	std::list<user> m_list;
	parser m_parser;
};

Thanks for help
Regards
l2u is offline   Reply With Quote
Old Jul 16th, 2007, 6:55 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Why don't you make a parser object a member of a user object?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 16th, 2007, 8:03 PM   #3
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5 lectricpharaoh will become famous soon enough
Dunno if it fits with your design requirements, but have you considered making parser a static class?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jul 17th, 2007, 4:07 PM   #4
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
Quote:
Originally Posted by DaWei View Post
Why don't you make a parser object a member of a user object?
This would take too much memory. parser class has some data that doesnt need to be multiplied and it has only one function that is being called with different arguments.


Quote:
Originally Posted by lectricpharaoh View Post
Dunno if it fits with your design requirements, but have you considered making parser a static class?
How should I construct parser object in class when its static? The usual way?

class user {
public:
user(int somedata) : m_parser(somedata) { } 
static class parser m_parser;
};
l2u is offline   Reply With Quote
Old Jul 17th, 2007, 5:54 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by l2u View Post
This would take too much memory. parser class has some data that doesnt need to be multiplied and it has only one function that is being called with different arguments.
In that case, there is no need for your parser to be a class at all. It would be better implemented as a single function that is called, with arguments providing necessary data, by your user classes.
grumpy is offline   Reply With Quote
Old Jul 17th, 2007, 9:44 PM   #6
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
Quote:
Originally Posted by grumpy View Post
In that case, there is no need for your parser to be a class at all. It would be better implemented as a single function that is called, with arguments providing necessary data, by your user classes.
Class itself still holds some data that is needed by parser class.
And I dont like having global objects/functions.
l2u is offline   Reply With Quote
Old Jul 17th, 2007, 10:20 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You seem to have all the answers. One wonders why there is a question.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 17th, 2007, 11:26 PM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,126
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by l2u
How should I construct parser object in class when its static? The usual way?
My bad; that was a brain fart. I should have said 'a class whose public methods are all static', as that is more accurately what I meant.

That way, you group one or more functions into your class, and can call them like regular global functions. The class can have an internal state, such that it can initialize itself when the first method is called, or upon calling some initialization method (for example, to 'attach' the stream std::cin as the source for user input).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jul 18th, 2007, 6:23 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by l2u View Post
Class itself still holds some data that is needed by parser class.
Not a valid justification for using a class rather than a single function. Simply pass the needed data as an argument (eg a struct, a pointer to a struct, a reference to a struct) to the function.
Quote:
Originally Posted by l2u View Post
And I dont like having global objects/functions.
Lots of people don't like eating vegetables either. But people who eat vegetables are usually healthier than those who don't.
grumpy 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
The Black Art of Video Game Console Design lostcauz Book Reviews 0 Apr 26th, 2006 8:31 PM
Does Programming Make You Smarter? Sane Coder's Corner Lounge 43 Oct 2nd, 2005 7:12 AM
Design / Programming Community Yuneek Existing Project Development 15 May 7th, 2005 10:41 AM




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

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