decorator

Simplified decorator pattern. Is that correct?

走远了吗. 提交于 2019-12-11 07:07:34
问题 So I've created something like this: interface IStudent { string DisplayInformation(); } public class Student : IStudent { public string Name { get; set; } public string Grade { get; set; } public int Age { get; set; } public virtual string DisplayInformation() { return $"{Name} - {Age} years old is in {Grade} grade"; } } public class StudentDecorator : Student { private Student _student; public StudentDecorator(Student student) { _student = student; } public override string

How to use Autofac to inject decorator of a class in one constructor and the class itself in another?

岁酱吖の 提交于 2019-12-11 06:58:18
问题 I have an IoCConfig where in the RegisterDependencies method first all Services (same assembly as ServiceBase ) are registered, except for one service and one class called LastActivityUpdator and a decorator of this class called AnonymousUserLastActivityUpdator which both implement ILastActivityUpdator . The goals I want to accomplish is to have the decorator registered properly, which already works following this answer. And I also asked for a way to for one class ( UserService ) use the

Python decorator that returns a function with one or more arguments replaced

元气小坏坏 提交于 2019-12-11 06:51:51
问题 I would like to create a decorator to a set of functions that replaces one or more of the arguments of the functions. The first thought that came to my mind was to create a decorator that returns a partial of the function with the replaced arguments. I'm unhappy with the way the decorated function is called, but even when it's being called "properly", i get a TypeError. Here is some example code: def decor(func, *args, **kwargs): def _new_func(*args, **kwargs): return partial(func, *args, *

OO design in C++ - Decorating a parent object with unknown types of child

雨燕双飞 提交于 2019-12-11 06:43:36
问题 I'm currently working on a system within which objects animate along a bezier curve. At various, user defined, points on the curve, there can be an event which the animating object has to react to. This could be a change of speed, change on animation routine, change of facing direction etc... So far I have the following AnimationEvent class. class AnimationEvent { private: unsigned int m_eventID; float m_InteroplationPoint; float m_SpeedInMPS; public: AnimationEvent(unsigned int id, float

C# Decorator Class using Default Class Version of Method

孤街浪徒 提交于 2019-12-11 04:52:19
问题 How do you leave a method the same as the default implementation if trying to decorate a class? For example I am trying to add a property to a IWebDriver interface of the WebDriver class. i.e. public class MyWebDriver : IWebDriver { private IWebDriver _driver; internal string _currentTest { get; set; } internal MyWebDriver(IWebDriver driver) { _driver = driver; } } Visual Studio IntelliSense had me implement the class (or interface) with a bunch of ' throw new NotImplementedException(); as

Identifying equivalent varargs function calls for memoization in Python

会有一股神秘感。 提交于 2019-12-11 03:01:34
问题 I'm using a variant of the following decorator for memoization (found here): # note that this decorator ignores **kwargs def memoize(obj): cache = obj.cache = {} @functools.wraps(obj) def memoizer(*args, **kwargs): if args not in cache: cache[args] = obj(*args, **kwargs) return cache[args] return memoizer I'm wondering, is there a reasonable way to memoize based on both args and kwargs, particularly in cases where two function calls specified with arguments assigned differently positionally

Head First Design Patterns - Decorator Pattern using Starbuzz

半世苍凉 提交于 2019-12-11 02:58:34
问题 I am going through the book Head First Design Patterns and am specifically looking at the Starbuzz example for the Decorator pattern. I am having trouble understanding that what exactly is the need for CondimentDecorator in the example provided. Why can't Mocha simply extend Beverage ? What's the need for another layer of abstraction?` public abstract class Beverage { String description = "Unknown beverage"; public String getDescription() { return description; } public abstract double cost();

Pickle and decorated classes (PicklingError: not the same object)

橙三吉。 提交于 2019-12-11 02:33:42
问题 The following minimal example uses a dummy decorator, that justs prints some message when an object of the decorated class is constructed. import pickle def decorate(message): def call_decorator(func): def wrapper(*args, **kwargs): print(message) return func(*args, **kwargs) return wrapper return call_decorator @decorate('hi') class Foo: pass foo = Foo() dump = pickle.dumps(foo) # Fails already here. foo = pickle.loads(dump) Using it however makes pickle raise the following exception: _pickle

Strict type checking for property type with property decorator

假如想象 提交于 2019-12-11 02:14:00
问题 Is there a way to validate the type of the property that is decorated in Typescript? I would like a property decorator that only works on boolean class properties, but not on e.g. string (example below). Is this possible? (Note: I don't want runtime validation via reflect-metadata, just a compile type warning with Typescript.) function booleanProperty<T extends HTMLElement>(target: T, prop: string) { // `target` will be the class' prototype Reflect.defineProperty(target, prop, { get(this: T):

Is it possible to provide type safety to method decorator options

青春壹個敷衍的年華 提交于 2019-12-11 02:09:14
问题 I decided to write some utility decorators such as memoize , rateLimiter . I want to achieve as much type safety as possible without unnecessary boilerplate code. Is it possible to ensure full type safety in decorators like that without manually specified generics? type GET_FUNCTION_SIGNATURE< T extends TypedPropertyDescriptor<any> > = T extends TypedPropertyDescriptor<infer U> ? U : never; interface ITestDecoratorOptions<DECORATED_FUNCTION_ARGUMENTS_TYPE, DECORATED_FUNCTION_RETURN_TYPE> {