Factory Pattern w/ Delegates
I've been focusing some of my efforts on rounding out a lightweight IoC Factory. The implementation is fairly simple, you have a Factory object that has Delegates, each Delegate represents a type that can be created by the Factory. The Factory itself only has few methods on it:
In the above example, we've registered the UserDaoImpl in a Singleton Delegate. The Delegate lookup within the Factory is smart enough to return the UserDaoImpl anytime a developer requests UserDao (it's interface).
One thing you'll notice is that the 'get' method uses variable arguments, just like the new System.out.printf(...) method. This allows the Delegate to take that input and automatically uses it during instantiation. e.g., UserSaveAction has a 2 constructors, calling Factory.get(UserSaveAction.class) didn't take any parameters, so it will create a UserSaveAction with whatever other Types are in the Factory (UserDaoImpl). In the second case of updating a user, the example had taken in a user-- get(UserSaveAction.class, user). This caused the Factory to find a constructor that also took in a User object.
In order to speed up performance, lets say this Action was used on the web and we wanted to squeak out a little more performance, UserSaveAction declares it's own public Delegate to be registered. This allowed UserSaveAction to control how it was going to be created based on input.
By creating it's own delegate, my performance tests performed only a fraction slower than wiring the components together directly.
I would now like to add "listener" behavior to creation. Allowing additional components to be wired together in ways other than constructor injection-- such as annotations. I'm open to suggestions on how/where the listener behavior should occur, if it should be assigned on a per Delegate level or at the Factory level. Also, since the Factory doesn't dictate lifecycles at all, listeners at the Factory level would be notified whenever an object is "grabbed", not knowing if it was just created or if it's the 4th time it's been grabbed.
public interface UserDao { ... }
public class UserDaoImpl implements UserDao { ... }
public class User { .... }
public class UserSaveAction {
public UserSaveAction(UserDao dao) { ... }
public UserSaveAction(UserDao dao, User user) { ... }
// getter/setters
public String save() { ... }
}
DefaultFactory f = new DefaultFactory();
f.register(new SingletonDelegate(UserDaoImpl.class));
f.register(UserSaveAction.DELEGATE); // register(Delegate>)
...
// builds a UserSaveAction with new User
UserSaveAction newUser = f.get(UserSaveAction.class);
newUser.setFirstName("John");
newUser.setLastName("Doe");
newUser.save();
// refetch that user, generics removes the need for casting
User user = f.get(UserDao.class).getByFirstName("John");
// get(Class,Object... params) (variable arguments)
UserSaveAction updateUser = f.get(UserSaveAction.class, user);
updateUser.setFirstName("Jacob");
updateUser.setLastName("Hookom");
updateUser.save();
In the above example, we've registered the UserDaoImpl in a Singleton Delegate. The Delegate lookup within the Factory is smart enough to return the UserDaoImpl anytime a developer requests UserDao (it's interface).
One thing you'll notice is that the 'get' method uses variable arguments, just like the new System.out.printf(...) method. This allows the Delegate to take that input and automatically uses it during instantiation. e.g., UserSaveAction has a 2 constructors, calling Factory.get(UserSaveAction.class) didn't take any parameters, so it will create a UserSaveAction with whatever other Types are in the Factory (UserDaoImpl). In the second case of updating a user, the example had taken in a user-- get(UserSaveAction.class, user). This caused the Factory to find a constructor that also took in a User object.
In order to speed up performance, lets say this Action was used on the web and we wanted to squeak out a little more performance, UserSaveAction declares it's own public Delegate to be registered. This allowed UserSaveAction to control how it was going to be created based on input.
public static final UserSaveDelegate DELEGATE =
new UserSaveDelegate();
public static class UserSaveDelegate
implements Delegate{
public UserSaveAction create(Factory f)
throws ActualizationException {
return new UserSaveAction(f.get(UserDao.class));
}
public UserSaveAction create(Factory f, Object[] p)
throws ActualizationException {
return new UserSaveAction((User) p[0],
f.get(UserDao.class));
}
public Class super UserSaveAction> getType() {
return UserSaveAction.class;
}
}
By creating it's own delegate, my performance tests performed only a fraction slower than wiring the components together directly.
I would now like to add "listener" behavior to creation. Allowing additional components to be wired together in ways other than constructor injection-- such as annotations. I'm open to suggestions on how/where the listener behavior should occur, if it should be assigned on a per Delegate level or at the Factory level. Also, since the Factory doesn't dictate lifecycles at all, listeners at the Factory level would be notified whenever an object is "grabbed", not knowing if it was just created or if it's the 4th time it's been grabbed.

3 Comments:
It's an interesting implementation. I think that if java5 supported a run-time rappresentation of generic type even a nicer factory coud be done. Here C# has an advantage.
http://weblogs.asp.net/pgielens/archive/2004/07/01/171183.aspx
PS. Your blog is similar to mine :)
http://schrepfler.blogspot.com
By
Srepfler Srgjan, at 3:56 AM
Wow, you have a bunch of great articles on the site, I hope you moove from an cubicle to a office (or lab) soon :)
By
Srepfler Srgjan, at 4:03 AM
情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情趣,情趣,情趣,情趣,A片,視訊聊天室,聊天室,視訊,視訊聊天室,080苗栗人聊天室,上班族聊天室,成人聊天室,中部人聊天室,一夜情聊天室,情色聊天室,視訊交友網a片,a片
免費A片,AV女優,美女視訊,情色交友,免費AV,色情網站,辣妹視訊,美女交友,色情影片,成人影片,成人網站,A片,H漫,18成人,成人圖片,成人漫畫,情色網,日本A片,免費A片下載,性愛
A片,色情,成人,做愛,情色文學,A片下載,色情遊戲,色情影片,色情聊天室,情色電影,免費視訊,免費視訊聊天,免費視訊聊天室,一葉情貼圖片區,情色,情色視訊,免費成人影片,視訊交友,視訊聊天,視訊聊天室,言情小說,愛情小說,AIO,AV片,A漫,avdvd,聊天室,自拍,情色論壇,視訊美女,AV成人網,色情A片,SEX,成人論壇
情趣用品,A片,免費A片,AV女優,美女視訊,情色交友,色情網站,免費AV,辣妹視訊,美女交友,色情影片,成人網站,H漫,18成人,成人圖片,成人漫畫,成人影片,情色網
情趣用品,A片,免費A片,日本A片,A片下載,線上A片,成人電影,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人文章,成人影城,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,臺灣情色網,色情,情色電影,色情遊戲,嘟嘟情人色網,麗的色遊戲,情色論壇,色情網站,一葉情貼圖片區,做愛,性愛,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,美女交友,做愛影片
av,情趣用品,a片,成人電影,微風成人,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,愛情公寓,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,aio,av女優,AV,免費A片,日本a片,美女視訊,辣妹視訊,聊天室,美女交友,成人光碟
情趣用品.A片,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,色情遊戲,色情網站,聊天室,ut聊天室,豆豆聊天室,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,免費A片,日本a片,a片下載,線上a片,av女優,av,成人電影,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,成人網站,自拍,尋夢園聊天室
By
sexy, at 7:23 AM
Post a Comment
<< Home