天道酬勤,学无止境

policy-based-design

What are the measures to call a Python code a policy-based design?

Description I wonder if the code I am showing can be considered as an example of policy-based design in Python. Also, I would like to know if have you seen python modules using something like this example so I can learn from them? I wrote more details and examples about this approach in a post. Recently I needed something like policy-based design for a python module I was working on. I found a similar question in this forum, but it was closed, and I was not able to add a comment. Let me give a summary of what I expect from this approach in Python. Module classes are divided between policy and

2022-01-16 18:18:55    分类:问答    python   mixins   policy-based-design

了解 Alexandrescu 关于多重继承弱点的阐述 [关闭](Understanding the exposition of Alexandrescu about the weaknesses of multiple inheritance [closed])

问题 关闭。 这个问题需要更加集中。 它目前不接受答案。 想改善这个问题吗? 更新问题,使其仅通过编辑这篇文章来关注一个问题。 6年前关闭。 改进这个问题 更新:我在这里问了一个更窄的问题。 在Modern C++ Design 的第 6-7 页上,Andrei Alexandrescu 就构建灵活设计的两个C++语言特性(多重继承和模板)的优缺点进行了非常基本的讨论。 他总结道: 现在比较多重继承的缺点列表和模板的缺点列表。 有趣的是,多重继承和模板促进了互补的权衡。 多重继承的机制稀缺; 模板具有丰富的机制。 多重继承会丢失模板中丰富的类型信息。 模板的专业化不能扩展,但多重继承可以很好地扩展。 您只能为模板成员函数提供一个默认值,但您可以编写无限数量的基类。 我能感觉到安德烈在这里说的很重要,但如果没有任何例子来说明要点,我无法真正理解所说的内容。 这个问题要求提供简单的例子来说明这些观点(请继续阅读)。 为了使问题更具体,我想问一下,请关注多重继承的弱点。 这就是安德烈对他们的看法(方括号中的文字是我对上下文的理解): 在这样的设置中[即多重继承],[构建灵活的SmartPtr ,] 用户将通过继承一些BaseSmartPtr类和两个类: MultiThreaded和RefCounted来构建多线程、引用计数的智能指针类。 任何有经验的班级设计师都知道

2021-12-12 08:56:06    分类:技术分享    c++   templates   multiple-inheritance   policy-based-design

Understanding the exposition of Alexandrescu about the weaknesses of multiple inheritance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago. Improve this question UPDATE: I have asked a narrower question here. On pages 6-7 of Modern C++ Design, Andrei Alexandrescu gives a very fundamental discussion of the strengths and weaknesses of two C++ language features -- multiple inheritance and templates -- with respect to building flexible designs. He concludes: Now compare the list of drawbacks of multiple inheritance with the

2021-11-28 19:52:08    分类:问答    c++   templates   multiple-inheritance   policy-based-design

Python中基于策略的设计[关闭](Policy based design in Python [closed])

问题 关闭。 这个问题需要细节或清晰。 它目前不接受答案。 想改善这个问题吗? 通过编辑此帖子添加详细信息并澄清问题。 7年前关闭。 改进这个问题 Andrei Alexandrescu 在Modern C++ Design描述的基于策略的设计给我留下了深刻的印象,并在一些轻量级程序中成功地尝试了它。 现在我必须用Python编写一个真实世界的系统,我认为这种方法在这里非常有用。 但是,我在Python找不到这种方法的单个示例。 在Python不推荐使用它还是有更好的选择? 有人可以指出我在Python基于策略的设计示例吗? 我的目标是开发一个拍卖系统,我希望能够在运行时选择拍卖策略 - English 、 Dutch 、 Silent等。 由于Python和Ruby非常相似,我想Ruby的示例也可以。 回答1 我的目标是开发一个拍卖系统,我希望能够在运行时选择拍卖策略 - 英语、荷兰语、静音等。 您可以使用策略模式(也称为策略模式)来实现这一点。 您还可以使用 mixins 来提供策略而不是组合。

2021-11-19 19:45:09    分类:技术分享    python   policy-based-design

Policy based design in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 7 years ago. Improve this question I was very impressed by the policy based design described in Modern C++ Design by Andrei Alexandrescu and tried it successfully in some light weight programs. Now I have to write a real world system in Python and I think that approach would be very useful here. However, I can't find a single example of this approach in Python. Is it not recommended in Python or are there better

