Decoration Mode 2-Invoice system

Source: Internet
Author: User
Tags abstract

[keywords]:java,design pattern, design pattern, Java and pattern learning, decorator, decoration mode

[Environment]:staruml5.0 + JDK6

[Author]:winty (wintys@gmail.com)

[Body]:

  Package pattern.decorator.order;
  Import java.util.*;
  Import java.text.*;
  /** * Decoration mode (decorator pattern): Invoice System * * Invoice is divided into three parts: Invoice head, invoice body, invoice end.
  * Where the invoice header and the end of the invoice are likely to change frequently.    * Run Result: Bill Gates 2009-6-6 ==================== name price milk 5.4 10¥54.00 Bread 7.3 3¥21.90 Salad 4.2 2¥8.40 ==================== Total price:¥84.30 client: Bill Gates Time: 2009 -6-6 ==================== Name price milk 5.4 10¥54.00 Bread 7.3 3¥21.90 ======= ============= Total price:¥75.90 * @version 2009-06-06 * @author winty (wintys@gmail.com)/public class Orderte
        st{public static void Main (string[] args) {//Create Item milk = new Item ("Milk", 5.4,10);
        Item bread = new Item ("Bread", 7.3, 3);
        Item salad = new Item ("Salad", 4.2, 2);
        Create invoice Order sales = new SalesOrder ();
        Sales.setcustomername ("Bill Gates");
   Sales.setsalesdate (New Date ());     Sales.additem (milk);
        Sales.additem (bread);
        
        Order of order;
        Add the invoice to the tail, then output order = new Footerdecorator (new Headerdecorator (sales));
        The decorated order is transparent to the client Order.additem (salad);
        Order.print ();
        System.out.println ("");
        Different packing order, output order of the same sequence = new Anotherheaderdecorator (new Footerdecorator (sales));
        The Remove Order.removeitem (salad) can also be safely invoked;        
    Order.print ();
    }/** * Product/class item{private String name; Private double price;//unit price private int amount;//Purchase quantity public item () {} public item (String name, double pric
        e, int amount) {this.name = name;
        This.price = Price;
    This.amount = amount;
    Public String GetName () {return this.name;
    public void SetName (String name) {this.name = name;
    Public double GetPrice () {return this.price; } public void Setprice (doublePrice) {this.price = Price;
    public int Getamount () {return this.amount;
    The public void Setamount (int amount) {this.amount = amount;
    }/** * Abstract component Role * * order{protected list<item> items;
    Private String CustomerName;
    Private Date salesdate;
    Public order () {items = new arraylist<item> ();
    public abstract void print ();
    Add merchandise public void AddItem (item item) {Items.Add (item);
    //Delete merchandise public void RemoveItem (item item) {Items.remove (item);
        Public double Caltotalprice () {Double total = 0.0;
        iterator<item> list = Items.iterator ();
            while (List.hasnext ()) {item = List.next ();
        Total + = Item.getprice () * Item.getamount ();
    Return to total;
    Public String Getcustomername () {return customerName; public void Setcustomername (String CustomerName) {this.customername = CustomerName;
    Public Date Getsalesdate () {return salesdate;
    The public void Setsalesdate (Date salesdate) {this.salesdate = salesdate;
    public static String FormatDate (Date mydate) {return dateformat.getdateinstance (). Format (mydate); public static String FormatCurrency (double currency) {return numberformat.getcurrencyinstance (). Format (Curre
    ncy); }/** * Specific component Role/class SalesOrder extends order{/** * Output Commodity information * * @Override Publ
        IC void print () {printitemheader ();
        iterator<item> list = Items.iterator ();
        while (List.hasnext ()) {print (List.next ()); }/** * Output individual product information */private void print (item) {Double subtotal = Item.getamount () *item
        . GetPrice ();
        String str = item.getname () + "T";
        STR + + item.getprice () + "T"; str = Item.getamounT () + "" T ";
        str = order.formatcurrency (subtotal);
    System.out.println (str);
        /** * Output Item Header/private void Printitemheader () {String Header = "T price" T number "T";
    System.out.println (header);
  }/** * Abstract decorative role * * Only the method (Caltotalprice ()) that is reassigned to the invocation, * or the method (Setcustomername () that replicates the data), can be invoked safely by the multiple decorated subclass.
    * * Abstract class Orderdecorator extends order{private order;
        Public Orderdecorator (order) {this.order = order;
        No "delegation" was used to copy the value directly This.setcustomername (Order.getcustomername ());
    This.setsalesdate (Order.getsalesdate ());
    @Override public void print () {order.print ();
    @Override public Double Caltotalprice () {return order.caltotalprice ();
    @Override public void AddItem (item) {Order.additem (item);
    @Override public void RemoveItem (item) {Order.removeitem (item); 
}  /** * Print Invoice Header decoration */class Headerdecorator extends orderdecorator{public headerdecorator (order) {
    Super (order);
        @Override public void print () {printheader ();
    Super.print ();
        private void Printheader () {String header = Getcustomername () + "T";
        Header + order.formatdate (Getsalesdate ());
        System.out.println (header);
    System.out.println ("====================");
        }/** * Print invoice at the end of the decoration */class Footerdecorator extends orderdecorator{public footerdecorator (order) {
    Super (order);
        @Override public void print () {super.print ();
    Printfooter ();
        private void Printfooter () {String total;
        Total = Order.formatcurrency (Caltotalprice ());
        System.out.println ("====================");
    SYSTEM.OUT.PRINTLN ("Total Price:" + total); }/** * Another kind of print invoice Header decoration class/class Anotherheaderdecorator extends Orderdecorator{Public Anotherheaderdecorator (order) {super (order);
        @Override public void print () {printheader ();
    Super.print ();
        private void Printheader () {String header;
        Header = "Customer:" + getcustomername () + "n";
        Header = + "time:" + order.formatdate (Getsalesdate ());
        System.out.println (header);
    System.out.println ("===================="); }   }

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.