Class Interaction
Posted: Wed Aug 08, 2007 10:58 am
My latest endeavor of miniature proportions has caused a bit of confusion on some of the class interactions I have going on, so I am in need of guidance.
My "project" is just to create a web-based chat system, using OOP (because I still want to learn more). Classes involved in my problem (thus far) are: Message, User, UserList, UserFinder (singleton), DB (singleton), and DBQueryParser.
The Message class has the basic properties, body, who it's from, and a UserList of Users to send the message to. The Message class only has a UserList if the message is a Message_Broadcast (Message is the parent, Broadcast is the only child so far), otherwise the TO of the message is just a User. The UserList uses the UserFinder class to search the database (via DB class, which uses the DBQueryParser to clean the query) for a user, and returns a User object that represents the user (imagine that!).
So the interaction looks something like: Message -> UserList -> UserFinder -> DB -> DBQueryParser
The hold up I'm running into is that both DB and DBQueryParser both work off a global config ($config) to base their actions off (ie: db host/user/pass, table names/aliases, etc), and how to alleviate having to create an instance of DBQueryParser and then pass it to whatever needs to parse their queries.
Here's the code I have so far (edited to just show implementation, not actual execution methods):
Just typing this post has helped a lot, and I have answered most of my own question(s), but I posted anyways for other solutions/critique/others to use as reference.
My "project" is just to create a web-based chat system, using OOP (because I still want to learn more). Classes involved in my problem (thus far) are: Message, User, UserList, UserFinder (singleton), DB (singleton), and DBQueryParser.
The Message class has the basic properties, body, who it's from, and a UserList of Users to send the message to. The Message class only has a UserList if the message is a Message_Broadcast (Message is the parent, Broadcast is the only child so far), otherwise the TO of the message is just a User. The UserList uses the UserFinder class to search the database (via DB class, which uses the DBQueryParser to clean the query) for a user, and returns a User object that represents the user (imagine that!).
So the interaction looks something like: Message -> UserList -> UserFinder -> DB -> DBQueryParser
The hold up I'm running into is that both DB and DBQueryParser both work off a global config ($config) to base their actions off (ie: db host/user/pass, table names/aliases, etc), and how to alleviate having to create an instance of DBQueryParser and then pass it to whatever needs to parse their queries.
Here's the code I have so far (edited to just show implementation, not actual execution methods):
Code: Select all
interface moose_IMessage { }
class moose_Message implements moose_IMessage {
public function addRecipient($user) {
$this->my_to = $user;
}
}
class moose_Broadcast extends moose_Message {
public function addRecipient($user) {
$this->my_to = new moose_UserList();
$this->my_to->addRecipient($user);
}
}
class moose_User { }
class moose_UserList {
public function __construct() {
$this->my_user_finder = moose_UserFinder::getInstance();
}
public function addUserByID($userid) {
$this->my_user_array[] = $this->my_user_finder->findById($userid);
}
}
class moose_UserFinder {
private function __construct() {
$this->finder_db = MySQL::getInstance();
}
public function findById($userid) {
$result = $this->finder_db->query("SELECT * FROM @users WHERE #id=$userid"); // calls the Parser that is part of MySQL class
}
}
class MySQL implements IDatabase { }