COMPOSITE PATTERN, PROXY PATTERN AND DECORATOR PATTERN

Prakash Jha
3 min readSep 8, 2020

COMPOSITE PATTERN

Composite pattern is used where we need to treat a group of objects in similar way as a single object. Composite pattern composes objects in term of a tree structure to represent part as well as whole hierarchy. This type of design pattern comes under structural pattern as this pattern creates a tree structure of group of objects.

This pattern creates a class that contains group of its own objects. This class provides ways to modify its group of same objects.

We are demonstrating use of composite pattern via following example in which we will show employees hierarchy of an organization.

lets have look at example of employee and company

import java.util.ArrayList; 
import java.util.List;
interface Employee {
public void showEmployeeDetails();
}
class Developer implements Employee {
private String name;
private long empId;
private String position;

public Developer(long empId, String name, String position)
{
this.empId = empId;
this.name = name;
this.position = position;
}

@Override
public void showEmployeeDetails()
{
System.out.println(empId+" " +name);
}
}
class Manager implements Employee {
private String name;
private long empId;
private String position;

public Manager(long empId, String name, String position)
{
this.empId = empId;
this.name = name;
this.position = position;
}

@Override
public void showEmployeeDetails()
{
System.out.println(empId+" " +name);
}
}
class CompanyDirectory implements Employee {
private List<Employee> employeeList = new ArrayList<Employee>();

@Override
public void showEmployeeDetails()
{
for(Employee emp:employeeList)
{
emp.showEmployeeDetails();
}
}

public void addEmployee(Employee emp)
{
employeeList.add(emp);
}

public void removeEmployee(Employee emp)
{
employeeList.remove(emp);
}
}
public class compositepatt {
public static void main (String[] args) {
Developer dev1 = new Developer(100, "Lokesh Sharma", "Pro Developer");
Developer dev2 = new Developer(101, "Vinay Sharma", "Developer");
CompanyDirectory engDirectory = new CompanyDirectory();
engDirectory.addEmployee(dev1);
engDirectory.addEmployee(dev2);
engDirectory.showEmployeeDetails();

Manager man1 = new Manager(200, "Kushagra Garg", "SEO Manager");
Manager man2 = new Manager(201, "Vikram Sharma ", "Kushagra's Manager");

CompanyDirectory accDirectory = new CompanyDirectory();
accDirectory.addEmployee(man1);
accDirectory.addEmployee(man2);
accDirectory.showEmployeeDetails();

CompanyDirectory directory = new CompanyDirectory();
directory.addEmployee(engDirectory);
directory.addEmployee(accDirectory);
directory.showEmployeeDetails();
}
}

PROXY DESIGN PATTERN

Proxy means ‘in place of’, representing’ or ‘in place of’ or ‘on behalf of’ are literal meanings of proxy and that directly explains Proxy Design Pattern.
Proxies are also called surrogates, handles, and wrappers.

A real world example can be a cheque or credit card is a proxy for what is in our bank account. It can be used in place of cash, and provides a means of accessing that cash when required. And that’s exactly what the Proxy pattern does — “Controls and manage access to the object they are protecting“.

interface Image {
void display();
}
class RealImage implements Image {private String fileName;public RealImage(String fileName){
this.fileName = fileName;
loadFromDisk(fileName);
}
@Override
public void display() {
System.out.println("Displaying " + fileName);
}
private void loadFromDisk(String fileName){
System.out.println("Loading " + fileName);
}
}
class ProxyImage implements Image{private RealImage realImage;
private String fileName;
public ProxyImage(String fileName){
this.fileName = fileName;
}
@Override
public void display() {
if(realImage == null){
realImage = new RealImage(fileName);
}
realImage.display();
}
}
public class proxypattern {

public static void main(String[] args) {
Image image = new ProxyImage("test_10mb.jpg");
//image will be loaded from disk
image.display();
System.out.println("");

//image will not be loaded from disk
image.display();
}
}

DECORATOR DESIGN PATTERN

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

This pattern creates a decorator class which wraps the original class and provides additional functionality keeping class methods signature intact.

We are demonstrating the use of decorator pattern via following example in which we will decorate a shape with some color without alter shape class.

Implementation

We’re going to create a Shape interface and concrete classes implementing the Shape interface. We will then create an abstract decorator class ShapeDecorator implementing the Shape interface and having Shape object as its instance variable.

RedShapeDecorator is concrete class implementing ShapeDecorator.

DecoratorPatternDemo, our demo class will use RedShapeDecorator to decorate Shape objects.

Credits — GeeksForGeeks and tutorialspoint

--

--