Imaging you have a large application, inside this large application you have many small individual applications.

main app

/             |               \

small A     Small B     Small C

For example you are building a redirection service in main app, you want to share some common partten code with small apps A,B,C.

There is one function inside redirection servcice:

Class RedirectionService {
getUrl ({state, isCustomer, namespace}) { }
}

It takes one object which contains 'state', which means in different state, we should redirection to different URL.

Second params is 'isCustomer', which tell whether current user is onboarded customer or not.

Last is 'namespace', which refer to Small A or Small B or Small C applications. Those applications have differnet url configed.

Now the problem is everytime from Small application, I have to tell all three params in order to make it works:

// small A
RedirectService.getUrl ({state: 'some state', isCustomer: CustomerA.isCustomer, namespace: 'small_A'})
RedirectService.getUrl ({state: 'some state_a2', isCustomer: CustomerA.isCustomer, namespace: 'small_A'}) // small B
RedirectService.getUrl ({state: 'some state_b1', isCustomer: CustomerB.isCustomer, namespace: 'small_B'})
RedirectService.getUrl ({state: 'some state_b2', isCustomer: CustomerB.isCustomer, namespace: 'small_B'})

So what we actually prefer is calling like this:

// Small A
RedirectService.getUrl('some state_a1') // Small B
RedirectService.getUrl('some state_b1')

From small applciation component level, we only care 'state',  we don't want to take care 'isCustomer' or 'namespace'.

To achieve this, we can use 'decorator' it is great way to code reuse.

// Take care isCustomer, by inject Customer service for based on different applications.
function NewRedirectionService($delegate, CustomerA) {
// store original function from Redirection Service
const getUrl = $delegate.getUrl; // Create a high order function to wrap original function
¨ // for small apps, they will call this new function instead
function newGetUrl(state) {
// apply common params and call original function
return getUrl.call($delegate, {state, isCustomer: CustomerA.isCustomer, namespace: 'small_A'});
} // override original function by new function.
$delegate.getUrl = newGetUrl; return $delegate;
} // Re-declare 'RedirectService' by using 'decorator'
angular.module('shared').decorator('RedirectionService', NewRedirectionService);

As you can see now, from compmonent level,

before:

RedirectService.getUrl ({state: 'some state', isCustomer: CustomerA.isCustomer, namespace: 'small_A'})

now:

RedirectService.getUrl ('some state')

[AngularJS] Decorator pattern for code reuse的更多相关文章

  1. 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

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

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

  3. 装饰模式(Decorator pattern)

    装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...

  4. angularjs decorator

    angularjs decorator https://docs.angularjs.org/guide/decorators decorator() $provide服务提供了在服务实例创建时对其进 ...

  5. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  6. 深入浅出设计模式——装饰模式(Decorator Pattern)

    模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...

  7. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  8. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  9. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...

随机推荐

  1. js中的var a = new A;与var a = new A()的区别

    JavaScript 中的new关键字与C#,JAVA中的概念完全不一样.  例:var a=new A();  让我们来看看在JavaScript中的new发生了什么?  var a={};//建立 ...

  2. jdk1.8 -- optional 的使用

    一.optional的介绍 Optional 是个容器:它可以保存类型T的值,或者仅仅保存null.Optional提供很多有用的方法,这样我们就不用显式进行空值检测. Optional 类的引入很好 ...

  3. Spark Scala当中reduceByKey(_+_) reduceByKey((x,y) => x+y)的用法

    [学习笔记] reduceByKey(_+_)是reduceByKey((x,y) => x+y)的一个 简洁的形式*/ val rdd08 = sc.parallelize(List((1, ...

  4. 如何配置kindeditor的工具栏

    kindeditor编辑器的工具栏主要是指编辑器输入框上方的那些可以操作的菜单,默认情况下编辑器是给予了所有的工具栏.针对不同的用户,不同的项目,不同的环境,可能就需要保留部分工具栏.那么我们应该如何 ...

  5. 虚拟机(VM)安装openwrt-koolshare软路由

    ⒈创建虚拟机 **软路由选择Windows操作系统,因为我们需要在PE环境中进行软路由的写入,固件类型选择BIOS,网络类型选择使用仅主机模式网络,虚拟磁盘类型选择IDE[一定要选择IDE模式],SC ...

  6. MySQL 按照日期格式查询带有时间戳数据

    按照日期格式查询带有时间戳数据一般在MSQL数据库中的时间都是以时间戳的格式来存储时间的,但是对于我们来说,时间戳格式具体表示的是什么时间,我们很难一眼看出来,所以当我们要具体查询某一个时间或时间段的 ...

  7. 怎样在Chrome浏览器上安装 Vue Devtools 扩展程序

    第一步: 前往 GitHub 下载 Vue Devtools 项目文件 https://github.com/vuejs/vue-devtools 注意: 1. 将分支切换为 master 2. 下载 ...

  8. Windows编程 鼠标

    客户区鼠标消息 由上一回我们得知Windows只把键盘消息发送给拥有输入焦点的窗口,而鼠标消息与此不同:只要鼠标跨越窗口或者在某窗口下按下鼠标键,那么窗口过程就会收到鼠标消息,不管该窗口是否活动或者是 ...

  9. 3、详解 ESLint 规则 转自https://blog.csdn.net/bbsyi/article/details/88816637

    什么是 ESLint ? ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 JSLint.JSHi ...

  10. git merge 命令的使用

    我们把dev分支的工作成果合并到master分支上: $ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 ...