CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2007
    Posts
    19

    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?

  2. #2
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: design pattern problem

    Why don't you substitute your fixed list of operators:
    Code:
    if ((x == '+') || (x == '-') || (x == '*') || (x == '/') || (x == '(') || (x == ')'))
    with something more flexible such as an ArrayList of chars?

    Or better yet, use List<char> and then use the List<T>.Contains(T t) method to see if the char is an operator.
    Last edited by petes1234; April 23rd, 2007 at 05:33 PM.

  3. #3
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: design pattern problem

    or perhaps a list of your class: List<SeriesOperator> operators....

    One other thing, please learn about code tags. They're easy to use and make your code readable:
    Code:
        class SeriesOperator
        {
            private static List<char> operators = new List<char>();
    
            private int _precedence;
    
            public abstract double eval(double lhs, double rhs);
    
            public SeriesOperator(int precedence)
            {
                _precedence = precedence;
            }
    
            public static bool IsAnOperator(char x)
            {
                if (operators.Contains(x))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            public static SeriesOperator CreateOperator(char x)
            {
                SeriesOperator ret = null;
                switch (x)
                {
                    case '+':
                        {
                            ret = new Add(x);
                            break;
                        }
                    case '-':
                        {
                            ret = new Subract(x);
                            break;
                        }
                    default:
                        break;
                }
            }

  4. #4
    Join Date
    Jan 2007
    Posts
    19

    Re: design pattern problem

    But then how would you initialize the list?

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: design pattern problem

    I would create a Builder Class, that Initialized the List. This would have to be modified (something does), but it would be a distinct file, that only contained the initialization, and therefore edits could not corrupt the abstract class.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    Re: design pattern problem

    you could also delegate IsOperator check to the implementation class so the base abstract class doesn't need any modification when new implementation is added, and the only list you have will be the list of implementations.

    Code:
        abstract class SeriesOperator
        {
            private int _precedence;
            private static List<SeriesOperator> _registeredOperators = new List<SeriesOperator>();
    
            public abstract double eval(double lhs, double rhs);
    
            protected abstract bool IsOperator(char x);
    
            public SeriesOperator(int precedence)
            {
                _precedence = precedence;
            }
    
            public static void RegisterOperator(SeriesOperator operatorImpl)
            {
                _registeredOperators.Add(operatorImpl);
            }
    
            public static bool IsAnOperator(char x)
            {
                bool retval = false;
                foreach (SeriesOperator operatorImpl in _registeredOperators)
                {
                    if (operatorImpl.IsOperator(x))
                    {
                        retval = true;
                        break;
                    }
                }
                return retval;
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured