A C# question about constructors/static methods and inheritance:
Please help me make my code simpler!
For fun and as an exercise I wrote somewhat classical B-tree
implementation
in C# which I later ported to java and Python for comparison.
[ See http://bplusdotnet.sourceforge.net/ for full details and code
]
The basic tree implementation has a number of subclasses which are
very
similar except for the types of certain data elements and the return
types of certain static methods. The only way I could think of to
implement these involved a lot of cutting and pasting. For example,
from hBplusTree.cs
public class hBplusTree : BplusTree
{
hBplusTreeBytes xtree;
public hBplusTree(hBpl usTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
.....
public static new hBplusTree Initialize(stri ng treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
hBplusTreeBytes tree = hBplusTreeBytes .Initialize(tre efileName,
blockfileName, PrefixLength, CultureId, nodesize, buffersize);
return new hBplusTree(tree );
}
public static new hBplusTree Initialize(stri ng treefileName, string
blockfileName,
int PrefixLength, int CultureId)
{
hBplusTreeBytes tree = hBplusTreeBytes .Initialize(tre efileName,
blockfileName, PrefixLength, CultureId);
return new hBplusTree(tree );
}
.....
public static new hBplusTree ReOpen(System.I O.Stream treefile,
System.IO.Strea m blockfile)
{
hBplusTreeBytes tree = hBplusTreeBytes .ReOpen(treefil e, blockfile);
return new hBplusTree(tree );
}
public static new hBplusTree ReOpen(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes .ReOpen(treefil eName,
blockfileName);
return new hBplusTree(tree );
}
public static new hBplusTree ReadOnly(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes .ReadOnly(treef ileName,
blockfileName);
return new hBplusTree(tree );
}
.....
and from xBplusTree.cs
public class xBplusTree : BplusTree
{
xBplusTreeBytes xtree;
public xBplusTree(xBpl usTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
.....
public static new xBplusTree Initialize(stri ng treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
xBplusTreeBytes tree = xBplusTreeBytes .Initialize(tre efileName,
blockfileName, PrefixLength, CultureId,
nodesize, buffersize);
return new xBplusTree(tree );
}
public static new xBplusTree Initialize(stri ng treefileName,
string blockfileName, int PrefixLength, int CultureId)
{
xBplusTreeBytes tree = xBplusTreeBytes .Initialize(tre efileName,
blockfileName, PrefixLength, CultureId);
return new xBplusTree(tree );
}
.....
public static new xBplusTree ReOpen(System.I O.Stream treefile,
System.IO.Strea m blockfile)
{
xBplusTreeBytes tree = xBplusTreeBytes .ReOpen(treefil e, blockfile);
return new xBplusTree(tree );
}
public static new xBplusTree ReOpen(string treefileName, string
blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes .ReOpen(treefil eName,
blockfileName);
return new xBplusTree(tree );
}
public static new xBplusTree ReadOnly(string treefileName,
string blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes .ReadOnly(treef ileName,
blockfileName);
return new xBplusTree(tree );
}
.....
(look familiar?)
Without going into great detail the Python implementation is much
simpler
(or at least shorter) than this due to Python's very relaxed type
system.
My question is: Is there a better way to do this? Please inform!
Thanks,
-- Aaron Watters
===
Let's not elect him in 2004 either.