SHARE
TWEET

Untitled

a guest Mar 20th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2.  
  3. /* Interface for Strategy */
  4. interface OfferStrategy {
  5.     public String getName();
  6.     public double getDiscountPercentage();
  7. }
  8. /* Concrete implementation of base Strategy */
  9. class NoDiscountStrategy implements OfferStrategy{
  10.     public String getName(){
  11.         return this.getClass().getName();
  12.     }
  13.     public double getDiscountPercentage(){
  14.         return 0;
  15.     }
  16. }
  17. /* Concrete implementation of base Strategy */
  18. class QuarterDiscountStrategy implements OfferStrategy{
  19.     public String getName(){
  20.         return this.getClass().getName();
  21.     }
  22.     public double getDiscountPercentage(){
  23.         return 0.25;
  24.     }
  25. }
  26. /* Context is optional. But if it is present, it acts as single point of contact
  27.    for client.
  28.    
  29.    Multiple uses of Context
  30.    1. It can populate data to execute an operation of strategy
  31.    2. It can take independent decision on Strategy creation.
  32.    3. In absence of Context, client should be aware of concrete strategies. Context acts a wrapper and hides internals
  33.    4. Code re-factoring will become easy
  34. */
  35. class StrategyContext {
  36.     double price; // price for some item or air ticket etc.
  37.     Map<String,OfferStrategy> strategyContext = new HashMap<String,OfferStrategy>();
  38.     StrategyContext(double price){
  39.         this.price= price;
  40.         strategyContext.put(NoDiscountStrategy.class.getName(),new NoDiscountStrategy());
  41.         strategyContext.put(QuarterDiscountStrategy.class.getName(),new QuarterDiscountStrategy());        
  42.     }
  43.     public void applyStrategy(OfferStrategy strategy){
  44.         /*
  45.         Currently applyStrategy has simple implementation. You can Context for populating some more information,
  46.         which is required to call a particular operation            
  47.         */
  48.         System.out.println("Price before offer :"+price);
  49.         double finalPrice = price - (price*strategy.getDiscountPercentage());
  50.         System.out.println("Price after offer:"+finalPrice);
  51.     }
  52.     public OfferStrategy getStrategy(int monthNo){
  53.         /*
  54.             In absence of this Context method, client has to import relevant concrete Strategies everywhere.
  55.             Context acts as single point of contact for the Client to get relevant Strategy
  56.         */
  57.         if ( monthNo < 6 )  {
  58.             return strategyContext.get(NoDiscountStrategy.class.getName());
  59.         }else{
  60.             return strategyContext.get(QuarterDiscountStrategy.class.getName());
  61.         }
  62.        
  63.     }
  64. }
  65. public class StrategyDemo{    
  66.     public static void main(String args[]){
  67.         StrategyContext context = new StrategyContext(100);
  68.         System.out.println("Enter month number between 1 and 12");
  69.         int month = Integer.parseInt(args[0]);
  70.         System.out.println("Month ="+month);
  71.         OfferStrategy strategy = context.getStrategy(month);
  72.         context.applyStrategy(strategy);
  73.     }
  74.    
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top