Kiss My App

Friday, December 03, 2004

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:

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 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.

2 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 Blogger schrepfler, 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 Blogger schrepfler, at 4:03 AM  

Post a Comment

<< Home