Java Reference
In-Depth Information
Chapter 18. Prototype
As a class developer, you ordinarily furnish your class with constructors to let client
applications instantiate it. The construction-oriented patterns covered so far—B UILDER ,
F ACTORY M ETHOD , and A BSTRACT F ACTORY —isolate the user of an object from this
constructor call. These patterns establish methods that instantiate an appropriate class on
a client's behalf. The P ROTOTYPE pattern also conceals object creation from clients but uses
a different approach. Instead of bringing forth new, uninitialized instances of a class, the
intent of the P ROTOTYPE pattern is to provide new objects by copying an example.
Prototypes as Factories
Suppose that you are using the A BSTRACT F ACTORY pattern at Oozinoz to provide user
interfaces for several contexts. Figure 18.1 shows the user interface factories you have in
place.
Figure 18.1. Three abstract factories, or kits, produce different user interface
look-and-feels.
The users at Oozinoz are thrilled with the productivity that results from applying the right user
interface in the right context. The problem you face is that they want several more user
interface kits, and it's becoming burdensome to create a new class for each context your users
envision.
To halt the proliferation of user interface factory classes, you can replace them all with
a single UIKit class that has prototypical objects in its instance variables. This class's
creation methods will return copies of these prototypical objects. You haven't figured out yet
how your copy methods will work or how you will set up your prototypical factories, but
having a sketch of the class will give you a target to work toward.
Search WWH ::




Custom Search