Monday 30 December 2013

Strategy Design pattern - Implementation [Payment]



Click here to watch in Youtube : https://www.youtube.com/watch?v=o-rSyfmBqh0

Click the below Image to Enlarge
Strategy Design pattern - Implementation [Payment]
Strategy Design pattern - Implementation [Payment] - Class Diagram




































PaymentStrategy.java

public interface PaymentStrategy
{
public void pay(String amount);

}


DebitCardPaymentStrategy.java

public class DebitCardPaymentStrategy implements PaymentStrategy
{

public void pay(String amount)
{
System.out.println("Customer pays the money " +amount+ "Rs. using Debit Card");
}

}

CreditCardPaymentStrategy.java

public class CreditCardPaymentStrategy implements PaymentStrategy
{

public void pay( String amount )
{
System.out.println("Customer pays the money " + amount + "Rs. using Credit Card");
}

}

ByCashPaymentStrategy.java

public class ByCashPaymentStrategy implements PaymentStrategy
{

public void pay( String amount )
{
System.out.println("Customer pays the money " + amount + "Rs. as a Cash");
}

}


PaymentContext.java

public class PaymentContext
{
private PaymentStrategy paymentStrategy;

// Client will set what PaymentStrategy to use by calling this method
public void setPaymentStrategy(PaymentStrategy strategy)
{
this.paymentStrategy = strategy;
}

public PaymentStrategy getPaymentStrategy()
{
return paymentStrategy;
}

public void pay(String amount)
{
paymentStrategy.pay(amount);
}

}


Customer.java

import java.util.Scanner;

public class Customer
{

public static void main( String[] args )
{

System.out.println("Please enter Payment Type : 'CreditCard' or 'DebitCard' or 'ByCash'");
Scanner scanner = new Scanner(System.in);
String paymentType = scanner.next();
System.out.println("Payment type is : " + paymentType);

System.out.println("\nPlease enter Amount to Pay : ");
Scanner scanner1 = new Scanner(System.in);
String amount = scanner1.next();
System.out.println("amount is : " + amount);

PaymentContext ctx = null;
ctx = new PaymentContext();

if( "CreditCard".equalsIgnoreCase(paymentType) )
{
ctx.setPaymentStrategy(new CreditCardPaymentStrategy());
}
else if( "DebitCard".equalsIgnoreCase(paymentType) )
{
ctx.setPaymentStrategy(new DebitCardPaymentStrategy());
}
else if( "ByCash".equalsIgnoreCase(paymentType) )
{
ctx.setPaymentStrategy(new ByCashPaymentStrategy());
}

System.out.println("PaymentContext has :"+ctx.getPaymentStrategy());

ctx.pay(amount);

}
}

Output

Please enter Payment Type : 'CreditCard' or 'DebitCard' or 'ByCash'
CreditCard
Payment type is : CreditCard

Please enter Amount to Pay :
90000
amount is : 90000
PaymentContext has :CreditCardPaymentStrategy@1e4cbc4
Customer pays the money 90000Rs. using Credit Card


Please enter Payment Type : 'CreditCard' or 'DebitCard' or 'ByCash'
DebitCard
Payment type is : DebitCard

Please enter Amount to Pay :
90000
amount is : 90000
PaymentContext has :DebitCardPaymentStrategy@1e4cbc4
Customer pays the money 90000Rs. using Debit Card


See also:



  • Strategy Design pattern - Introduction
  • Strategy Design pattern - Real Time Example [Compress files]
  • Strategy Design pattern - Real Time Example [Payment]
  • Strategy Design pattern - Real Time Example [Travel]
  • Strategy Design pattern - Real Time Example [Sorting]
  • Strategy Design pattern - Real Time Example [Search]
  • Strategy Design pattern - Class Diagram
  • Strategy Design pattern - Sequence Diagram
  • Strategy Design pattern - Implementation [Compress files]
  • Strategy Design pattern - Implementation [Travel]
  • Strategy Design pattern - Implementation [Search]
  • Strategy Design pattern - Implementation [Sort]
  • Strategy Design pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment