views:

84

answers:

2

Hi i am educating myself oop principles. I would like to know if this is a correct example of Cardellis definition of parametric polymorphism. Please enlighten me.
The example is in cfml's script based syntax.

<cfscript>
Parent = createobject("component","webapp.model.Parent").init();
Child = createobject("component","webapp.model.Child").init();
GrandChild = createobject("component","webapp.model.GrandChild").init();
Test = createobject("component","webapp.model.DealWithObject");
dump(Test.getNumberOfParents(Parent));
dump(Test.getNumberOfParents(Child));
dump(Test.getNumberOfParents(GrandChild));
</cfscript>

<cfcomponent>
<cfscript>
// should deal with an infinte number of abstract data types (because of common structure)
public numeric function getNumberOfParents(component arg){
  return -1 + arraylen(structfindkey(getmetadata(arguments.arg),"extends","all"));
}
</cfscript>
</cfcomponent>
+1  A: 

I don't believe this pp since the function is explicitly dealing with the type of the any argument. The point of pp is that the function works without regard to the type of the object.

If I introduce a new type into the system, this function will break since it does not have special handling for it.

EDIT: I think your updated example is subtype polymorphism, since the function will handle object and any of it's subtypes, by virtue that getmetadata handles object (and by substitution principle, it's subtypes.)

mdma
Yeah, i reread the Cardelli article. i guess my example is bad because i am using primitive and abstract data types. I will rewirte it.
mrt181
Not the primitive data types are the problem. Whenever you begin to write code that checks if the passed object is of type Foo/Bar/Whatever you should stop and try to find another solution (there are some special cases in which this would be neccessary but those are very, very rare, i.e. serialization and ORM require those checks internally).
dbemerlin
So, does the updated example work as pp?
mrt181
Sorry, my CF knowledge is limited so i'm not sure, but it doesn't look as horrible as the original so it should be ok.
dbemerlin
Ok, in my example they look as if they are all related but i do not specify this in the method signature. To realize subtyping-polymorphism i would have to specify the ancestor class like this getNumberOfParents(Parent arg). This way only Parent and its sub-types would be accepted. By specifying component i introduce a boundary on the accepted data-types - limiting it to objects. I couldn't pass an array or string to this method - getNumberOfParents(component arg) can handle any abstract data-type (you can in theory create an infinte number of classes).
mrt181
+1  A: 

No, just no.

Polymorphism means that you do not have to check what type something is, you just use it.

An example would be (C#):

public Boolean AreEqual(Object o1, Object o2)
{
  return o1.Equals(o2);
}

The Method can accept any type of Object that inherits from Object (in C# almost everything) and Object implements Equals, so you can use it to make the check and don't have to check the type of any parameter.
Usually you accept some kind of interface to make sure the object supports the operation you want to perform.

dbemerlin
yes, realised this myself, i have changed the example
mrt181