ColdFusion组件可以共享方法,而不是同一个超类的后代 [英] Can Coldfusion components share methods without being descendants of the same super class

查看:124
本文介绍了ColdFusion组件可以共享方法,而不是同一个超类的后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用了一个本地化版本的面向对象的冷灌注一段时间,我开始尝试cfc的和如何应该完成...

We have used a homegrown version of object oriented coldfusion for a while and I'm just starting to experiment with cfc's and how it "should" be done...

如果我理解正确,cfinterface定义函数的签名,任何实现该接口的类都必须有自己的函数来做接口中定义的内容。

If I understand correctly, cfinterface defines the signature of functions, and any class that implements that interface must have their own functions to do whats defined in the interface.

I这种接口不仅仅定义函数的签名,而且定义了函数的逻辑,并且实现该接口的任何东西都可以使用它的函数,而不必定义它自己。

I'm kind of trying to do the opposite - the interface doesn't just define the function's signature, but also defines the logic of the function and anything that implements that interface can use its functions without having to define it itself. Does that exist besides creating subclasses?

例如,假设你有A,B,C,D类,所有类都属于Animal类。

For example, say you have classes A,B,C,D that all belong to the Animal class


  • A& B可以走路

  • A& C可以说话

  • D可以睡眠

  • 假设walk,talk&

  • 理想情况下,如果A& B都实现了步行界面,他们可以走路,而不必在每个类中定义单独的步行方法。

  • A & B can walk
  • A & C can talk
  • B & D can sleep
  • Suppose the logic of walk, talk & sleep (if the object can do it) is the same regardless of the class doing it
  • Ideally, if A & B both implement the walking interface, they can walk without defining a separate walk method in each class.

或从这个借来更好的例子 java多继承问题

Or borrowing a better example from this java multiple inheritance question


  • 飞马是马和鸟的混合物,因为它像马
    运行,但像鸟一样飞翔

这是可能吗? (我认为这是多重继承?)

Is that possible? (I think this is multiple inheritance?)

推荐答案

总之:接口只定义了一个契约,并且不能)定义功能)。另外CFML没有多重继承的概念。

In short: no, an interface only defines a contract, it does not (and cannot) define functionality). Also CFML does not have the concept of multiple inheritance.

你必须使用单继承和具体实现来实现你所需要的。我不能打扰评估你的实现共享需求,以确定一个approrpriate类层次结构可能是为了最小化代码重复。我相信你自己可以做到这一点(它并不是你的问题的真正的一部分)。

You will have to use single-inheritance and concrete implementations to effect what you need. I can't be bothered assessing your implementation-sharing requirements to work out what an approrpriate class hierarchy might be to minimise code duplication. I'm sure you can do that yourself (and it's not really part of your question anyhow).

你可以尝试的一个策略是使用mixins作为你的常用方法。将常用方法存储在不同的库中,然后根据需要将它们注入到对象中。所以基本上Mixins.cfc会实现 walk() talk() ,您将有 AFactory.cfc BFactory.cfc CFactory.cfc 。当向工厂询问新的 A B C ,而factory方法在返回实例之前注入mixin方法。显然这是一个相当麻烦的过程,你想使用某种IoC容器来管理所有这一切。

One tactic you could try is to use mixins for your common methods. Store the common methods in a different library, and then inject them into your objects as required. So basically Mixins.cfc would implement walk(), talk(), sleep(), and you'd have an AFactory.cfc, BFactory.cfc, CFactory.cfc. When asking a factory for a new A, B or C, and the factory method injects the mixin methods before returning the instances. Obviously this is a fairly cumbersome process, and you'd want to use some sort of IoC container to manage all this.

一个更好的问题可能会从你显示我们更多的现实世界的例子...我怀疑你的域设计可能会改善,如果你发现自己需要做你的例子建议。实际设计要求很少通过使用动物的示例公开。

A better question might come out of you showing us more real world examples... I suspect your domain design could perhaps stand improvement if you find yourself needing to do what your example suggests. Actual design requirements are seldom exposed with examples using animals.

这篇关于ColdFusion组件可以共享方法,而不是同一个超类的后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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