处理无法实例化的对象的最佳方式? [英] Best way to deal with object that can't instantiate itself?

查看:264
本文介绍了处理无法实例化的对象的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我以前问了几个类似的问题,但是我正在围着丛林殴打。我认为这是真正的问题,我不能放弃休息。

I guess I've asked a few similar questions before, but I was beating around the bush. I think this is the real problem that I can't quite lay to rest.

我正在处理一个第三方库,并且有一个对象不能自己创建, b2Body b2World 必须实例化。我个人不喜欢这种设计模式;我认为, b2Body 应该能够独立于世界存在,然后在需要时被添加到世界。无论如何,我已经用我自己的类类 Body 包裹了 b2Body ,因为我需要添加一些额外的东西无论如何同样,我有一个世界包装器。现在我想我有3个选项:

I'm dealing with a third party library, and there's an object that can't create itself, b2Body. The b2World has to instantiate it. I personally don't like this design pattern very much; I think the b2Body should be able to exist independently of the world, and then be added to the world when needed. Anyway, I've wrapped b2Body with my own class class, Body, because I need to add some extra stuff to it anyway. Similarly, I have a World wrapper. Now I figure I have 3 options:


  1. 正文的构造函数需要一个指向 World ,以便它可以完全实例化(在某个地方调用 b2World :: CreateBody ) - 即有一个构造函数像 Body * b = new Body(world_ptr)

  2. 通过正文到一些 World :: CreateBody 方法,如图书馆已经这样做 - 即 Body * b = world.CreateBody(params);

  3. 复制 b2Body 中的所有数据,以便您可以使用它,然后添加它到世界,它将切换使用 b2Body 数据 - 即正文b 及以后 world.addBody(b)

  1. Have Body's constructor takes a pointer to World so that it can be fully instantiated (calls b2World::CreateBody somewhere inside) -- i.e. have a constructor like Body *b = new Body(world_ptr)
  2. Pass Body to some World::CreateBody method like how the library already does it -- i.e. Body *b = world.CreateBody(params);
  3. Duplicate all the data in b2Body so that you can use it however you want, and then after you add it to the world it will 'switch over' to use the b2Body data -- i.e. Body b and later world.addBody(b).

(1)和(2)不能有一个正文没有世界,我可能不需要,但它可能是很好有这个选择[所以我可以使用它作为其他对象的模板等等]。不知道有什么其他的利弊。 (3)似乎更好,但实现起来还有更多的工作,这意味着我必须复制已经包含在 b2Body 中的大部分数据。

(1) and (2) mean that you can't have a Body without a World, which I probably won't need, but it might be nice to have that option [so that I can use it as a template for other objects and such]. Not sure what other pros and cons there are. (3) seems nicer, but it's a lot more work to implement, and it means I have to duplicate most of the data that's already contained in b2Body.

你有什么想法?我会 CW 这样就没有人会发财。

What are your thoughts? I'll CW this just so no one frets.

我还是不能躺下休息。这是每个选项的样子:

I still can't lay this to rest. This is what each of the options would look like:

选项1:(我喜欢)

World w;
Body b;
Fixture f;
b.addFixture(f);
w.addBody(b);

选项2:(中间位置)

World w;
Body b(w);
Fixture f(b);

选项3:(Box2D如何)

World *w = new World;
Body *b = w->CreateBody(args);
Fixture *f = b->CreateFixture(args);

选项2和3没有那么不同,但它改变了拥有控制权的人是创建对象。

Options 2 and 3 aren't so different, but it changes who has control over is creating the objects.

如何实际实现选项3? World :: CreateBody()必须调用 b2World :: CreateBody(args)调用 b2Body :: b2Body()并返回 b2Body ,但不要调用 Body :: Body(args)这是一个问题。 b2Body 将完全初始化,但是我的包装器没有地方可以做到这一点...更具体地说,我如何写 World :: CreateBody (const BodyDef& bd)?假设BodyDef继承自b2BodyDef,来自b2Body的Body,来自b2World的世界等。

How would I actually implement option 3 though? World::CreateBody() has to call b2World::CreateBody(args) which calls b2Body::b2Body() and returns b2Body but never calls Body::Body(args) which is a problem. The b2Body would get fully initialized, but my wrapper has no place to do it's thing... More specifically, how would I write World::CreateBody(const BodyDef &bd)? Assuming BodyDef inherited from b2BodyDef, Body from b2Body, World from b2World, etc.

推荐答案

我想,如果你打算使用第三方库,如果你有比OMA更好的理由哦,我不喜欢这样的设计模式,那么你应该只会打败它的设计。你的图书馆有一种办法,显然是通过使用一个工厂的对象,而且这样做会使你的代码复杂性大大增加。

I think, if you're going to use a third-party library, you should only fight its design if you have a much better reason than oh, I don't like that design pattern much. Your library has a way of doing things — apparently, by using a factory object — and fighting that will increase your code complexity, possibly substantially.

这篇关于处理无法实例化的对象的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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