|
-
April 23rd, 2007, 04:44 PM
#1
design pattern problem
This isn't really a C# problem, but more of a design pattern problem.
I have the following abstract class..
abstract class SeriesOperator
{
private int _precedence;
public abstract double eval(double lhs, double rhs);
public SeriesOperator(int precedence)
{
_precedence = precedence;
}
public static bool IsAnOperator(char x)
{
if ((x == '+') || (x == '-') || (x == '*') || (x == '/') || (x == '(') || (x == ')'))
return true;
else
return false;
}
public static SeriesOperator CreateOperator(char x)
{
SeriesOperator ret = null;
switch (x)
{
case '+':
{
ret = new Add();
break;
}
case '-':
{
ret = new Subtract();
break;
}
.
.
.
default:
break;
}
return ret;
}
and multiple subclasses
i.e.....
class Add : SeriesOperator
{
public Add():base(1)
{
}
public override double eval(double lhs, double rhs)
{
return lhs + rhs;
}
}
The problem is that everytime I need to create a new subclass, I would need to modify the abstract class's methods IsAnOperator and CreateOperator.
How can I rewrite this class so that evertime I create a new subclass I would not have to touch the abstract base class?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|