java模式之装饰模式
1. 什么叫装饰模式?
根据业务的需求,需要对一个类的方法进行增强的处理。
2. 为什么需要装饰模式?
拓展性更加的好,当觉得这个装饰不好的时候,可以直接拿下,不需要改变任何的代码。
3. 装饰模式的一个具体的应用? 电子发票系统
代码:
[java] 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;
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();
}
}
}
[java] 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 SalsOrder extends Order {
public SalsOrder(){
System.out.println("调用SalsOrder的构造方法");
}
public void print(){
super.print();
}
}
[java] 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;
public class ItemsLine {
private String itemsName;
private double unit ;
private int number;
private double subTotalPrice;
public String getItemsName() {
return itemsName;
补充:软件开发 , Java ,