1. //装饰者模式:就是在保证不改变原有对象的基础上,去扩展一些想要的方法或去求
  2. var CarInterface = new BH.Interface('CarInterface' , ['getPrice' , 'assemble']);
  3. var Car = function(car){ //也可以这样写类。
  4. //让子类都有这个属性
  5. this.car = car ;
  6. //检查接口
  7. BH.Interface.ensureImplements(this , CarInterface);
  8. };
  9. Car.prototype = {
  10. constructor :Car,
  11. getPrice:function(){
  12. return 200000 ;
  13. },
  14. assemble:function(){
  15. document.write('组装汽车...');
  16. }
  17. };
  18.  
  19. var LightDecorator = function(o){ //函数调用的时候执行
  20. //继承属性并传值,this.car = car,此时传进来的car是上面的Car对象。
  21. LightDecorator.superClass.constructor.call(this , o);
  22. /*
  23. 相当于复制代码:this.car = car ;
  24. BH.Interface.ensureImplements(this , CarInterface);
  25. */
  26. };
  27. BH.extend(LightDecorator , Car); //立即执行
  28.  
  29. LightDecorator.prototype = {
  30. constructor:LightDecorator ,
  31. getPrice:function(){
  32. return this.car.getPrice() + 10000;
  33. },
  34. assemble:function(){
  35. document.write('组装车灯...');
  36. }
  37. };
  38.  
  39. var IceBoxDecorator = function(o){
  40. //继承属性并传值,this.car = car,此时传进来的car是上面的LightDecorator对象。js没有多态概念,
  41. IceBoxDecorator.superClass.constructor.call(this , o);
  42. };
  43. BH.extend(IceBoxDecorator , Car); //原型继承
  44.  
  45. IceBoxDecorator.prototype = {
  46. constructor:IceBoxDecorator ,
  47. getPrice:function(){
  48. return this.car.getPrice() + 20000;
  49. },
  50. assemble:function(){
  51. document.write('组装车载冰箱...');
  52. }
  53. };
  54.  
  55. var car = new Car();
  56. alert(car.getPrice());
  57. car.assemble();
  58.  
  59. var L = new LightDecorator(car);
  60. alert(L.getPrice());
  61. L.assemble();
  62.  
  63. var I = new IceBoxDecorator(L);
  64. alert(I.getPrice());
  65. I.assemble();

  1. //装饰者 不仅可以用在类上, 还可以用在函数上
  2.  
  3. //返回一个当前时间的字符串表示形式
  4. function getDate(){
  5. return (new Date()).toString();
  6. };
  7.  
  8. // 包装函数 (装饰者函数)
  9. function upperCaseDecorator(fn){
  10. return function(){
  11. return fn.apply(this, arguments).toUpperCase();
  12. }
  13. };
  14.  
  15. alert(getDate());
  16.  
  17. var getDecoratorDate = upperCaseDecorator(getDate);
  18.  
  19. alert(getDecoratorDate());

js29--装饰着模式的更多相关文章

  1. 装饰者模式 Decoration

    1.什么是装饰者模式 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活.当使用装饰后,从外部系统的角度看,就不再是原来的那个对象了,而是使用一系列 ...

  2. JAVA装饰者模式(从现实生活角度理解代码原理)

    装饰者模式可以动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. 该模式的适用环境为: (1)在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职 ...

  3. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  4. 设计模式(九)装饰者模式(Decorator Pattern)

    一.引言 在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).A ...

  5. PHP 装饰器模式

    装饰器模式:是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能. [装饰器模式中主要角色] 抽象组件角色(Component):定义一个对象接口,以规范准备接受附加责任的对象,即可以给这 ...

  6. C#设计模式-装饰者模式

    在软件开发中,我们经常想要对一类对象添加不同的功能,例如要给手机添加贴膜,手机挂件,手机外壳等,如果此时利用继承来实现的话,就需要定义无数的类,如StickerPhone(贴膜是手机类).Access ...

  7. Java 的设计模式之一装饰者模式

    刚开始接触装饰者的设计模式,感觉挺难理解的,不够后来花了一个晚上的时间,终于有头绪了 装饰者设计模式:如果想对已经存在的对象进行装饰,那么就定义一个类,在类中对已经有的对象进行功能的增强或添加另外的行 ...

  8. 《Head First 设计模式》之装饰者模式

    作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5922248.html 模式名称 装饰者模式(Decorator Pattern) 需求 定义咖啡厅中的 ...

  9. DecoratorPattern(装饰器模式)

    /** * 装饰者模式 * @author TMAC-J * 总的来说,装饰者模式就是继承的应用 */ public class DecoratorPattern { interface Beans{ ...

  10. 设计模式-装饰器模式(Decrator Model)

    文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6057666.html 目录 1.概述 2.目的 3.结构组成 4.实现 5.总结 1.概 ...

随机推荐

  1. 百度API调用实例

    今天依据需求要从百度API中取出一些数据.这些操作包含:将坐标转换成百度坐标.依据转换的百度坐标进行特定的查询. 有需求的收藏下,免得下次手写浪费时间. 涉及到的操作有:JSON格式的字符解析.HTT ...

  2. Install the IIS 6.0 Management Compatibility Components in Windows 7 or in Windows Vista from Control Panel

    https://technet.microsoft.com/en-us/library/bb397374(v=exchg.80).aspx Install the IIS 6.0 Management ...

  3. django 笔记4 数据库操作

    django操作数据库 orm操作 对应关系 models.tb.objects.filter(id__gt=) models.tb.objects.filter(id=) models.tb.obj ...

  4. 2.cocos设置背景图片

    在bool HelloWorld::init()中加入如下代码 auto bg = Sprite::create("1.jpg"); if (bg) { bg->setPos ...

  5. Gym - 100625G Getting Through 计算几何+并查集

    http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...

  6. SGU 461 Wiki Lists dfs

    不难的题,不过蛮有意思的dfs #include <iostream> #include <cstdio> #include <fstream> #include ...

  7. Scrapy发送POST请求

    一.发送post请求需要将start_urls注释,然后重写start_requests方法二.使用yield scrapy.FormRequest(url=post_url, formdata=fo ...

  8. Mark Compact GC (Part two :Two-Finger)

    目录 Two-Finger算法 前提 概要 步骤一:移动对象 步骤二:更新指针 优缺点 表格算法 概要 步骤一:移动对象群 和 构筑间隙表格 移动对象群 构筑间隙表格 步骤二:更新指针 优缺点 Two ...

  9. PHP如何去掉多维数组的重复值

    1.定义函数 function array_unique_new($arr){ $t = array_map('serialize', $arr);//利用serialize()方法将数组转换为以字符 ...

  10. HttpComponents入门解析

    1 简介 超文本传输协议(http)是目前互联网上极其普遍的传输协议,它为构建功能丰富,绚丽多彩的网页提供了强大的支持.构建一个网站,通常无需直接操作http协议,目前流行的WEB框架已经透明的将这些 ...