标签: policy-injection

基于策略的设计和最佳实践 - C++

struct InkPen
{
  void Write()
  {
    this->WriteImplementation();
  }

  void WriteImplementation()
  {
    std::cout << "Writing using a inkpen" << std::endl;
  }

};

struct BoldPen
{
  void Write()
  {
    std::cout << "Writing using a boldpen" << std::endl;
  }
};

template<class PenType>
class Writer : public PenType
{
public:
  void StartWriting()
  {
    PenType::Write();
  }
};

int main()
{
  Writer<InkPen> writer;
  writer.StartWriting();
  Writer<BoldPen> writer1;
  writer1.StartWriting();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我将上述代码编写为学习基于策略的设计的一部分.我对上面的代码几乎没有疑问

1 - 此实现看起来是否正确?我的意思是:它真的看起来像一个基于政策的设计吗?

2 - 我现在可以将任何类型的笔钩到作家身上.但是当我拿到没有默认构造函数的笔(仅参数化构造函数)时,我会怎么做?我该如何处理这种情况?

template<class PenType>
class Writer : public PenType …
Run Code Online (Sandbox Code Playgroud)

c++ design-patterns policy-injection policy-based-design

26
推荐指数
4
解决办法
3万
查看次数

判断一个方法是否是策略注入中的属性的最佳方法是什么?

我有一个应用于类的自定义处理程序(使用 entlib 4 中的策略注入应用程序块),我想知道调用 Invoke 时输入方法是否是一个属性。以下是我的处理程序的样子。

[ConfigurationElementType(typeof(MyCustomHandlerData))]
public class MyCustomHandler : ICallHandler
{
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        if (input.MethodBase.IsPublic && (input.MethodBase.Name.Contains("get_") || input.MethodBase.Name.Contains("set_")))
        {
            Console.WriteLine("MyCustomHandler Invoke called with input of {0}", input.MethodBase.Name);
        }
        return getNext().Invoke(input, getNext);
    }

    public int Order { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

正如您从我的代码示例中看到的,到目前为止我想到的最好方法是解析方法名称。难道没有更好的方法吗?

c# enterprise-library policy-injection

3
推荐指数
1
解决办法
2798
查看次数