<< Chapter < Page | Chapter >> Page > |
Factories are a natural partner with anonymous inner classes. With a factory that returns anonymous inner classes, we can instantiate unique objects with unique behaviors. If the factory method takes in parameters, there local variables can be used to alter the resultant object's behavior, a process called " currying " (named after the famous mathematician/computer scientist Haskell Curry ). The objects made by the factory are then sent off to various odd and sundry different parts of our OO system but all the while retaining their closures, which were determined at the time of their instantiation. Thus they retain the ability to communicate back to the factory that made them even though they are being used in another part of the system that knows nothing about the factory. We like to call these " spy objects " because they act like spies from the factory. This gives us powerful communications even though the system is decoupled.
This is the last piece of our abstraction puzzle! We have
Reverse
such that it takes one parameter, the
IListFactory
, but such that its helper only takes one parameter (other than the host list) which is the accumulated list.
public class Reverse implements IListAlgo {public static final Reverse Singleton = new Reverse();private Reverse() {}public Object emptyCase(IMTList host0, Object... fac) {
return ((IListFactory)fac[0]).makeEmptyList();
}public Object nonEmptyCase(INEList host0, Object... fac) {final IListFactory f = (IListFactory) fac[0]; // final so that the anon. inner// class can access it.return host0.getRest().execute(new IListAlgo() {public Object emptyCase(IMTList host1, Object... acc) {
return acc[0];
}public Object nonEmptyCase(INEList host1, Object... acc) {return host1.getRest().execute(this,
f.makeNEList(host1.getFirst(), (IList) acc[0]));
}},f.makeNEList(host0.getFirst(), f.makeEmptyList()));
}}
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?