Some times you want to make a nice generic class that a user can extend via subclassing.
However, one issue arises with this.
How can a factory method in the super class create instances of the subclasses ? This is especially troublesome if the classes you have are encrypted so you cant change the source or in a plugin.
However, it IS possible to create an “extensible factory method” so the factory can create instances of the subclasses users provide.
Lets look at how you can do this.
Usually a class factory method looks something like
Class MySuperClass
Private Sub Constructor()
End Sub
Shared Function CreateNew()
return new MySuperClass
End Function
End Class
The private constructor makes it so no one can create an instance without using the factory.
So how do you make this basic pattern extensible so the factory can create instances of the correct subclasses ?
If you had the source code you could change the factory method to
Shared Function CreateNew(name as string)
select case name
case “MyCustomSubclass”
return new MyCustomSubclass
else
return new MySuperClass
end select
End Function
But there are some real problems with this
the superclass has to be alterable – if you dont have source code you cant do this. So distributing code to other people to uses becomes a real problem
every time you add a subclass you have to remember to alter the superclass
What do you do if this is a plugin written in C++ ?
As the author of a class thinking about how to solve these issues is really important. And this one is solvable. For pure Xojo code and for plugin authors.
Recall that Introspection allows you to look at a class, and its constructors. As well you can invoke one and get back a new instance.
So lets create a mechanism to let the superclass know about subclasses, and then use that knowledge to create the right instances when we need them.
First lets add the “way to know about subclasses”
At runtime the way to “know” about other classes is .. introspection !
And that knowledge is a typeinfo object.
So our superclass needs a way to get the typeinfo’s of subclasses and hang on to it for later use.
Holding on to it for later is easy – an array of Introspection.TypeInfo objects will suffice.
Dim mSubTypes() as Introspection.TypeInfo
Now how do we get all the info about subtypes ?
We cant just go through all the objects that exist at runtime because if there are no instances of subtypes yet then we would not find any. So we need some way to “register” the subtypes with the superclass so it can use this information later.
So a method like
Shared Sub RegisterSubType( subType() as Introspection.TypeInfo)
that adds to the array we defined earlier should suffice
Our class now looks like
Class MySuperClass
Private Sub Constructor()
End Sub
Shared Function CreateNew(name as string)
select case name
case “MyCustomSubclass”
return new MyCustomSubclass
else
return new MySuperClass
end select
End Function
Shared Sub RegisterSubType(
subType as Introspection.TypeInfo)
mSubtypes.Append subType
End Sub
Private Dim mSubTypes() as Introspection.TypeInfo
End Class
And now all we really need to fix is the CreateNew method so it can return a subtype properly
We’ll leave the parameters as is for now – so we create “by name”
This has some implications, like if you make a typo you wont get back the instance you asked for, but we can fix that later.
The first thing I’ll do is slightly alter the method signature so it has a default parameter.
Shared Function CreateNew(name as string = “”)
And that’s if for changing the method signature
This allows us to do a tiny bit of optimization AND makes writing code a tiny bit easier
So first things first – if NO name is supplied we just return a new instance of the superclass.
if name = “” then
return new MySuperClass
end if
And after this we have to find the right “TypeInfo” and create an instance from that. And should we find no match we should return nil – which can be used as an error check to find any issues.
Each TypeInfo object has a NAME property.
So we an iterate through our list of registered TypeInfo objects, find the one with the right name, and invoke its Constructor.
For i As Integer = 0 To mSubTypes.Ubound
// find the type info with the right name
If mSubTypes(i).Name = subtype then
// found it so get a list of its constructors
Dim cInfo() As Introspection.ConstructorInfo =
mSubTypes(i).GetConstructors
// and find the 0 param constructor
// since out factory does not pass along any params
For j As Integer = 0 To cInfo.Ubound
Dim pInfo() As Introspection.ParameterInfo =
cInfo(j).GetParameters
// has no params ?
If pInfo.ubound < 0 Then
// yes - invoke this one & return the result
// which will be an instance of the right type
Return cInfo(j).Invoke
End If
Next
End If
Next
// if we reach here we will be default return a nil
// or we could explicitly return nil
And there we go – a superclass with a factory method that can be extended by anyone – with or without source code !
The end result is a class as follows
Class MySuperClass
Private Sub Constructor()
End Sub
Shared Function CreateNew(name as string = “”)
if name = “” then
return new MySuperClass
end if
For i As Integer = 0 To mSubTypes.Ubound
// find the type info with the right name
If mSubTypes(i).Name = subtype then
// found it so get a list of its constructors
Dim cInfo() As Introspection.ConstructorInfo =
mSubTypes(i).GetConstructors
// and find the 0 param constructor
// since out factory does not pass along any params
For j As Integer = 0 To cInfo.Ubound
Dim pInfo() As Introspection.ParameterInfo =
cInfo(j).GetParameters
// has no params ?
If pInfo.ubound < 0 Then
// yes - invoke this one & return the result
// which will be an instance of the right type
Return cInfo(j).Invoke
End If
Next
End If
Next
// if we reach here we will be default return a nil
// or we could explicitly return nil
End Function
Shared Sub RegisterSubType(
subType as Introspection.TypeInfo)
mSubtypes.Append subType
End Sub
Private Dim mSubTypes() as Introspection.TypeInfo
End Class
Something you might do is to make the factory NOT take a string but a typeinfo so that there’s no “typo’s” to debug at runtime. Using a typeinfo would mean most typo issues would be caught at compile time.
That’s left as an exercise for the reader.