Simple Example of PAC with Visual Proxy
This document provides a very simple example of the PAC software architecture using the visual proxy approach described by Allen Holub in JavaWorld. This document fits into discussions from Ball State University's Spring 2008 offering of CS345/545: Graphical User Interfaces; it is not indended as a complete explanation of any of the topics contained herein.
Let's get some infrastructure out of the way first: we know we will
need a UserInterface
interface that identifies any
abstraction layer object that can produce its own visual proxy. It
will look like the one we've been using in class.
We will consider the world of employees. Our abstraction-layer object is an employee that has one attribute: a name. A first pass at the implementation is shown below.
Employee is an abstraction-layer object, but it implements a method that can create and return the presentation-layer representation of this object. That is, it can create a visual proxy for itself.
The control is the piece that actually asks the abstraction-layer object for its visual proxy. We can make a control out of a JPanel as shown below.
That's it! It's the simplest example of visual proxy that I could devise. You could test this example with a minimal driver:
OK, you've made it past the overview, and hopefully you see how the three aspects of PAC work together. However, PAC and Visual Proxy are not formulaic solutions: they are approaches to solving software design problems. Consider what would happen if we needed not only an EmployeeViewer but also an EmployeeEditor. Now we need to build the system a little more carefully. Here is the approach described in the original articles on visual proxy:
We have added another parameter to the visualProxy method, this one indicating whether or not the requested proxy will be used for editing. We can change the implementation of Employee as follows:
Of course, we should use the techniques presented in class to monitor multiple proxies and prevent memory leaks. Ignoring these issues for now, we can write new controls like this: