Search This Blog

Thursday 13 October 2016

Proxy design pattern

Proxy design pattern

It is structural design pattern; It is place holder for original object and limits the access to it. This provides interface original object functionality to clients.This represents functionality of another class which is sensitive remote target object.
Client deals with Proxy class which delegates to the original object (target object).

Proxy class which holds the original object and provides interface and  delegates to the target.

Proxy Demo:

BankAccount.java
------------------------
class BankAccount {
int accountNo;
String name
                float amount;
String accountType;
String bankName;
String address;
int customerID;
           
              public BankAccount() {
accountNo = 1111111111111;
name = "abc"
                amount = 10000;
accountType = "Savings";
bankName = "XYZ Bank";
address = "D No-111 efgh street";
customerID = 12345;
}

public boolean withdraw(float _amount) {
boolean isWithdrawSuccess = false;
if (amount > 0 && amount >= _amount) {
amount -= _amount;
isWithdrawSuccess = true;
}
return isWithdrawSuccess;
}

public void deposit(float _amount) {
amount += _amount;
}
public int getAccountNo() {
return accountNo;
}

public void setAccountNo(int _accountNo) {
accountNo = _accountNo;
}

public String getName() {
return name;
}

public void setName(String _name) {
name =_name;
}

  public float getAmount() {
return amount;
}

public void setAmount(float _amount) {
amount = _amount;
}

public String getAccountType() {
return accountType;
}

public void setAccountType(String _accountType){
accountType= _accountType;
}

public String getBankName() {
return bankName;
}

public void setBankName(String _bankName) {
bankName = _bankName;
}

public String getAddress() {
return address;
}

public void setAddress(String _address) {
address = _address;
}

public int getCustomerID() {
return customerID;
}

public void setCustomerID(int _customerID) {
customerID =_customerID;
}

}

 interface OrderOperations {
  AccountDetails getAccountDetails();
  void  bookOrder(float orderAmount, String customerType);
  void unbookOrder(float orderAmount, String customerType);
}
AccountDetails.java
---------------------------
class AccountDetails {
String name;
float amount;
public String getName() {
return name;
}
public void setName(String _name) {
name =_name;
}
public float getAmount() {
return amount;
}
public void setAmount(float _amount) {
amount = _amount;
}

}
ProxyOrderOperationsImpl.java
-------------------------------------------
class ProxyOrderOperationsImpl implements OrderOperations {
BankAccount customerAccount;
ProxyOrderOperationsImpl() {
customerAccount = new BankAccount();
}

public AccountDetails getAccountDetails() {
AccountDetails  accountdetails = new AccountDetails();
System.out.println("***Account Details***");
System.out.println("Name:::" + customerAccount.getName());
accountdetails.setName(customerAccount.getName());
System.out.println("Amount:::" + customerAccount.getAmount());
accountdetails.setAmount(customerAccount.getAmount());
   return accountdetails;
}

public void  bookOrder(float orderAmount, String customerType) {
if (orderAmount > 5000) {
if("Regular".equalIgnoreCase(customerType)) {
orderAmount-=500;
} else {
orderAmount-=100;
}
}
boolean isBookOrder = customerAccount.withdraw(orderAmount);
if (isBookOrder) {
System.out.println("Order Booking is successful...");
} else {
System.out.println("Order Booking is un-successful...");
}

}
public void unbookOrder(float orderAmount, String customerType) {
        if (orderAmount > 5000) {
if("Regular".equalIgnoreCase(customerType)) {
orderAmount-=500;
} else {
orderAmount-=100;
}
        }
boolean isUnbookOrder = customerAccount.deposit(orderAmount);
if (isUnbookOrder ) {
System.out.println("Order Unbooking is successful...");
} else {
System.out.println("Order Unbooking is un-successful...");
}
}

}
PrxoyPatternDemo.java
--------------------------------
public PrxoyPatternDemo {
public static void main (String args[]) {
ProxyOrderOperationsImpl orderOperations = new ProxyOrderOperationsImpl();
orderOperations.getAccountDetails();
orderOperations.bookOrder(3000, "Regular");
orderOperations.unbookOrder(3000,"Regular");
}
}


No comments:

Post a Comment