Get all objects of a particular class in Object Oriented programming
I've seen a few alternatives of how to get all objects recently and I particularly like the Factory method, but ive seen people using functions outside of their classes, calling a getAll method on their user object and a few more.
This example below is how I most commonly get all users from the database. Although this is PHP code, I would like to see some ideas how people do the same in different programming languages.
What do you prefer and why? This is more of a discussion and one that I hope can give me more ideas next time I come to doing this.
class userFactory extends Factory {
public function __construct(){
}
public function getAllUsers() {
$users = array();
$result = $this->db->Execute("SELECT * FROM user");
while (!$result->EOF) {
$users[] = new user($result);
开发者_StackOverflow }
return $users;
}
}
You've implemented part of DAO design pattern (not a factory even when it returns user instances). DAO generally takes care of finding, creating, persisting, editing and deleting objects of the same kind.
See also the Repository pattern - repository is higher level of abstraction - it can query multiple DAOs to get expected results however one DAO should query only one db table.
PHP persistence frameworks
- Doctrine ORM - Java-like ORM, without dependencies, see examples (getters/setters should be optional)
- Kohana ORM - ActiveRecord, part of Kohana 3.0 web framework
- Zend_Table - ActiveRecord, part of Zend framework
- there were few others (google orm/activerecord/persistence for php)
Then you can define relations and work with objects like this:
//ORM/ActiveRecord way
$user = Repo :: get ( "users" ) -> find_by_id ( $user_id );
$user = User :: find_by_id ( $user_id );
$user_payments = $user -> payments;
$user_articles = $user -> articles;
instead of:
$user_repository = Repo :: get ( "users" );
$articles_repository = Repo :: get ( "articles" );
$user = $user_repository -> find_by_id ( $user_id );
$user_articles = $articles_repository -> find_all_for ( $user );
ActiveRecord is much simplier and easier to use but it's not very OOP. Methods like:
- $user -> save (),
- $user -> delete (),
- User :: find_all ()
- User :: find_by_id ( $id )
just should not be the part of User class because it is not the object's responsibility to know how all instances can be found, how it should be stored in db, etc. Anyway - it is a good start, it forces you to thinking about rich domain model. So - go for ActiveRecord and after you reach some skill try to switch to some true ORM
Other languages
- For JAVA projects, google for JPA, Hibernate, Toplink, iBatis.
- For Ruby, there is an ActiveRecord class
- and if you really love OOP, google for smalltalk persistence :-)
Well, if you're just looking for ideas, you may consider using an Object-Relational Mapper (ORM). This allows you to use the results of database queries as first-class objects in your language of choice. For example, if I had a table of Companies and Employees, I might be able to do this in Ruby/Rails (using ActiveRecord ORM):
mycompany = Company.where(:name => "MyCompany").first
employee1 = mycompany.employees.first
This generates an SQL query, and automagically creates a system of first-class objects. In your previous example, you would accomplish it by using this type of code:
$users = UserQuery::create()=>all();
If you use PHP, there are already some good ORMs out there, just google "php orm" to find them. There are some ActiveRecord clones, as well as some that fit more cleanly into the php language.
精彩评论