jbd
jbd

Reputation: 415

General OO in CFML: Is it possible to dynamically extend a CFC?

(I may be on the wrong track from the start here, so my question title may need editing...)

For an ecommerce app, I have a StoreBase CFC, and "subclass" CFCs StoreCheckout, StoreCart and other modules/objects within the app.

Some clients have module specific biz logic, so we create CFCs with custom functionality as needed -- eg StoreCartClientX extends StoreCart and overrides some calculation.

Some clients also have custom base biz logic -- eg StoreBaseClientX extends StoreBase and restricts access to certain parts of the app based on custom rules.

My problem is that I need a way to tell StoreCart to extend StoreBaseClientX when StoreBaseClientX exists which would seem to require dynamic extension and that seems like a bad idea based on various posts (taken on faith) in SO (and in anycase, I'm using ColdFusion (go ahead, laugh) and dynamic expressions are not allowed for extension.)

Here's some psuedo code to illustrate the problem.

public class StoreBase {
    checkRights(){
      //standard rights check
    }

    findWishList(){
   }
}

public class StoreBaseClientX extends storeBase {
       checkRights(){
         //custom rights check for ClientX
       }
}


   public class StoreCart extends StoreBase { //if StoreBaseClientX exists, it should extend that instead


   }

Upvotes: 2

Views: 249

Answers (3)

Adam Cameron
Adam Cameron

Reputation: 29870

Firstly, is the inheritance model you're using where both StoreCheckout and StoreCart extend StoreBase correct? It seems unlikely to me that StoreBase is such that one could say StoreCheckout is a StoreBase as well as StoreCart is a StoreBase? What are example methods in both? Are you sure StoreCheckout and StoreCart oughtn't be such that each has a StoreBase (ie: the StoreBase object is something the other classes are init'ed with)?

But anyway, that's just something to think about, and tangential to your issue. Maybe pop something on Code Review for us to look at, if you like?

I'd turn things around a bit, as Russ says. instead of having to work out whether a client-specific sub-component exists and then if it does, use that instead of the base component, you should always have the client-customisable sub-component, and always reference those ones in your code, and never the base components.

In short, though, one cannot have a dynamic/runtime extends attribute on a component in ColdFusion, no. It has to be there at compile time.

Upvotes: 0

Russ
Russ

Reputation: 1951

One way to handle this would be to always include StoreBaseClient, rather than trying to make it's existence optional. Just ship StoreBaseClient as an empty stub class that can be customized by the client as needed. Example components:

<cfcomponent name="StoreBase">
   <!--- your code --->
</cfcomponent>

<cfcomponent name="StoreBaseClient" extends="StoreBase">
   <!--- empty --->
</cfcomponent>

<cfcomponent name="StoreCart" extends="StoreBaseClient">
   <!--- your code --->
</cfcomponent>

Hope that helps.

Upvotes: 1

Henry
Henry

Reputation: 32905

Short answer: You can make use of a static factory to create the class for you.

Long answer: From what I read so far, I think you should familiar yourself with the strategy pattern. http://en.wikipedia.org/wiki/Composition_over_inheritance.

Upvotes: 1

Related Questions