使用不是 GObject 属性的参数初始化 GObject?

发布于 2024-12-10 05:19:42 字数 243 浏览 0 评论 0原文

我有一个 GObject“A”,它在其构造函数中创建另一个 GObject“B”的实例。

“B”对象需要传递几个仅用于构造的属性。现在,当创建对象“A”的实例时,我希望允许通过对象“A”的构造函数将这些属性的值传递到对象“B”的构造函数。

我发现做到这一点的唯一方法是为对象“A”创建相同的属性并将它们的值传递给“B”的构造函数。这些属性对“A”没有进一步的意义,所以这看起来像是一个拼凑。

有更好的方法来做我想做的事吗?

I have a GObject "A" which creates an instance of another GObject "B" in its constructor.

The "B" object needs to be passed several construction-only properties. Now when creating an instance of object "A" I want to allow passing values for these properties through the constructor of object "A" on to the constructor of object "B".

The only way I have found to do that was to create identical properties for object "A" and pass their values on to the constructor of "B". These properties would have no further meaning to "A" so this seems like a kludge.

Is there a better way to do what I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我喜欢麦丽素 2024-12-17 05:19:42
  • A 继承自 B。那么A会自动拥有B的所有属性。
  • 不要在 A 中使用属性,而是将 B 的属性(或者更好的是,已经构造的 B 对象)作为参数传递到 A 的构造函数。
  • 延迟 B 的构建,直到 A 能够弄清楚需要如何配置 B。向 Ab_initialized 等添加私有标志,告诉您 A 的内部指针是否指向 B是有效的。

对第二个建议的更多说明:

A 的内容是在 G_DEFINE_TYPE() 提供的 a_init() 函数中构造的宏。但这不是获取 A 实例的方式。通常编写一个函数,它是 A 公共接口的一部分,如下所示:

A *a_new() 
{
    return (A *)g_object_new(TYPE_A, NULL);
}

您可以轻松地扩展它以包含其他参数:

A *a_new(int b_param_1, int b_param_2)
{
    A *a = (A *)g_object_new(TYPE_A, NULL);
    a->priv->b = b_new(b_param_1, b_param_2);
    return a;
}

这有一个缺点,即保留 A如果您使用 g_object_new 构建它,例如如果您尝试从 GtkBuilder 文件构建它,则 code> 对象处于无效状态(即没有 B)。如果这是一个问题,我仍然强烈建议重构。

  • Have A inherit from B. Then A has all of B's properties automatically.
  • Don't use properties in A, but instead pass B's properties (or even better, an already-constructed B object) as parameters to A's constructor.
  • Delay construction of B until A can figure out how it nees to configure B. Add a private flag to A, b_initialized or something, that tells you whether A's internal pointer to B is valid.

Some more clarification on the second suggestion:

A's stuff is constructed in the a_init() function that is provided for by the G_DEFINE_TYPE() macro. But that's not how you get an instance of A. It's usual to write a function, which is part of the public interface of A, like this:

A *a_new() 
{
    return (A *)g_object_new(TYPE_A, NULL);
}

You can easily extend this to include other parameters:

A *a_new(int b_param_1, int b_param_2)
{
    A *a = (A *)g_object_new(TYPE_A, NULL);
    a->priv->b = b_new(b_param_1, b_param_2);
    return a;
}

This has the disadvantage of leaving your A object in an invalid state (i.e., without a B) if you construct it using g_object_new, for example if you're trying to build it from a GtkBuilder file. If that's a problem, I still strongly suggest refactoring.

海夕 2024-12-17 05:19:42

使用依赖注入,将一个已经初始化的B类型的对象传递给A 的构造函数。

这样,使用您的类的客户端就可以决定是否传入不同类型的 B(如果有意义,您甚至可以使用接口而不是类作为 B) code> 类型,针对接口编写代码通常比针对实现编写代码更好)。

仅当它确实是其父类的特化时,从 B 派生 A 才有意义。

从问题来看,尚不清楚推导是否有意义,但它是一种经常过度使用的组合方法

Use dependency injection, pass an already initialized object of type B to the constructor of A.

That way the client that is using your class can decide whether to pass in different kinds of Bs (if it makes sense you can even use an interface instead of a class as the B type, writing code against interfaces is generally better than writing code against implementations).

Deriving A from B only makes sense if it really is a specialization of it's parent class.

From the question it isn't clear if derivation makes sense, but it's an often overused method for composition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文