Pattern: Proxy


Context

There are occasions when it is convenient for one object to represent another object. Some techniques which can use this approach are:

Solution

Create a Proxy class with the same protocol as the subject class by subclassing both from a common Abstract Class.

The Proxy object should receive messages and forward them to the subject after performing any necessary actions. For example, in the case of the remote proxy, the proxy receives messages, packages them, and communicates them to the remote object.

Example

Imagine an access proxy that restricts access to a company payroll. It will only allow certain users to print the payroll information.

PayrollAccessProxy>>print
        (User current permissionsInclude: #payrollAccess)
                ifTrue: [self payroll print]

Related Patterns