日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java模式之装饰模式

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java模式之装饰模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 什么叫裝飾模式?

根據業務的需求,需要對一個類的方法進行增強的處理。

?

2. 為什么需要裝飾模式?

拓展性更加的好,當覺得這個裝飾不好的時候,可以直接拿下,不需要改變任何的代碼。

?

3. 裝飾模式的一個具體的應用??? 電子發票系統

代碼:

package com.huxin.decorator.test; import java.util.Date; import java.util.Vector;abstract public class Order {private String name;private Date date ;private Vector itemsLinelist = new Vector(10);public Order(){System.out.println("調用order的構造方法");}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}public double getTotalPrice() {double price = 0.0d;for(int i =0 ;i<itemsLinelist.size();i++){ItemsLine itemsLine = (ItemsLine)itemsLinelist.get(i);price +=itemsLine.getSubTotalPrice();}return price;}public void addItems(ItemsLine itemsLine){itemsLinelist.add(itemsLine);}public void removeItems(ItemsLine itemsLine){itemsLinelist.remove(itemsLine);}public void print(){ System.out.println("order==================================================");for(int i =0 ;i<itemsLinelist.size();i++){ItemsLine itemsLine = (ItemsLine)itemsLinelist.get(i);itemsLine.print();}} }


?

package com.huxin.decorator.test;public class SalsOrder extends Order {public SalsOrder(){System.out.println("調用SalsOrder的構造方法");}public void print(){super.print();} }


?

package com.huxin.decorator.test;public class ItemsLine {private String itemsName;private double unit ;private int number;private double subTotalPrice;public String getItemsName() {return itemsName;}public void setItemsName(String itemsName) {this.itemsName = itemsName;}public double getUnit() {return unit;}public void setUnit(double unit) {this.unit = unit;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public double getSubTotalPrice() {return number * unit;}public void print(){System.out.println("購買商品的名字為:"+ itemsName+ "單價為:"+ unit+ "數量為"+ number);} }


?

package com.huxin.decorator.test;abstract public class AbstractDecorator extends Order{protected Order order; //裝飾類必須接受這個order對象public AbstractDecorator(Order order){this.order = order;this.setDate(order.getDate());this.setName(order.getName());}public void print(){super.print();} }


?

package com.huxin.decorator.test;public class FootDecorator extends AbstractDecorator {//裝飾類必須接受這個order對象public FootDecorator(Order order){super(order); System.out.println("我后被創建FootDecorator");}public void print(){order.print();printFooter();}public void printFooter(){ System.out.println("foot==================================================");System.out.println("總價格為:"+ super.order.getTotalPrice());} }


?

package com.huxin.decorator.test;public class HeaderDecorator extends AbstractDecorator {//裝飾類必須接受這個order對象public HeaderDecorator(Order order){super(order); System.out.println("我先被創建HeaderDecorator");}public void print(){printHeader();super.order.print();}public void printHeader(){ System.out.println("header==================================================");System.out.println("顧客的姓名為:"+ super.order.getName() +"日期為:"+ super.order.getDate());} }


?

package com.huxin.decorator.test;import java.util.Date;public class Client {private static Order order;public static void main(String[] args) {order = new SalsOrder();ItemsLine itemsLine = new ItemsLine();itemsLine.setItemsName("杯子");itemsLine.setUnit(8.0d);itemsLine.setNumber(1);ItemsLine itemsLine2 = new ItemsLine();itemsLine2.setItemsName("瘋狂java");itemsLine2.setUnit(610d);itemsLine2.setNumber(2);order.addItems(itemsLine2);order.addItems(itemsLine);order.setDate(new Date());order.setName("胡鑫");order= new HeaderDecorator(new FootDecorator(order));order.print();} }


?

?

?

?

轉載于:https://www.cnblogs.com/jiangu66/p/3165477.html

總結

以上是生活随笔為你收集整理的java模式之装饰模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。