2021-11-13 21:00:12    分类:问答    python   policy-based-design

具有不同接口的策略类(Policy classes with differing interfaces)

问题 假设一个具有策略FooPolicy的算法。 实现此策略的策略类具有静态成员函数foo ,但是,对于其中一些, foo接受一个int参数,而对于其他人则没有。 我试图通过constexpr静态数据成员启用这些具有不同接口的策略类: struct SimpleFoo { static constexpr bool paramFlag = false; static void foo() { std::cout << "In SimpleFoo" << std::endl; } }; struct ParamFoo { static constexpr bool paramFlag = true; static void foo(int param) { std::cout << "In ParamFoo " << param << std::endl; } }; template <typename FooPolicy> struct Alg { void foo() { if (FooPolicy::paramFlag) FooPolicy::foo(5); else FooPolicy::foo(); } }; int main() { Alg<ParamFoo> alg; alg.foo(); return 0; } 此代码无法编译。 gcc 4.8.2给出了错误:

2021-09-29 05:05:11    分类:技术分享    c++   c++11   policy-based-design

Mechanics of multiple inheritance compared to templates wrt building flexible designs

This is a narrower version of the question put on hold due to being too broad. On pages 6-7 of Modern C++ Design, Andrei Alexandrescu lists three ways in which the multiple inheritance is weaker than templates with respect to building flexible designs. In particular, he states that the mechanics provided by multiple inheritance is poor (the text in square brackets and formatting are mine as per my understanding of the context): In such a setting [i.e. multiple inheritance], [to build a flexible SmartPtr,] the user would build a multithreaded, reference-counted smart pointer class by inheriting

2021-07-31 18:45:46    分类:问答    c++   templates   multiple-inheritance   policy-based-design

Policy classes with differing interfaces

Suppose an algorithm that has a policy FooPolicy. Policy classes that implement this policy feature a static member function foo, but, for some of them, foo takes an int argument, while for others it does not. I am trying to enable the use of these policy classes with differing interfaces by means of constexpr static data members: struct SimpleFoo { static constexpr bool paramFlag = false; static void foo() { std::cout << "In SimpleFoo" << std::endl; } }; struct ParamFoo { static constexpr bool paramFlag = true; static void foo(int param) { std::cout << "In ParamFoo " << param << std::endl; }

2021-06-27 03:18:22    分类:问答    c++   c++11   policy-based-design

Partial specialization for a parent of multiple classes

I want to use partial specialization for a template class so that all children of that template class will use that specialization. Let me explain it with an example :) template < typename T, unsigned int rows, unsigned int cols> class BaseMatrix {...}; This class will have children that specify the structure of the matrix, like sparse,dense,diagonal,.. template < typename T, unsigned int rows, unsigned int cols> class DiagonalMatrix : public BaseMatrix<T,rows,cols>{..} Then those classes will have children again that specify the storage : stack arrays, vectors, list, queues,.. template <

2021-06-22 03:47:56    分类:问答    c++   templates   policy-based-design

特质和政策有什么区别?(What is the difference between a trait and a policy?)

问题 我有一个类,我正在尝试配置其行为。 template<int ModeT, bool IsAsync, bool IsReentrant> ServerTraits; 然后我有我的服务器对象本身: template<typename TraitsT> class Server {…}; 我的问题是我上面的用法是我的命名错误吗? 我的模板化参数实际上是策略而不是特征吗? 什么时候模板化参数是特征还是策略? 回答1 政策 策略是将行为注入父类的类(或类模板),通常是通过继承。 通过将父接口分解为正交(独立)维度,策略类构成了更复杂接口的构建块。 一种常见的模式是将策略作为用户可定义的模板(或模板模板)参数提供,并带有库提供的默认值。 标准库中的一个例子是分配器,它们是所有 STL 容器的策略模板参数 template<class T, class Allocator = std::allocator<T>> class vector; 在这里, Allocator模板参数(它本身也是一个类模板!)将内存分配和释放策略注入到父类std::vector 。 如果用户不提供分配器,则使用默认的std::allocator<T> 。 与基于模板的多态性一样,策略类的接口要求是隐式和语义(基于有效表达式)而不是显式和句法(基于虚拟成员函数的定义)。 请注意,最近的无序关联容器具有多个策略。

2021-06-09 16:57:55    分类:技术分享    c++   template-meta-programming   typetraits   policy-based-design