Java 디자인 모드의 Strategy 모드

OO의 기초를 바탕으로 디자인 모델을 열심히 공부하기 시작합니다!디자인 모델은 자바 디자인에서 빠질 수 없는 것입니다!
Apple.java

package strategy;
/**
 * 
 * @author Andy
 *
 */
 
public class Apple implements Discountable {
  // 
  private double weight;
  //     BigDecimal;
    private double price;
    // 
  // private Discountor d = new AppleWeightDiscountor();
    // 
    private Discountor d = new ApplePriceDiscountor();
     
  public double getWeight() {
    return weight;
  }
   
  public void setWeight(double weight) {
    this.weight = weight;
  }
   
  public double getPrice() {
    return price;
  }
   
  public void setPrice(double price) {
    this.price = price;
  }

  public Apple (double weight,double price ){
   
    super();
    this.weight=weight;
    this.price=price;
  }
 
  @Override
  public void discountSell() {
     d.discount(this);
  }  
}

Banana.java

package strategy;
/**
 * 
 * @author Andy
 *
 */
public class Banana implements Discountable {
  // 
  private double weight;
////     BigDecimal
  private double price;
   
  public Banana(double weight, double price) {
    super();
    this.weight = weight;
    this.price = price;
  }
 
  public double getWeight() {
    return weight;
  }
   
  public void setWeight(double weight) {
    this.weight = weight;
  }
   
  public double getPrice() {
    return price;
  }
   
  public void setPrice(double price) {
    this.price = price;
  }
 
  @Override
  public void discountSell() {
    // 
    if(weight < 5) {
      System.out.println("Banana : " + weight * price);
    }else if(weight >= 5 && weight < 10) {
      System.out.println("Banana : " + weight * price * 0.88 );
    }else if(weight >= 10) {
      System.out.println("Banana : " + weight * price * 0.5 );
    }    
     
  }

}

Market.java

package strategy;
/**
 * 
 * @author Andy
 *
 */
public class Market {
   
  /**
   *  
   * @param apple
   */

  public static void discountSell(Discountable d) {
    d.discountSell();

}
}

Discountable.java

package strategy;
/**
 * 
 * @author Andy
 *
 */
public interface Discountable {
  public void discountSell();
}

Test.java

package strategy;
/**
 * 
 * @author Andy
 *
 */
public class Test {
   
  /**
   * 
   * @param args
   */
 
  public static void main(String[] args) {
//          
//    
    Discountable d = new Apple(10.3, 3.6);
    Discountable d1= new Banana(5.4,1.1);
      Market.discountSell(d);
      Market.discountSell(d1);
     
 
  }
 
}

좋은 웹페이지 즐겨찾기