Strategy mode _java of Java design pattern

Source: Internet
Author: User

Based on the OO Foundation, begin to learn design patterns carefully! Design patterns are essential in Java design!

Apple.java

Package strategy;
/**
 * * 
 @author Andy * */Public
 
class Apple implements discountable {
  //weight
  private double Weight;
  The exact calculation of design money in the actual development of unit Price is bigdecimal;
    private double price;
    Discount on purchase
  //private Discountor D = new Appleweightdiscountor ();
    At the purchase price discount
    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
   
  Able {//weight private double weight;////Unit price actual development involves money and other accurate calculations are used bigdecimal private double prices;
    Public Banana (double weight, double) {super ();
    This.weight = weight;
  This.price = Price;
  Public double Getweight () {return weight;
  The 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 () {//Discount algorithm if (weight < 5) {System.out.println ("Banana not Discounted price:")
    + weight * price);
    }else if (weight >= 5 && Weight <) {System.out.println ("banana dozen 88 percent prices:" + weight * price * 0.88);
    }else if (weight >=) {System.out.println ("banana call 50 percent Price:" + weight * prices * 0.5); }    
     
  }

}

Market.java

Package strategy;
/**
 * * @author Andy * * */public
class Market {
   
  /**
   * Discounts on a category of things to be discounted
   * @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) {
//    only discounts on apples you can't discount on one of the common things and sell what you want to do. Discount algorithm 
/   In fact, each kind of things discount algorithm is inconsistent
    discountable d = new Apple (10.3, 3.6);
    Discountable d1= New Banana (5.4,1.1);
      Market.discountsell (d);
      Market.discountsell (D1);
     
 
  }
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.