L9 Design Patterns 2 - Proxy and Template Method and Builder design patterns Flashcards
Structural Pattern: Proxy
- A proxy object provides a surrogate or placeholder for another object in order to control access to it
– The proxy pattern involves creating a proxy class that implements the same interface as the class that it is standing-in for
– The proxy class stores an instance of the class that it is standing in for.
– The proxy then forwards requests to the real object
– Clients simply access the proxy
– In proxy pattern, a proxy class represents functionality of another class
Application of the proxy design
pattern
- Use the Proxy pattern whenever there is a need for a more versatile or sophisticated reference to an object.
- The level of indirection introduced by the Proxy pattern allows the design pattern to be used in several situations including:
- A remote proxy where an object provides a local representative for an object in a different address space.
- A method invocation on local object results in remote method invocation on the remote object
- A virtual proxy that enables the creation of expensive objects on demand as shown in our next example
- Virtual proxies provide some default results, if a more results are required, the proxy creates the real object
- A protection proxy that provides controlled access to the real object
- A remote proxy where an object provides a local representative for an object in a different address space.
Virtual Proxy: Reducing memory footprint of objects
- Consider the case where you want to minimise the memory footprint of an image object by loading it into memory only when necessary.
– First, create an Image interface and two the classes implementing the Image interface, i.e., ProxyImage and RealImage.
* ProxyImage is a a proxy class to reduce memory footprint of RealImage object loading.
– The Client, uses ProxyImage to get an Image object to load and display as it needs.
Proxy pattern: Image example
Behavioural Pattern: Template Method
- The template method pattern uses a an abstract class to defines a the skeleton of an algorithm in an operation, but lets sub-classes redefine certain steps of the algorithm without changing the structure of the algorithm
– The abstract class that contains a series of operations which define the algorithm steps is called the AbstractTemplate
– The method containing the algorithm is called the templateMethod
Template Method components
- The Template Method pattern has three main components :
- The Client that triggers the execution of the template.
- The AbstractTemplate, which is an abstract class that contains a series of operations which define the necessary steps for carrying out the execution of the algorithm.
- In the example class diagram the abstractTemplate class defines a templateMethod() method for executing step1, step2 and step3 in order.
- The templateMethod() is declared as ‘final’ to prevent it from being overridden by subclasses
- The Step1(), step2() and step3() methods are protected abstract methods, so can only be called by subclasses of the AbstractTemplate class
- The Implementation classes represent concrete templates which inherit from AbstractTemplate and implement its abstract methods.
Behavioural Pattern: Template Method
- The Template Method lets sub-classes redefine certain steps of an algorithm without changing the algorithm’s structure
– In other words, it defines the skeleton of algorithm in a method, deferring some steps to sub-classes. - Consider an order processing system for a company that has an online and street presence. The order processing steps are: doSelect, doPayment, giftWrap (if required), and doDelivery
Template method pattern
Combining Template Method and Observer patterns to improve Order Processing for online shoppers
Builder Design Pattern
The Builder separates the construction of a complex object from its representation, so the same construction process process can create different representations
– It facilitates the construction of a complex object by separating the individual steps into separate methods in a Builder hierarchy,
– Then using a Director object to specify the required steps in the correct order.
– Finally, the finished product is retrieved from the Builder.
Code example for Pizza class
//Product
public class Pizza{
private String dough = “”;
private String sauce = “”;
private String topping = “”;
public void setDough(String dough){
this.dough = dough; }
public void setSauce(String sauce){
this.sauce = sauce; }
public void setTopping(String topping){
this.topping = topping; }
}