ColdFusion , Centaur

Inheritance and composition in ColdFusion 9

1 Comments July 13, 2009

ColdFusion 9 brings a great implementation in the language, regarding OOP,the components ColdFusion now can be written entirely WITHOUT TAGS, and the invocation of these components are accomplished through the operators NEW and IMPORT, where the method init is recognized as constructor, and with the methods getters and settres implicit, in other words, you not need to write.
To exemplify, I ask for license the, but I did a new reading of the article of de Using Inheritance and Composition in ColdFusion Components. I do not go to rewrite him completely, I am going to remake only the classes, the whole remaining of the text still is current, so read Hal's Article.

In the part that talks about inheritance, the classes Vehicle.cfc stays of the following form:

component
    displayname="Vehicle"
    hint="I am a Vehicle"
    output="false"
{
    property Numeric mph;

    public Vehicle function init()
            output="false"
            hint="I am constructor"
    {
        Variables.mph = 0;
        return this;
    }
    
    public Numeric function increaseSpeed(Numeric mph)
            output="false"
            hint="I increase speed"
    {
        Variables.mph = Variables.mph + Arguments.mph;
        return Variables.mph;
    }

    public Numeric function decreaseSpeed(Numeric mph)
            output="true"
            hint="I decrease speed"
    {
        if(Variables.mph >
= Variables.speedIncrement) {
            Variables.mph = Variables.mph - Arguments.mph;
        } else {
            stop();
        }
        return Variables.mph;
    }    

    public void function stop()
            output="true"
    {
        Variables.mph = 0;
        writeoutput("Your vehicle has stopped.<br>");
    }

}

And the class Car.cfc, it stays this way:

component     
        extends="vehicle"
        displayname="Car"
        hint="I am a Car"
        output="false"
{
    property String locomotion;
    property Numeric speedIncrement;
    
    public Car function init()
            output="true"
            hint="I am constructor"
    {
        local.null=super.init();
        Variables.locomotion = "internal combustion engine";
        Variables.speedIncrement = 20;
        writeoutput("Ready car <br>");
        return this;
    }
    
    public Numeric function stepOnGas()
        output="true"
    {
        local.currentSpeed = increaseSpeed( speedIncrement );
        writeoutput("You are now going #currentSpeed# <br>");
        return local.currentSpeed;
    }
    
    public Numeric function stepOnBrakes()
        output="true"
    {
        local.currentSpeed = decreaseSpeed( speedIncrement );
        writeoutput("You are now going #currentSpeed# <br>");
        return local.currentSpeed;
    }
    
    public void function stop()
            output="true"
    {
        super.stop();
    }

    
}

and the test of the classes, TestCar.cfm, of the following form:

<cfscript>
    import CF9Demo.com.pcsilva.demo.machine.Car;
    myCar = new Car();
    myCar.stepOnGas();
    myCar.stepOnGas();
    myCar.stepOnGas();
    myCar.stepOnGas();
    myCar.stepOnBrakes();     
    myCar.stepOnBrakes();     
    myCar.stepOnBrakes();
    myCar.stop();     
</cfscript>

Obtaining the following result:

In the part that Hal talks about composition, the class Address.cfc stays of the following form:

component
        displayname="Address"
        hint="I am an Address"

{
    property String address1;
    property String address2;
    property String city;
    property String province;
    property String postalCode;

    public Address function init()
            output="false"
            hint="I am constructor"
    {
        return this;
    }    
}

The class Person.cfc, this way:

component
        displayname="Person"
        hint="I am a Person"
{
    property String firstName;
    property String lastName;
    property Address address;

    public Person function init()
            output="false"
            hint="I am constructor"
    {
        return this;
    }    
}

The test form is the same, it pages her of processing of the form is that should be changed:

<cfscript>
    add = new CF9Demo.com.pcsilva.demo.people.Address();
    add.setAddress1(form.add1);
    add.setAddress2(form.add2);
    add.setCity(form.city);
    add.setProvince(form.province);
    add.setPostalCode(form.postalCode);
    user = new CF9Demo.com.pcsilva.demo.people.Person();
    user.setFirstName(form.firstName);         
    user.setLastName(form.lastName);         
    user.setAddress(add);         
</cfscript>

<cfdump var="#user#">

Where we obtain the following print on screen.

These are some of the news of ColdFusion 9, accompany this week will be showing more.



Send

  1. Sid Maestre's Gravatar Hi Bob,

    Thanks for taking Hals example and showing it off for ColdFusion 9. I'm still transitioning into OO Concepts and was looking at how Inheritance and Composition differ. Hals and your article together clarified the issue. # Posted By Sid Maestre | 7/5/10 7:53 PM

Share your view

Add Comment

Subscribe

Enter your email address to subscribe to this blog.

Digitrum Servers Powered by Blogcfc 5.9.2.002