Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Last active December 16, 2015 07:19
Show Gist options
  • Save DeaconDesperado/5398089 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/5398089 to your computer and use it in GitHub Desktop.
public class UserDAO(){
static cache;
static user_model;
public static getInstance(CacheInterface cache, UserInterface user_model){
this.cache = cache;
this.user_model = user_model;
}
public static get(int Id){
UserInterface from_cache = this.cache.get(this.cache.generateKeyHash(Id));
if(from_cache){
return from _cache;
}else{
UserInterface user = this.user_model.find(Id);
return user;
}
}
}
public abstract class UserInterface{
public static find(int Id);
public static change_pass(String old_pass, String new_pass);
}
public class MongoUser extends UserInterface{
public static find(Int Id){
//I am a static get method where db instances are returned
MongoUser user this.collection.findOne(Id);
return User;
}
public function change_pass(String old_pass,String new_pass){
//I am an instance method
if(old_pass == this.pass){
this.pass = new_pass;
this.save();
}
}
}
//We could make other user interfaces, though
public class FileUser extends UserInterface{
// same methods here, but hit filesys!
}
public abstract class Cache{
public static function getKey();
public static function setKey();
}
public class Memcache extends Cache{
//do those two methods from memcached
}
public class FilesysCache extends Cache{
//same thing but from filesys!
}
public class UserController{
UserDAO DAO;
public function Controller(){
//different DAOs here can access totally different persistency or cache layers and the
//whole system will still work! Also dummy content for webservices, etc. We could swap
//the args to getInstance for any classes that inherit from cache or UserInterface
this.DAO = UserDAO.getInstance(Memcache,MongoUser);
}
public function getRoot(int Id){
//Call DAO methods instead of model directly!
this.DAO.getUser(Id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment