1

I asked this previous question regarding multiple inheritance in coldfusion cfcs. Now, I have a question of whether this would work to mimic multiple inheritance in cfcs and what the drawbacks might be.

Suppose I have these 4 classes: Animal, Horse, Bird, Pegasus.

  • Horse and Bird are subclasses of Animal.
  • Pegasus is a subclass of Horse.
  • However, Pegasus flies the same way that an instance of the Bird Class does.

Instead of defining fly() identically twice in the Bird class and the Pegasus class, can I define it in a separate file and include it in the cfc of Pegasus and Bird? Are there any drawbacks to doing this?

horse.cfc

    <cfcomponent extends="animal">
       <!--- misc functions run, eat, whinny... --->
    </cfcomponent>

bird.cfc

<cfcomponent extends="animal">
<cfinclude template="flier.cfm">
       <!--- misc functions hop, eat, chirp... --->
</cfcomponent>

pegasus.cfc

<cfcomponent extends="horse">
<cfinclude template="flier.cfm">
</cfcomponent>

flier.cfm

<cffunction name="fly">
<!--- flying action --->
</cffunction>

I have tried this code and at first glance, it seems to work for what I'm trying to accomplish, but since it was not a mentioned solution to my previous question about multiple inheritance in CFCs, I would like some feedback as to whether this would be a good way to go or not.

Community
  • 1
  • 1
jessieloo
  • 1,759
  • 17
  • 24
  • 1
    Well, have you tried it to see what happens? It looks like you pretty much have the sample code set up. Though there are probably good reasons to use another design pattern, I can confirm that it is possible to mix in UDFs via include. Those UDFs should live in the context of their target CFC and share it's variables and this scope. Some frameworks do this to easily include UDF libraries without relying on inheritance or composition or for weaving AOP aspects. – Brad Wood Sep 02 '14 at 15:33
  • Well yes it *was* mentioned in the answer I gave (and you accepted) on the other question. Third para. Not your exact approach here, but same thing. – Adam Cameron Sep 02 '14 at 16:04
  • For this particular example, putting the fly() function in the animal.cfc seems to be a very simple way of achieving your goal. Having said that, I once wrote a .net class library where I had functionality that had to be available to more than one class. The method I used was to create a project of HelperClasses. The CF equivalent would be to create cfc's and create objects in your other cfc's. However, that's very similar to using cfinclude as you mention in your question. – Dan Bracuk Sep 02 '14 at 16:41
  • My apologies, @Adam. I didn't realize that this was just an example of what you were suggesting. I am still trying to get the hang of using cfcs and their best practices. – jessieloo Sep 02 '14 at 16:44
  • No prob. Using an include is certainly an easy way to do it if you can defined your mix-in requirements before compile time (ie: in your source code). My suggestion was more doing it at run time anyhow. – Adam Cameron Sep 02 '14 at 18:04
  • @DanBracuk makes a good point. If you want to do this in source code, simply hardcoding an instantiation of a dependent CFC would be better than including a library of UDFs. – Adam Cameron Sep 02 '14 at 18:05

2 Answers2

2

Yes, what you described will work and is a technique used relatively frequently for various reasons.

That said, when you ask questions here on SO it is in your best interest to try things, especially if they are easy to try, before you ask if they will work. Generally, when you come here to ask, you should phrase your questions as:

  • This is my problem
  • This is what I've tried
  • This is the error message I'm getting
  • What am I doing wrong?
Adam Tuttle
  • 19,505
  • 17
  • 80
  • 113
  • I apologize. I did try it and it does seem to work for what I want so far. However, since no one mentioned this as an option for my previous option, I am just wondering if it is a practical option or if I'm just digging myself a hole. I didn't think it would be right to simply append it to my original question as I think it's a separate question. I edited my question to try to clarify. I appreciate your input. – jessieloo Sep 02 '14 at 15:58
1

Consider this a continuation to the reply I just posted to your last question. Again, WireBox already has some functionality called "runtime mixins" that is similar to what you as asking for.

http://wiki.coldbox.org/wiki/WireBox.cfm#Runtime_Mixins()

Basically, when you use WireBox to create your objects, you can specify an annotation on the component to a file of UDFs that will be added as public method.

// Via annotation
component mixins="/helpers/base"{

}

You can have multiple mixin files, this simulating multiple inheritance. If you don't find it directly useful, you can at least use its implementation for inspiration.

Full disclosure: I am a contributing member of the Box libraries.

Brad Wood
  • 3,863
  • 16
  • 23