Simple Example of PAC with Visual Proxy

By Paul Gestwicki

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.

public interface UserInterface { public JComponent visualProxy(String attribute); }

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.

public class Employee implements UserInterface { public static final String NAME_ATTRIBUTE = "name"; private String name; public Employee(String name) { this.name=name; } public JComponent visualProxy(String attribute) { if (attribute.equals(NAME_ATTRIBUTE)) { return new JLabel(name); } } }

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.

public class EmployeeViewer extends JPanel { public EmployeeViewer(Employee employee) { add(employee.visualProxy(Employee.NAME_ATTRIBUTE)); } }

That's it! It's the simplest example of visual proxy that I could devise. You could test this example with a minimal driver:

public class Driver { public static void main(String[] args) { Employee employee = new Employee("Dirk McQuickly"); EmployeeViewer viewer = new EmployeeViewer(employee); JFrame f = new JFrame("VisualProxy Test"); f.getContentPane().add(employeeViewer); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }

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:

public interface UserInterface { public JComponent visualProxy(String attribute, boolean isReadOnly); }

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:

public class Employee implements UserInterface { public static final String NAME_ATTRIBUTE = "name"; private String name; public Employee(String name) { this.name=name; } public JComponent visualProxy(String attribute, boolean isReadOnly){ if (attribute.equals(NAME_ATTRIBUTE)) { if (isReadOnly) return new JLabel(name); else { final JTextField proxy = new JTextField(name); proxy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { name = proxy.getText(); } }); return proxy; } } }

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:

class EmployeeViewer extends JPanel { public EmployeeViewer(Employee e) { add(e.visualProxy(Employee.NAME_ATTRIBUTE,true); } } class EmployeeEditor extends JPanel { public EmployeeEditor(Employee e) { add(e.visualProxy(Employee.NAME_ATTRIBUTE,false); } }

The information presented on this page represents the personal views, ideas, and opinions of the author. This is not an official Ball State University web page. Links contained at this web site to other organizations are presented as a service and neither constitute nor imply university endorsement or warranty.
Valid XHTML 1.0 Strict Valid CSS! Creative Commons License
This page was last modified on January 14, 2018 at 07:34:32 PM EST