Introduction to OOP Chapter 24: Design Patterns : next previous audio real text

Software Factory

Problem: How do you simplify the manipulation of many different implementations of the same interface (i.e., iterators).

Solution: Hide creation within a method, have the method declare a return type that is more general than its actual return type.

class SortedList {
	...
	Enumerator elements () { return new SortedListEnumerator(); }
	...
	private class SortedListEnumerator implements Enumerator {
		...
	}
}
The method is the ``factory'' in the name. Users don't need to know the exact type the factory returns, only the declared type.

The factory could even return different types, depending upon circumstances.

Intro OOP, Chapter 24, Slide 09