Gang of Four Structural pattern: Proxy
An object representing another object. Provide a surrogate or placeholder for another object to control access to it.
The proxy class in this example, TriangleProxy, proxies the TriangleShape Subject. After calling drawShape() on the subject it logs to System.out how many times the drawShape() method has been called
The proxy pattern is similar to the decorator pattern. One of the differences is that with the Proxy pattern the relationship between a proxy and the subject is usually defined at compile time, whereas with decorators the relationship between the decorator and the class being decorated can be defined at runtime.
To get the code for this example:
git clone https://github.com/spotadev/gangoffour.git
In src/main/java navigate to this package:
com.javaspeak.designpatterns.go4.structural.proxy
You can run the code from the main method of:
ProxyApplication
In our example the ShapeProxy counts and logs to System.out how many times the drawShape() method has been called every time it is invoked.
package com.javaspeak.designpatterns.go4.structural.proxy;
/**
* Text book description:
* <ul>
* Proxy: An object representing another object. Provide a surrogate or placeholder for
* another object to control access to it.
* </ul>
* The proxy class in this example, TriangleProxy, proxies the TriangleShape Subject. After
* calling drawShape() on the subject it logs to System.out how many times the drawShape()
* method has been called
* <p>
* The proxy pattern is similar to the decorator pattern. One of the differences is that with
* the Proxy pattern the relationship between a proxy and the subject is usually defined at
* compile time, whereas with decorators the relationship between the decorator and the class
* being decorated can be defined at runtime.
* <p>
* In our example the ShapeProxy counts and logs to System.out how many times the drawShape()
* method has been called every time it is invoked.
* <p>
* @author John Dickerson - 22 Feb 2020
*/
public class ProxyApplication {
public void runExample() {
Shape triangleProxy = new TriangleProxy();
triangleProxy.drawShape();
triangleProxy.drawShape();
}
/**
* Main method
*
* @param args
*/
public static void main( String[] args ) {
ProxyApplication application = new ProxyApplication();
application.runExample();
}
}
package com.javaspeak.designpatterns.go4.structural.proxy;
/**
* Describes methods of Shape
*
* @author John Dickerson - 23 Feb 2020
*/
public interface Shape {
/**
* Draws Shape to System.out
*/
public void drawShape();
}
package com.javaspeak.designpatterns.go4.structural.proxy;
/**
* Implementation of Shape. This is the class we are proxying.
*
* @author John Dickerson - 23 Feb 2020
*/
public class Triangle implements Shape {
@Override
public void drawShape() {
StringBuilder sb = new StringBuilder();
sb.append( " x \n" );
sb.append( " x x \n" );
sb.append( " x x \n" );
sb.append( "xxxxxxx\n" );
System.out.println( sb.toString() );
}
}
package com.javaspeak.designpatterns.go4.structural.proxy;
/**
* @author John Dickerson - 23 Feb 2020
*/
/**
* This proxy class proxies the shape Subject. After calling drawShape() on the subject it logs
* to Systm.out how many times the drawShape() method has been called.
* <p>
* Note that the relationship to the subject is ususally determined at compile time with the proxy
* pattern.
* <p>
* With the decorator pattern which is quite similar the relationship is usually defined at runtime.
* A runtime relationship means that the class being decorated is passed in via a constructor or
* setter and a compile time relationship means the proxy is hard coded to proxy a specific class.
* <p>
* Decorators are often chained together while proxies are not chained.
*
* @author John Dickerson - 23 Feb 2020
*/
public class TriangleProxy implements Shape {
private Shape subject;
private int numberTimesInvoked;
/**
* Constructor
* <p>
* Instantiates subject we are proxying
*/
public TriangleProxy() {
this.subject = new Triangle();
}
@Override
public void drawShape() {
this.subject.drawShape();
numberTimesInvoked++;
System.out.println( "drawShape() has been invoked " + numberTimesInvoked + " times" );
}
}
Back: Gang of Four
Page Author: JD