English 中文(简体)
C++:对访问模式的怀疑
原标题:C++: doubts about visitor pattern

我知道访问模式是什么,如何使用;这一问题不是


我有一个图书馆,我在那里把大多数可改用的代码I写成,我与我的大多数项目联系起来。

我往往需要增加某些课程的特征,但是没有在图书馆增加这些新特点。 让我举一个真实的例子:

在这一校正I ve got a category Shape, 继承者为CircleShape,PolygonShapeCompositeShape

我现在正在开发一种图形应用,如果我需要提供<条码>-Shape,但不想在核心<条码>中设置一个虚拟功能<条码>。 自我使用<代码>的一些项目以来,课程 Shape no t do any causesing,而其他图形项目则可使用不同的助推引擎(该项目使用Qt,但对于游戏一使用开放式GL,因此,render功能将需要不同的执行)。

当然,最著名的办法是利用访客模式,但这引起了我的一些疑虑:

任何类别的图书馆都需要作为我的<代码>Shape加以扩展。 大部分公共图书馆(约占全部)没有为访客模式提供任何支持;为什么? 为什么我?

访客模式是在C++模拟双重派任的方式。 这在C++中不是本土的,需要明确实施,使班级接口更加复杂: 我不认为<代码>applyVisitor功能应当与我的班级职能相同,因此,我看上去,这好像是破碎的。

Explicitly up-casting Shape with dynamic_cast is more expensive, but to me it looks like a cleaner solution.


因此,我应该做些什么? 在我所有的图书馆课堂上实行双重派任? 提供<代码>Shape的图书馆是哪里的,但有些GPL图书馆在互联网上发现?

最佳回答

首先:Visitor Format is a way tosimulate Double Dissigning in C++ er,这并非完全正确。 实际上,双重派遣是一种多种形式的派遣,这是在C++模拟(失踪人员)多方法的一种方式。


班级业务是否应当由<>>><>>+/strong>或实施<><>><><><><><>><>+> > > ?

  • If the number of classes keeps changing more rapidly than the number of operations, use virtual functions. That is because adding a class requires modifying all visitors.
  • If the number of classes is relatively stable compared to the number of operations, use visitors. That is because adding a virtual function requires changing all classes in the hierarchy.

Yes, many libraries do not come with a visitor interface.
When we are just looking at the reasoning above, this would be right if the number of classes changes often. That is, if a library is released often, with new classes being added constantly, then providing a visitor interface wouldn t make much sense, because each time a new release brings new classes, everybody using the library need to adapt all of their visitors. So if we only looked at the above reasoning, a visitor interface would seem to only be helpful if the number of classes in a lib s class hierarchy seldom or never changes.

However, with 3rd-party libraries there s another aspect to that: Usually, users cannot change the classes in the library. That is, if they need to add an operation, the only way they can do this is by adding a visitor - if the library provides the hooks for them to plug into it.
So if you are writing a library and feel like users should be able to add operations to it, then you need to provide a way for them to plug their visitors into your lib.

问题回答

This doesn t look like a case for the Visitor pattern to me.

我建议你有一个<代码>可复制的Shape的类别,即合计一个<代码>Shape的物体,然后为每一种形状设立子类。 <代码>可复制Shape将采用虚拟<代码>render方法。

如果你想要支持多个化引擎,你可以有一个<代码>RenderContext的基类,该基类为每一成型发动机提供分级的提款业务,每个子类在提款机上实施提款业务。 页: 1 采用<代码>RenderContext作为论据,并使用其抽象的APIC。

因此,有一个XxxShape类,它在某种程度上含有“沉淀物”的信息。 对于可能成为中心圆顶的圆顶圈,或某些圆顶。 也许在填料和颜色方面还有其他一些障碍。

You dont t want to/cannot update those classes to add the actual rendering logic, and I think your reasons for not doing so are valid/inevitable.

但是,大概而言之,你在课堂上拥有足够的公众准入方法,使你们能够掌握“衍生”的信息,否则你就注定了。

因此,在这种情况下,你不能仅仅总结这些项目:

 CircleRenderer hasA Cicle, knows how to render Circles

等等。 如今,在投标者课堂上使用访客模式。

有许多可能的解决办法,但你可以这样做,例如: 启动新的等级制度,使<代码>Shapes载于具体<代码>Context:

// contracts:

class RenderingContext {
public: virtual void DrawLine(const Point&, const Point&) = 0; 
    // and so on...
};

class ShapeRenderer {
public: virtual void Render(RenderingContext&) = 0;
};

// implementations:

class RectangleRenderer : public ShapeRenderer {
 Rectangle& mR;

public: 
 virtual void Render(RenderingContext& pContext) {
   pContext.DrawLine(mR.GetLeftLower(), mR.GetRightLower());
   // and so on...
 }

 RectangleRenderer(Rectangle& pR) : mR(pR) {}
};




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?