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

java模式之装饰模式的更多相关文章

  1. 代理模式 vs 装饰模式

    代理模式和装饰模式有很大的相似性,二者的类图(几乎)是一样的.下面分别讲解代理模式和装饰模式. 1.代理模式 一般著名的跑步运动员都会有自己的代理人,如果想联系该运动员的比赛事宜,可以直接联系他的代理 ...

  2. JAVA设计模式:装饰模式

    前面我们学习了代理模式: 代理模式主要使用了java的多态,干活的是被代理类,代理类主要是接活,你让我干活,好,我交给幕后的类去干,你满意就成,那怎么知道被代理类能不能干呢?同根就成,大家知根知底,你 ...

  3. 设计模式GOF23(结构型模式:代理模式,适配模式,桥接模式,组合模式,装饰模式,外观模式,享元模式)

    结构型模式: – 分类: • 适配器模式.代理模式.桥接模式.装饰模式.组合模式.外观模式.享元模式 – 核心作用:是从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题.   结构 ...

  4. Java模式之模板方法模式

    当我们遇到的业务逻辑具有大致相同的方式的时候,我们也许就该将这个业务逻辑抽象出来,采用模板方法,来进行封装我们的代码,提高代码的重用性,以及可维护性.下面是我的一个复习用的案例: 第一步:我们需要一个 ...

  5. 设计模式:代理模式 vs 装饰模式

    参考文章:https://www.cnblogs.com/luoxn28/p/5535877.html 代理模式和装饰模式非常类似,甚至代码都类似. 二者最主要的区别是: 代理模式中,代理类对被代理的 ...

  6. 【设计模式】 模式PK:代理模式VS装饰模式

    1.概述 对于两个模式,首先要说的是,装饰模式就是代理模式的一个特殊应用,两者的共同点是都具有相同的接口,不同点则是代理模式着重对代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,它着重类的功能 ...

  7. Java模式开发之责任链模式

    Java模式开发之责任链模式 从击鼓传花谈起 击鼓传花是一种热闹而又紧张的饮酒游戏.在酒宴上宾客依次坐定位置,由一人击鼓.击鼓的地方与传花的地方是分开的.以示公正. 開始击鼓时,花束就開始依次传递,鼓 ...

  8. Java设计模式(9)——结构型模式之装饰模式(Decorator)

    一.概述 动态地给一个对象添加一些额外的职责.就增加功能来说, Decorator模式相比生成子类更为灵活.该模式以对客 户端透明的方式扩展对象的功能. UML简图 角色 在持有Component的引 ...

  9. javascript模式 (3)——工厂模式和装饰模式

    上节我们讲解了单例模式,这节我们将继续讲解工厂模式和迭代器模式 工厂模式: 工厂模式的目的是为了方便的创建对象(可以在不知道构造的情况下),通过静态方法来实现,在java或c#等静态编译语言中需要通过 ...

随机推荐

  1. animation渐进实现点点点等待效果实例页面

    CSS代码: .ani_dot { font-family: simsun; } :root .ani_dot { display: inline-block; width: 1.5em; verti ...

  2. leetcode第38题--Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  3. SSIS如何引用外部DLL

    原文:SSIS如何引用外部DLL 当SSIS引用外部的DLL时,外部的DLL须满足以下条件: 1. DLL是强命名. 2. 加入到GAC (C:\WINDOWS\assembly),直接把DLL拉进目 ...

  4. ajax 请求数据

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. 如何在在网页上显示pdf文档

    ------解决方案--------------------通过flash插件 ------解决方案--------------------RAD PDF Release 2.7 http://www ...

  6. 打开VMware的系统出错

    打开VMware系统时,出现错误 “Invalid configuration file. File "I:/My Virtual Machines/Windows XP english P ...

  7. WCF、Web API、WCF REST、Web Service 区别

    Web Service It is based on SOAP and return data in XML form. It support only HTTP protocol. It is no ...

  8. Android 5.0之应用中实现材料设计—Material Design

    上午的时候在刷Google+,看到了Abraham Williams转发了一篇强文,是Android Developers网站新发的一篇博客—Implementing Material Design ...

  9. angularjs + seajs构建Web Form3

    angularjs + seajs构建Web Form前端(三) -- 兼容easyui 回顾 在上一章中使用了angular实现了ajax form和树形结构,经过以上两章对于angular的大致使 ...

  10. MFC双缓冲和裁剪问题导致闪烁

    问题描述: 应用场景:在对话框中,自定义一个MFC图形控件(为了描述方便,暂定为HSPaintControl),控件覆盖整个对话框的客户区,属于最底层的控件,在这之上放置了很多其他的小图形控件. 问题 ...