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();
}
}