基于策略的设计和最佳实践 - C ++ [英] Policy based design and best practices - C++

查看:140
本文介绍了基于策略的设计和最佳实践 - 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;
}



我写了上述代码作为学习基于策略的设计的一部分。我对上述代码有几个问题

I wrote the above code as part of learning policy based designs. I have few questions on the above code

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

1 - Does this implementation look correct? I mean: does it really look like a policy based design?

2 - 我现在可以钩任何种类的笔给作家。但是,当我得到一个没有默认构造函数(只有参数化构造函数)的笔,我该怎么办?我将如何处理这种情况?

2 - I can now hook any kind of pens to writer. But what will I do when I got a pen with no default constructor (only parameterized constructors)? How will I handle this situation?

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

3 - 当上述代码使用像

3 - When the above code is used like

Writer<InkPen> writer;

我猜编译器会将 PenType 替换为 InkPen >。如果是,为什么我不能调用 Write() StartWriting()而不是前缀基类名称( PenType :: Write em>)?

I guess compiler will replace PenType with InkPen. If yes, why I am not able to call just Write() from StartWriting() instead of prefixing base class name (PenType::Write())?

4 - 我认为基于策略的设计迫使你从语义无效的类派生。在上面的代码中,作者是从笔中派生的,因为作者使用笔。但说作家是一支笔在语义上无效。

4 - I think policy based design forces you to derive from classes which is semantically invalid. In the above code, a writer is derived from a pen only because writer uses a pen. But saying writer is a pen is semantically invalid. Is there any other better way to address this or I am missing something here?

有任何想法吗?

推荐答案

下面是我如何实现类:

template<class PenType>
class Writer
{
public:
  Writer(const PenType& pen = PenType()) : pen(pen) {}

  void StartWriting()
  {
    pen.Write();
  }

private:
  PenType pen;
};

这允许用户将特定的Pen对象传递给构造函数,如果它没有一个默认的构造函数,或者你不想使用它,第二,它仍然允许你省略PenType对象,如果你很高兴让它创建一个与默认构造函数。 C ++标准库在许多类中也是这样(例如,考虑容器类的分配器)。

This allows the user to pass a specific Pen object to the constructor, if it either doesn't have a default constructor, or you don't want it to be used, and second, it still allows you to omit the PenType object if you're happy to let it create one with the default constructor. The C++ standard library does the same in many classes (think of the allocators for container classes for example).

我删除了继承。你可能不希望Writer类的用户直接调用PenType :: Write函数,你可以使用私有继承,但是通常情况下,组合是一个更简单和更传统的设计。

I removed the inheritance. It didn't really seem to add anything (and might cause problems. You probably don't want the user of the Writer class to call the PenType::Write function directly. You could use private inheritance instead, but often, composition is a simpler and more conventional design.

一般来说,基于策略的设计不需要继承,添加它作为一个成员也是如此,如果你去继承,使其私有,所以你不会遇到你提到的问题#4。

In general, policy-based design does not require inheritance. Adding it as a member works just as well. If you do go for inheritance, make it private so you don't get the problem you mentioned as #4.

这篇关于基于策略的设计和最佳实践 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