[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’ ...
随机推荐
- ESXi 虚拟机 提示 无法打开本地虚拟机的 xxx.vmx 的本地管道的 问题解决.
1. 今天同事与我联系, 说一个虚拟机出现连不上, vcenter控制台关闭虚拟机之后 再次打开报错: 2. 自己最开始的解决方法 移除虚拟机, 进入服务器的datastore 重新注册, 结果发现问 ...
- brew update慢,brew install慢如何解决?
主要是资源访问太慢造成的,替换默认源镜像就行. brew使用国内镜像源 这里用中科大的,另外还有清华的可用 1 2 3 4 5 6 7 8 9 10 # 步骤一 cd "$(brew ...
- Windows下mysql导出和导入数据库表(命令行)
导出: 1.打开ctrl+R输入cmd 打开命令行 2.cd D:\mysql-8.0.15-winx64\bin 到MySQL的bin目录 3.输入命令 mysqldump -u root -p ...
- python 修改文件的创建时间、修改时间、访问时间
目录 python 修改文件创建.修改.访问时间 方案一 方案二(无法修改文件创建时间) python 修改文件创建.修改.访问时间 突如其来想知道一下 python 如何修改文件的属性(创建.修改. ...
- Git安装使用秘籍
首先Git的功能,是用于帮助用户实现版本控制的软件,GIT一般和GitHub配套使用.Git是个软件,GitHub是个网站,它们的关系就像雷锋与雷峰塔一样,没什么关系.本文只提供Git安装方法,其它请 ...
- 通过python的selenium实现网站自动登陆留言
from selenium import webdriver import time driver = webdriver.Chrome() driver.get('https://wordpress ...
- docker 入门2 - 容器 【翻译】
入门,第 2 部分:容器 先决条件 安装的 Docker 版本是 1.13 及以上. 读完 第一部分 用下面的命令快速测试你的环境是否完备: docker run hello-world 概述 现在开 ...
- 怎样快捷获取元素节点body
1. 使用: document.body document.body.nodeName; // "BODY" 2. 使用: document.getElementsByTagNam ...
- react用高阶组件实现路由守卫
react-router不像vue-router一样有很多钩子函数,可以做路由守卫.想实现路由守卫,可以用高阶组件来实现. @connect(state => ({ isLogin: state ...
- PHP转码函数mb_convert_encoding() 和iconv()
注意:函数mb_convert_encoding的执行效率比iconv差,且需开启php的mbstring扩展. 一般情况下使用iconv,但此函数在碰到无法转码字符会丢弃,此种情况下可以用mb_co ...