Categories
Algorithms & Design Design Pattern

Proxy Pattern

Account.java

public abstract class Account {
	
	protected String customerName;
	protected int accountId;
	protected String accountType;
	
	public Account(){
		
	}
	public Account(String customerName, int accountId, String accountType) {
		this.customerName=customerName;
		this.accountId=accountId;
		this.accountType=accountType;
	}

	public abstract void displayAccountInfo();
}

SalaryAccount.java

public class SalaryAccount extends Account {

	private double balance;
	private String companyName;
	
	
	public SalaryAccount(String customerName, int accountId, String accountType,String companyName) {
		super(customerName, accountId, accountType);
		this.balance=Math.random()*10000;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void displayAccountInfo() {
		System.out.println("Account Id ::" +this.accountId);
		System.out.println("customer Name ::" +this.customerName);
		System.out.println("accountType ::" +this.accountType);
		System.out.println("balance ::" +this.balance);
		System.out.println("Company Name ::" +this.companyName);
	}

	
}

ProxySalaryAccount.java

public class ProxySalaryAccount extends Account{

	private Account account;
	
	public ProxySalaryAccount(String customerName, int accountId, String accountType) {
		// TODO Auto-generated constructor stub
		super(customerName,accountId,accountType);
	}

	@Override
	public void displayAccountInfo(){
		getAccountDetails();
		account.displayAccountInfo();
	}

	public void getAccountDetails(){
		//loading actual account from database when only required, so that to save memory
		
		if(account==null)
			account = new SalaryAccount(customerName, accountId,accountType,"TechSoft");
	}
	
}

BankOfficer.java

public class BankOfficer {

	
	public BankOfficer() {
		// TODO Auto-generated constructor stub
	}

	public void authenticateCustomer(Account account){
		System.out.println("This is first step to authenticate, if not authenticated we will reject your request");
		System.out.println("You are authenticated : "+account.accountId);
	}
	
	public void addedRequestInQueue(Account account){
		System.out.println("Pls wait for your turn to come, we will call you in some time");
	} 
	
	public void processRequest(Account account){
		System.out.println("Here are your details");
		account.displayAccountInfo();
	}
	public void submitRequest(Account account){
		authenticateCustomer(account);
		addedRequestInQueue(account);
		processRequest(account);
	}

}


ProxyPatternDemo.java

public class ProxyPatternDemo {

	public ProxyPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	 //call BankOfficer
		BankOfficer officer  = new BankOfficer();
		
		//initially passing proxy object to avoid loading from database.
		//on actual process of display, real object is created by loading from database.
		//before actual process, there several steps which can work with proxy account.
		//also by authentication, we are controlling access to Original account so for that time only proxy account is used
		officer.submitRequest(new ProxySalaryAccount("Jany Jose", 1234, "Deposit"));

	}

}

Output

Leave a comment

Design a site like this with WordPress.com
Get started