[AngularJS] Decorator pattern for code reuse
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的更多相关文章
- 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- 装饰模式(Decorator pattern)
装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...
- angularjs decorator
angularjs decorator https://docs.angularjs.org/guide/decorators decorator() $provide服务提供了在服务实例创建时对其进 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 深入浅出设计模式——装饰模式(Decorator Pattern)
模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- 第 13 章 装饰模式【Decorator Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...
随机推荐
- 更新neo4j节点信息
将多个属性的内容更新到节点上 def update_by_id(id,graph,**kwargs): """ 更新节点的属性 根据节点的ID来更新节点的属性,如果存在该 ...
- ubuntu下安装navicat
1.去官网下载 https://www.navicat.com/en/download/navicat-premium 2.命令行输入(解压命令) tar -zxvf xxxxx.tar.gz 3.移 ...
- 剑指offer46:圆圈中最后剩下的数字(链表,递归)
1 题目描述 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随 ...
- NOIP(CSP)答题技巧&小细节
1.主函数类型 通常使用int main(),然而可以使用完全等价的signed main() 解锁 #define int long long 的操作 2.long long 的使用 数列长度/边 ...
- Selenium工具爬取商品
selenium是一个优秀的自动化测试工具,支持多种语言,具体介绍参考官方文档:https://www.seleniumhq.org/docs/. 下面我们使用selenium工具模拟用户点击商品详情 ...
- Python文件操作——读写图片,音频,视频
注意:其实就是将mode="rb"或者mode="wb",因为图片,视频,音频就是二进制进行读取,b 代表binary ,其他的和一般文件操作步骤一样,另外, ...
- git bash配置SSH远程连接阿里云ECS
1.连接配置 1-1.添加安全组规则 1-2.使用GitHub的话本地都会有id_rsa.pub(公钥),id_rsa(私钥),一般保存在C盘用户目录下.ssh文件夹. 把公钥内容复制下来(ssh-r ...
- 微信公众号支付备忘及填坑之路-java
一.背景 最近公司给第三方开发了一个公众号,其中最重要的功能是支付,由于是第一次开发,遇到的坑特别的多,截止我写博客时,支付已经完成,在这里我把遇到的坑记录一下(不涉及退款).不得不吐槽一下,腾讯这么 ...
- uni-app使用Canvas绘图
最近公司项目在用uni-app做小程序商城,其中商品和个人需要生成图片海报,经过摸索记录后将一些重点记录下来.这里有两种方式来生成 1.后台控制生成 2.前端用canvas合成图片 这里我们只讲使用c ...
- 【php设计模式】组合模式
定义: 是用于把一组相似的对象当作一个单一的对象.组合模式依据树形结构来组合对象,用来表示部分以及整体层次.这种类型的设计模式属于结构型模式,它创建了对象组的树形结构. 应用场景: 部分.整体场景,如 ...