gpt4 book ai didi

java - 如何确定 ColdFusion 对象所在的上下文?

转载 作者:搜寻专家 更新时间:2023-10-30 21:33:34 25 4
gpt4 key购买 nike

所以,假设我有这个组件的一个实例:

foo.cfc

<cfcomponent>
<cffunction name="locateMe">
<cfreturn "I don't know where I live!">
</cffunction>
</cfcomponent>

还有这个其他组件,fooParent.cfc:

<cfcomponent>
<cfset this.foo = createObject("component", "foo")>
</cfcomponent>

假设我用几种不同的方式创建了“foo”的实例:

<cfset myStruct = {}>
<cfset myStruct.foo = createObject("component", "foo")>

<cfset myFoo = createObject("component", "foo")>

<cfset myFooParent = createObject("component", "fooParent")>

<cfoutput>
#myStruct.foo.locateMe()#<br>
#myFoo.locateMe()#<br>
#myFooParent.foo.locateMe()#<br>
</cfoutput>

如预期的那样,输出:

I don't know where I live!
I don't know where I live!
I don't know where I live!

我想知道的是,我可以在 foo.cfc 中做什么来告诉我一些(任何东西!)关于它被调用的上下文?由于一切最终都(至少)存在于某种范围内,并且所有范围都是一种对象,所以我想说的是,我真的很想要某种方法来确定包含对象实例化对象。最终,构建 foo.cfc 的某种方式可以使类似这样的内容成为我的输出,来 self 上面的示例片段:

I live within a "class coldfusion.runtime.Struct" instance!
I live within a "class coldfusion.runtime.VariableScope" instance!
I live within a "component cfjunk.fooParent" instance!

其中每个值都可以通过检查传递 getMetaData 实际包含对象引用的结果来确定。

更新 正如 Micah 在评论中所建议的那样,我为此添加了“Java”标签,因为我怀疑他可能是正确的,因为解决方案可能在于使用 Java 进行内省(introspection)。

更新

与其让这看起来像是一场纯粹的学术讨论,不如让我解释一下我为什么需要这个。

我正在使用带有包含的 CFWheels ORM 来取回对我的数据的引用,如下所示:

var user = model("User").findOne(where="id=123", include="AuthSource", returnAs="object");

这将返回一个对象,我可以像这样引用它:

user.id // property of the "User" model
user.reset() // method on the "User" model
user.AuthSource.id // property of the "AuthSource" model
user.AuthSource.authenticate(password) // method on the "AuthSource" model

现在,在我的“AuthSource.authenticate”方法中,我想知道我包含在其中的“用户”对象。否则,我最终将不得不像这样调用函数:

user.AuthSource.authenticate(user, password) // note the redundancy in the use of "user"

我应该能够相信我通过 User 对象调用 AuthSource 模型上的方法并实际从该方法中读取该对象。

最佳答案

自从我完成冷融合以来已经有很长一段时间了,所以请原谅我的伪代码,但我认为在这种情况下通常会做的是让父对象在实例化时将其自身的副本发送给子对象 child 。这用于许多 OOP 设计模式中,其中两个对象需要以双向方式相互通信,而不仅仅是父调用子方法。

因此您的子类将被定义为如下所示:

<cfcomponent>
<cffunction name="init">
<cfargument name="parentParam" required="yes" type="object">
<cfset this.parent = parentParam >
<cfreturn this>
</cffuncton>
<cffunction name="locateMe">
<cfreturn "I belong to #this.parent.className# !">
</cffunction>
<cffunction name="doOtherStuff">
<cfreturn "I do stuff with my parent: #this.parent.className# !">
</cffunction>
</cfcomponent>

然后当你使用它时......

<cfset myParent.child = createObject("component", "Object").init(myParent) />
#myparent.locateMe()#
#myparent.doOtherStuff()#

parentParam 将是名为“init”的构造方法中的必需参数,因此子项始终具有对其父项的引用。然后你所有的方法都可以使用 this.parent 来处理它。在我的代码示例中,我做了#this.parent.className#,但不知道冷融合对象有这样的属性。也许您可以使用某种反射或元编程来做同样的事情。

请注意:据我所知,coldfusion 不支持内置构造函数,因此我向您展示的是来自该站点的社区标准最佳实践:

http://www.iknowkungfoo.com/blog/index.cfm/2007/8/22/Object-Oriented-Coldfusion--1--Intro-to-Objectcfc

很抱歉,顺便说一句,你在做混杂……;)

关于java - 如何确定 ColdFusion 对象所在的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9319032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com