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

  1. Abstract Structure -- abstract classes, interfaces
  2. Abstract Behavior -- abstract methods, strategies, visitors.
  3. Abstract Construction -- factories
  4. Abstract Environments -- anonymous inner classes, closures.

Examples

Example 1: reversing a list using factory and anonymous inner class helper

Write 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())); }}

Example 2: ant world

Imagine a world of ants that live in a one-dimensional space. A queen ant can make a bunch of worker ants. Each time she gives birth to a worker ant, she gives it a name. A worker ant can always tell what its name is. A worker ant from a particular colony can always calculate its distance from its queen. A worker ant can also move its queen to a different location. Wherever the queen moves to, ALL of her workers always know their relative distance from her. We want to keep track of all the ants in our ant world and all the ants in each of the existing colonies. We want to model the fact that each queen produces its own worker ants, each one which can move its queen around without telling the other ants in the same colony, yet ALL of the ants in the same colony would know where their queen is.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask