当前位置:Gxlcms > PHP教程 > PHP、Python和Javascript的装饰器模式对比_PHP

PHP、Python和Javascript的装饰器模式对比_PHP

时间:2021-07-01 10:21:17 帮助过:8人阅读

修饰模式(Decorator Pattern),又叫装饰者模式,是面向对象编程领域中,一种动态地往一个类中添加新的行为的设计模式。就功能而言,修饰模式相比生成子类更为灵活,这样可以给某个对象而不是整个类添加一些功能。装饰模式非常适用于灵活扩展对象的功能,下面是装饰模式的UML图:

例如,有一个技术论坛,用户通过留言进行沟通,由于刚开始论坛里都是熟人,几乎都不需要对留言的内容作出审核,接收留言的页面可以是这样:

  1. class SaveMsg(){
  2. private $msg;
  3. public function __construct($msg){
  4. $this->msg=$msg;
  5. }
  6. public function __store(){
  7. //存入数据库
  8. }
  9. }

后来,随着论坛逐渐出名,就有一些人在上面发链接,就需要对含有链接的消息进行过滤,论坛进一步发展,发现除开发垃圾链接的外,还有很多无用的灌水,到后来可能还有攻击等等各种不正常的帖子,所以对论坛帖子的管理,可以单独抽象出一个类进行管理,当需要扩充过滤规则时,可以进行动态扩充。

  1. //基类
  2. abstract class Filter{
  3. abstract public function isForbid();
  4. }
  5. //基础过滤类
  6. class MsgFilter extends Filter{
  7. public $content;
  8. public function __construct($msg){
  9. $this->content=$msg;
  10. }
  11. public function isForbid(){
  12. if(preg_match("/https?/i",$this->content)){
  13. return [true,"Not Allowed Urls"];
  14. }else{
  15. return [false];
  16. }
  17. }
  18. }
  19. //装饰器,用来扩充功能
  20. abstract class FilterDecorator extends Filter{
  21. protected $obj;
  22. public function __construct(Filter $obj){
  23. $this->obj=$obj;
  24. }
  25. }
  26. //新过滤器,判断是否重复发帖
  27. class repeat extends FilterDecorator{
  28. public function isForbid(){
  29. if($this->obj->isForbid()[0] === true){
  30. //判定是否包含url
  31. return $this->obj->isForbid();
  32. }else if($this->obj->content == "this is a test"){
  33. //判定是否重复发帖
  34. return [true,"Repeat Posts"];
  35. }else{
  36. return [false];
  37. }
  38. }
  39. }
  40. $test = new MsgFilter("httpsfdjoafdsajof");
  41. print_r($test->isForbid());//被禁止
  42. $test2 = new repeat(new MsgFilter("this is a test"));
  43. print_r($test2->isForbid());//被禁止


在python中,不存在抽象类和方法,实现就更加简单:

  1. #!/usr/bin/env python
  2. class Filter():
  3. pass
  4. class MsgFilter(Filter):
  5. def __init__(self,msg):
  6. self.content=msg
  7. def isForbid(self):
  8. if('http' in self.content):
  9. return [True,"Not Allowed Urls"]
  10. else:
  11. return [False]
  12. class FilterDecorator(Filter):
  13. def __init__(self,obj):
  14. self._obj=obj
  15. class Repeat(FilterDecorator):
  16. def isForbid(self):
  17. if self._obj.isForbid()[0]:
  18. return self._obj.isForbid()
  19. elif self._obj.content == 'this is a test':
  20. return [True,"Repeat Posts"];
  21. else:
  22. return [False]
  23. test = MsgFilter("this is a content have http urls")
  24. print test.isForbid()
  25. test2 = Repeat(MsgFilter('this is a test'))
  26. print test2.isForbid()

Javascript中,没有严格的类,所有继承都基于原型,理解起来会稍费功夫:

  1. function MsgFilter(msg){
  2. this.content=msg;
  3. this.isForbid=function(){
  4. if(this.content.match(/http/g)){
  5. return [true,"Not Allowed Urls"];
  6. }else {
  7. return [false];
  8. }
  9. }
  10. }
  11. function Repeat(obj){
  12. var _obj=obj;
  13. this.isForbid=function(){
  14. if(_obj.isForbid[0] === true){
  15. return _obj.isForbid();
  16. }else if(_obj.content=='this is a test'){
  17. return [true,"Repeat Posts"];
  18. }else{
  19. return [false];
  20. }
  21. }
  22. }
  23. var test = new MsgFilter("his is a content have http urls");
  24. console.log(test.isForbid());
  25. var test2 = new Repeat(new MsgFilter("this is a test"));
  26. console.log(test2.isForbid());

由于Javascript缺少类的特性,继承对于它来说就显得有点鸡肋了,上面的代码看起来更像是对两个函数的处理, 在python中,有更加简单的添加装饰器的方法,直接通过”@”给函数自动添加装饰器,达到扩展功能的目的,如:

  1. def Decorator(F):
  2. def newF(age):
  3. print "You Are Calling",F.__name__
  4. F(age)
  5. return newF
  6. @Decorator
  7. #通过@给函数showAge添加装饰器Decorator
  8. def showAge(age):
  9. print "hello , i am %d years old"%age
  10. showAge(10)

装饰模式的目的是解决动态扩展功能的难题,装饰模式的本质是对对象的灵活处理,理解装饰模式,不仅能深入了解面向对象的程序设计,更能提高编程的思维能力。

人气教程排行