草庐IT

Decorator

全部标签

php - 尝试在 php 中实现(理解)Decorator 模式

我正在尝试理解Decorator模式,并且我已经阅读了关于SO的其他相关问题,然后我决定用一个简单的例子来尝试一下(我是PHP新手):interfaceiTitle{publicfunctiongetTitle();}classTitleimplementsiTitle{protected$_text;publicfunction__construct(){$this->_text='Ourpage';}publicfunctiongetTitle(){return$this->_text;}}abstractclassTitleDecoratorimplementsiTitle{pr

用于打印函数执行的每一行的 Python Decorator

出于调试目的,我想打印出与在python方法中执行的每一行有关的内容。例如,如果行中有一些赋值,我想打印为该变量赋值的值,如果有函数调用,我想打印出函数返回的值,等等。因此,例如,如果我要使用一个装饰器,应用于函数/方法,例如:@some_decoratordeftesting():a=10b=20c=a+be=test_function()调用函数测试时,应打印以下内容:a=10b=20c=30e=some_value有什么办法可以实现吗?更根本的是,我想知道我是否可以编写一个代码来逐行检查其他代码,检查它是什么类型的指令,等等。或者我们可以得到一个字典来找出所有变量一个类,我可以得

python - PySide Slot Decorator 是必需的吗?

我已经看到了一些使用@QtCore.Slot装饰器的PySide插槽示例代码,而有些则没有。自己测试了一下,好像没什么区别。我有理由应该或不应该使用它吗?例如,在以下代码中:importsysfromPySideimportQtCore#thenextlineseemstomakenodifference@QtCore.Slot()defa_slot(s):printsclassSomeClass(QtCore.QObject):happened=QtCore.Signal(str)def__init__(self):QtCore.QObject.__init__(self)defdo

python - 为什么@decorator 不能装饰静态方法或类方法?

为什么decorator不装饰静态方法或类方法?fromdecoratorimportdecorator@decoratordefprint_function_name(function,*args):print'%swascalled.'%function.func_namereturnfunction(*args)classMy_class(object):@print_function_name@classmethoddefget_dir(cls):returndir(cls)@print_function_name@staticmethoddefget_a():return'a

python - 在 Python : property decorator or lambda? 中定义属性的首选方式

在Python中定义类属性的首选方式是什么?为什么?可以在一个类中同时使用两者吗?@propertydeftotal(self):returnself.field_1+self.field_2或total=property(lambdaself:self.field_1+self.field_2) 最佳答案 对于只读属性我使用装饰器,否则我通常会这样做:classBla(object):defsneaky():deffget(self):returnself._sneakydeffset(self,value):self._snea

java - 装饰器模式 : Why do we need an abstract decorator?

这个问题已经被问过了here,但不是回答具体问题,而是给出了装饰器模式如何工作的描述。我想再问一次,因为仅仅通过阅读装饰器模式的工作原理对我来说答案并没有立即显现出来(我已经阅读了维基百科的文章和《HeadFirstDesignPatterns》一书中的部分)。基本上,我想知道为什么必须创建一个抽象装饰器类来实现(或扩展)某些接口(interface)(或抽象类)。为什么所有新的“装饰类”都不能简单地实现(或扩展)基本抽象对象本身(而不是扩展抽象装饰器类)?为了更具体,我将使用设计模式书中处理咖啡饮料的示例:有一个抽象的组件类叫BeverageHouseBlend等简单饮料类型只需扩展

c++ - 类 Python C++ 装饰器

有没有办法像python风格一样在C++中装饰函数或方法?@decoratordefdecorated(self,*args,**kwargs):pass以宏为例:DECORATE(decorator_method)intdecorated(inta,floatb=0){return0;}或DECORATOR_MACROvoiddecorated(mytype&a,mytype2*b){}有可能吗? 最佳答案 std::function为我提出的解决方案提供了大部分构建block。这是我提出的解决方案。#include#includ

c++ - 类 Python C++ 装饰器

有没有办法像python风格一样在C++中装饰函数或方法?@decoratordefdecorated(self,*args,**kwargs):pass以宏为例:DECORATE(decorator_method)intdecorated(inta,floatb=0){return0;}或DECORATOR_MACROvoiddecorated(mytype&a,mytype2*b){}有可能吗? 最佳答案 std::function为我提出的解决方案提供了大部分构建block。这是我提出的解决方案。#include#includ

javascript - 什么是 "decorators"以及它们是如何使用的?

我很好奇AngularJS中的装饰器到底是什么。除了AngularJSdocumentation中的简介外,没有太多关于装饰者的在线信息。并在youtubevideo中简短(尽管很有趣)提及.正如Angular人所说,装饰器是:Decorationofservice,allowsthedecoratortointercepttheserviceinstancecreation.Thereturnedinstancemaybetheoriginalinstance,oranewinstancewhichdelegatestotheoriginalinstance.我真的不知道是什么意思,

javascript - 什么是 "decorators"以及它们是如何使用的?

我很好奇AngularJS中的装饰器到底是什么。除了AngularJSdocumentation中的简介外,没有太多关于装饰者的在线信息。并在youtubevideo中简短(尽管很有趣)提及.正如Angular人所说,装饰器是:Decorationofservice,allowsthedecoratortointercepttheserviceinstancecreation.Thereturnedinstancemaybetheoriginalinstance,oranewinstancewhichdelegatestotheoriginalinstance.我真的不知道是什么意思,