"Baris" <ne*******@acar .org.uk> wrote in message
news:40******** ******@acar.org .uk...
Frank Oquendo wrote: Baris wrote:
Given the C# code below, can anyone think of a better
class design?
What really gets my goat is that the code within derived
classes D1 and D2 is identical and in my mind should be
refactored into a base class, but how can I move this
particular 'shared functionality' into
a base class?
It would seem the *only* thing you have is a base class as
your example shows no reason to have either derived class.
What exactly are you after?
Conceptually, I want to associate data/functionality with
a given class at runtime, that can be returned from a virtual
method on a base class.
IRL, these classes are derived from a class that derives from
TreeNode, and the static members represent class-specific
context menus; for a node of a given type I want a particular
context menu. At runtime, I set the menus for each class.
The following [essentially an implementation of an abstract 'Menu' factory]
is probably overkill for your needs, but:
* It ensures that most of the functionality resides in the
base class
* Does not rely on any language-specific facility e.g.
reflection [though could easily be altered to do so]
* May easily be expanded to have the menu setup code reside
in a method rather than in the derived class constructors
as is currently the case
I hope this helps.
Anthony Borla
// ----------------- Code Sample Start --------------------
using System;
using System.Collecti ons;
class Application
{
public static void Main()
{
Menu m;
m = new D1().GetMenu();
Console.WriteLi ne(m);
m = new D2().GetMenu();
Console.WriteLi ne(m);
m = new D3().GetMenu();
Console.WriteLi ne(m);
}
}
class Menu
{
public Menu(String name)
{
this.name = name;
}
public override String ToString()
{
return name;
}
private String name;
}
abstract class Base
{
abstract public Menu GetMenu();
protected static Menu GetMenu(String name)
{
return (Menu) menuTable[name];
}
protected static Boolean RegisterMenu(St ring name, Menu menu)
{
menuTable.Add(n ame, menu);
return menuTable.Conta insKey(name);
}
protected static void DropMenu(String name)
{
menuTable.Remov e(name);
}
private static Hashtable menuTable = new Hashtable();
}
class D1 : Base
{
public D1()
{
if (!registered)
{
Menu menu = new Menu(menuName);
// Customise menu ...
registered = RegisterMenu(me nuName, menu);
}
}
public override Menu GetMenu()
{
return GetMenu(menuNam e);
}
private static Boolean registered = false;
private static String menuName = "D1";
}
class D2 : Base
{
public D2()
{
if (!registered)
{
Menu menu = new Menu(menuName);
// Customise menu ...
registered = RegisterMenu(me nuName, menu);
}
}
public override Menu GetMenu()
{
return GetMenu(menuNam e);
}
private static Boolean registered = false;
private static String menuName = "D2";
}
class D3 : Base
{
public D3()
{
if (!registered)
{
Menu menu = new Menu(menuName);
// Customise menu ...
registered = RegisterMenu(me nuName, menu);
}
}
public override Menu GetMenu()
{
return GetMenu(menuNam e);
}
private static Boolean registered = false;
private static String menuName = "D3";
}