July 14, 2018
OOP and ColdFusion – Part 2
Comments
(0)
July 14, 2018
OOP and ColdFusion – Part 2
Staff 4 posts
Followers: 2 people
(0)

In my previous blog, I wrote about the following OOP features:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstract CFCs & Methods

In ColdFusion 2018, we support ‘Covariant Method Return Type’. This is another OOP concept and has been adopted by many modern programming languages.

Covariant Method Return Type

Most modern programming languages support ‘subtyping’. Let me explain subtyping by an example. If the return type of my class function is ‘Animal’ while overriding the function in a subclass, you can specify the return type to be ‘Cat’ which is a subtype of ‘Animal’. Covariance defines subtyping in complex and simple types.

In ColdFusion 2018, when a CFC implements an interface function the return type of the function can be ‘Covariant’.

Let’s explore our CFC kingdom to understand Covariance.

I have an Animal Interface with functions that are common to any kind of animal. With CF 2018,  you can also define default functions in interfaces. You can read more about it on this blog. But for simplicity, I have not used default functions in IAnimal.

interface displayName="IAnimal" hint="Animal Interface."{
     
    any function eat(any prey="");
    any function makeNoise(string noise);
     
}

Then I have an Animal CFC, which implements this interface. While implementing the interface function eat() & makenoise(), it overrides the return type to a more specific type. This is known as Covariant Method Return Type.

component implements="IAnimal" displayname="Animal"{
	property animalSize;
	property animalType;
	function init(animalSize,animalType){
		variables.instance.animalSize = arguments.animalSize
		variables.instance.animalType = arguments.animalType
	}
	Animal function eat(any prey=""){
			return prey;
		}
	string function makeNoise(string noise){
			return noise;
		}
	function getanimalSize(){
		return variables.instance.animalSize
	}
	function getanimalType(){
		return variables.instance.animalType
	}
}

Let’s see how my animal kingdom eats & make noise.

<cfscript>
	bunny=new Animal('small','rabbit');
	simba =new Animal('large','Tiger');
	writeoutput("Simba eats " & simba.eat(bunny).getAnimaltype() & "<br/>");
	writeoutput("Simba " & simba.makenoise('roarsss'));
</cfscript>

This is a small example of how you can use Covariance in ColdFusion. Let’s move to another OOP feature that was introduced in ColdFusion 2018, ‘final variables, methods & component’.

Final

ColdFusion 2018 improves on its object-oriented feature set by introducing support for Final Components, Final Methods, and Final Variables. Final provisions for non-access modifiers, disallowing modifications to maintain stricter programming constructs.

Final can be applied to three contexts. When a variable is declared with a Final keyword, its reference cannot be modified.  When a method in a component is declared as a final, it cannot be overridden by sub-components.  When a component is declared as final, other components will not be allowed to extend the former.

Let’s look at each of these in details.

Final Variables

Variables declared as final are ensured to be constants for the rest of the execution. The value of a final variable cannot be updated post-initialization. It’s important to note that variables that contain references to other objects cannot be re-bound to reference a new object, but the internal state of the object itself may be changed. i.e., If a final variable holds a reference to an array, the reference may not be updated to a new array – but the contents of the original array itself may be updated.

Eg:

final name = ”John Doe”
name = “John Roe” // Throws a final variable exception
final details = {“age”: 35}
details[“nickname”] = “John Roe” // Allowed
details = {} // Disallowed – The reference of details cannot be updated.  

 

Final Methods

Methods within a component too can be declared as a final method. Final methods cannot be overridden by sub-components. Final methods can be used to limit the extent to which sub-components redefine the behavior. This may be useful when frameworks/base components are being developed, to ensure the same implementation is being followed in all derived classes.

Eg:

 Comp1.cfc

              component {
					
					public string function A(){
					       return “A”;
					}

					public final string function B(){
							return “B”;

					}           

			  }

Comp2.cfc

           		component extends=”Comp1” {
				
				// Allowed to overwrite method A()

                      public string function A(){
								return “A”;
						}

				// Disallowed – Throws a final method change exception

				      public final string function B(){
								return “B”;
						}
				}

Final Components

Thirdly, a component can also be declared as final. Final components cannot be extended (inherited) by other components. A final component, however, can extend other components. Final components can be used to prevent inheritance and create immutable components. This may be useful when you are trying to establish stricter rules on a component’s capability.

Eg:

Comp1.cfc

              component {    

                  public string function A(){
					return “A”;
				  }
			  }

Comp2.cfc // Allowed

              final component extends=”Comp1” {       

                  public string function B(){
					return “B”;
				  }
			  }

Comp3.cfc // Disallowed – Throws final component cannot be extended exception

              component extends=”Comp2” { 

                    public string function C(){
						return “C”;
						}
			   }

This is all about OOP in ColdFusion. Hope you are excited to use these new features.

0 Comments
Add Comment