使用不是 GObject 属性的参数初始化 GObject?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
A
继承自B
。那么A
会自动拥有B
的所有属性。A
中使用属性,而是将B
的属性(或者更好的是,已经构造的B
对象)作为参数传递到A
的构造函数。B
的构建,直到A
能够弄清楚需要如何配置B
。向A
、b_initialized
等添加私有标志,告诉您A
的内部指针是否指向B
是有效的。对第二个建议的更多说明:
A
的内容是在G_DEFINE_TYPE()
提供的a_init()
函数中构造的宏。但这不是获取A
实例的方式。通常编写一个函数,它是A
公共接口的一部分,如下所示:您可以轻松地扩展它以包含其他参数:
这有一个缺点,即保留
A
如果您使用g_object_new
构建它,例如如果您尝试从 GtkBuilder 文件构建它,则 code> 对象处于无效状态(即没有B
)。如果这是一个问题,我仍然强烈建议重构。A
inherit fromB
. ThenA
has all ofB
's properties automatically.A
, but instead passB
's properties (or even better, an already-constructedB
object) as parameters toA
's constructor.B
untilA
can figure out how it nees to configureB
. Add a private flag toA
,b_initialized
or something, that tells you whetherA
's internal pointer toB
is valid.Some more clarification on the second suggestion:
A
's stuff is constructed in thea_init()
function that is provided for by theG_DEFINE_TYPE()
macro. But that's not how you get an instance ofA
. It's usual to write a function, which is part of the public interface ofA
, like this:You can easily extend this to include other parameters:
This has the disadvantage of leaving your
A
object in an invalid state (i.e., without aB
) if you construct it usingg_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.使用依赖注入,将一个已经初始化的
B
类型的对象传递给A
的构造函数。这样,使用您的类的客户端就可以决定是否传入不同类型的
B
(如果有意义,您甚至可以使用接口而不是类作为B
) code> 类型,针对接口编写代码通常比针对实现编写代码更好)。仅当它确实是其父类的特化时,从
B
派生A
才有意义。从问题来看,尚不清楚推导是否有意义,但它是一种经常过度使用的组合方法 。
Use dependency injection, pass an already initialized object of type
B
to the constructor ofA
.That way the client that is using your class can decide whether to pass in different kinds of
B
s (if it makes sense you can even use an interface instead of a class as theB
type, writing code against interfaces is generally better than writing code against implementations).Deriving
A
fromB
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.