473,799 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class design quandary

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?

If anyone can suggest a better newsgroup for this, such as one focussed
primarily on C# class design, please let me know.

[This could be easily done with an intermediate template class in the
hierarchy in C++, I think.]

// quandary.cs
// compile with csc.exe quandary.cs
class app
{
public static void Main()
{
D1 d1 = new D1();
D2 d2 = new D2();

D1.SetFoo("some thing marvellous, specific to class D1");
D2.SetFoo("some thing wonderful , specific to class D2");

System.Console. WriteLine(d1.Ge tFoo());
System.Console. WriteLine(d2.Ge tFoo());
}
}

abstract class Base
{
public abstract string GetFoo();
}

class D1 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}

class D2 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
// quandary.cs ends

Nov 15 '05 #1
6 1822
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?

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #2
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.

Does that help?

Baris

Nov 15 '05 #3
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?


BTW, I disagree with this statement; I wouldn't get the same
functionality with only one class. These statements from the Main()
wouldn't print different values for these two lines if d1 and d2 were
instances of the same class:

System.Console. WriteLine(d1.Ge tFoo());
System.Console. WriteLine(d2.Ge tFoo());

Baris.

Nov 15 '05 #4
Hi Baris,

You might be able to do something along the lines you want via reflection.
Heres an example of a line of code from one of our base classes that
'reaches' upwards in the class hierachy:

string strTableName = (string)GetType ().InvokeMember ("TableName" ,
BindingFlags.Pu blic | BindingFlags.Ge tProperty | BindingFlags.St atic, null,
null, new object [] {});

Cheers

Doug Forster

"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?


BTW, I disagree with this statement; I wouldn't get the same
functionality with only one class. These statements from the Main()
wouldn't print different values for these two lines if d1 and d2 were
instances of the same class:

System.Console. WriteLine(d1.Ge tFoo());
System.Console. WriteLine(d2.Ge tFoo());

Baris.

Nov 15 '05 #5

"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";
}
Nov 15 '05 #6
Hi Baris,

You may want to experiment with something like the following. The idea is
that Base has a protected nested class Data and a protected abstract
method/property to get the data from an instance. Each derived class has a
static field of type Data and implements the GetData method to return the
value of its field.

Derived classes also need to either supply stub methods that just delegate
to Base/Data or alternatively, Data can implement some interface IFoo and
each derived class has a static GetIFoo method/property.

// This is really only needed if you decide NOT to have stub methods for
every bit of functionality.
interface IFoo
{
void SetFoo(string s);
public string GetFoo();
}

abstract class Base
{
protected class Data : IFoo
{
public string _foo;

// The methods are only needed if you use the IFoo interface.
public void SetFoo(string s) { _foo = s; }
public string GetFoo() { return _foo; }
}

// Derived classes are required to provide us with their data.

protected abstract Data GetData();

// Base can now fully implement the instance methods (except for the
call to GetData() of course).
public string GetFoo() { return GetData()._foo; }
public void SetFoo(string s) { GetData()._foo = s; } // optional. Can
also use D1.SetFoo(...)
}

class D1 : Base
{
private static Data data = new Data();

// So Base can get our data.
protected override Data GetData() { return data; }

// You can have a static GetIFoo so clients can call
GetIFoo().SetFo o("Hello").
// This helps if there are lots of methods on the IFoo interface.
public static IFoo GetIFoo() { return data; }

// Or have stub methods. Or have both a static GetIFoo plus stub methods
for the most commonly used methods.
public static void SetFoo(string s)
{
data.SetFoo(s);
}
}

class D2 : Base
{

private static Data data = new Data();

// So Base can get our data.
protected override Data GetData() { return data; }

// D2 might have a different set of stub methods than D1 if so desired.
public static IFoo GetIFoo() { return data; }
}

Hope this helps.

Shon Katzenberger

"Baris" <ne*******@acar .org.uk> wrote in message
news:10******** *******@linuxbo x.localdomain.. .
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?

If anyone can suggest a better newsgroup for this, such as one focussed
primarily on C# class design, please let me know.

[This could be easily done with an intermediate template class in the
hierarchy in C++, I think.]

// quandary.cs
// compile with csc.exe quandary.cs
class app
{
public static void Main()
{
D1 d1 = new D1();
D2 d2 = new D2();

D1.SetFoo("some thing marvellous, specific to class D1");
D2.SetFoo("some thing wonderful , specific to class D2");

System.Console. WriteLine(d1.Ge tFoo());
System.Console. WriteLine(d2.Ge tFoo());
}
}

abstract class Base
{
public abstract string GetFoo();
}

class D1 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}

class D2 : Base
{
private static string _foo;
public static void SetFoo(string s)
{
_foo = s;
}
public override string GetFoo()
{
return _foo;
}
}
// quandary.cs ends

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4814
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. The trick is, the instatiation will be based on a string with the exact same name as the type that I am trying to instantiate. Obviously, writing a fifty element long switch statement is an inferior solution. So, how can I instantiate based on...
15
9082
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
9
2240
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that all have a different enumeration in them called Properties. I want to basically have a variable The_Class that I can dynamically point to either Class_A, Class_B, or Class_C and then do The_Class.properties to get the correct enumeration. How...
3
1936
by: Trammel | last post by:
Hi, I recently upgraded to VB.net from VB6.. and woah... I feel lost :¬O One of my reasons for upgrading is I was told that VB.net can do class inheritance and subclassing easier. Would someone be so kind as to provide a small demo about classes for some
6
2082
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple DeviceManager which is only interested in CD/DVD devices. I want to get the list of CD/DVD devices and "be informed when a disc inserted into a device". I am developing this on Linux. So, I used HAL API and read some system (actually /proc) files to gather...
6
2142
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
5
1741
by: pgrazaitis | last post by:
I cant seem to get my head wrapped around this issue, I have myself so twisted now there maybe no issue! Ok so I designed a class X that has a few members, and for arguments sake one of the members Y is the location of a file to be read. The original design assumes that this class will be instantiated and each instance will happily mange its own members. (ie One file location per instance...no thread-safety). Now another class A...
6
8165
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is common for subclasses but the members are common within subclass only (static member of subclass) but...
1
1463
by: Mike | last post by:
I have always been told that I should only get the data I need for each web page, so my quandary is set against that backgound. Let's say I have a User class with 10 properties. I have a private method that populates all of these properties with values from the database. However, I may only need to display two of the properties on a web page (name and last login time, for example). Coming from a pure ADO/ADO.NET background, where I...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.