0

I want to do this in the right way to learn

I have a few classes which have only one method. For example:

public class RedColorText
{
   public void AddRedColorText(string text)
   {
      //something here
   }
}

public class WhiteColorText
{
   public void AddWhiteColorText(string text)
   {
      //something here
   }
}

Normally I could just create instance of each one of them based on the method I want to use but what if I got requirement to use only one class and call methods based on condition? Can I do this like below using constructor and switch? Or should I use something else like interfaces etc?

public class ColorText
{
    public ColorText(enum condition, string text)
    { 
        switch(condition)
        {
            case 1:
                new RedColorText().AddRedColorText(text);
                break;
            case 2:
                new WhiteColorText().AddWhiteColorText(text);
                break;
        }
    }

}
7
  • 5
    Why would you want to name each class for a color and then name its accessor method also for that color? The normal thing to do would be to make your classes inherit from a common superclass and give them the same method, something like addText(). Jan 13, 2015 at 12:00
  • @KilianFoth I didn't write the code and now I have to modify it. Is rewriting it the only solution?
    – cah1r
    Jan 13, 2015 at 12:20
  • 1
    @MateuszMigała If you want to do it properly, then yes. Very often the solution to badly designed code is a massive refactoring.
    – l0b0
    Jan 13, 2015 at 12:30
  • You can also do reflection to avoid duplicate code. But this should only be your last resort as it makes code much harder to reason about and makes it impossible for the compiler to find errors for you at compile time. I just wanted to add it so that you know this siluation exists when you are absolutely unable to change the existing code.
    – valenterry
    Jan 13, 2015 at 12:59
  • 1
    Individual colors are not classes; Color is a property. Jan 13, 2015 at 17:19

1 Answer 1

4

You could always use an interface or an abstract class to define the contract where you have generalized behavior for the ColorText class, but you need to keep the method name the same (AddColorText)

public interface IColorText
{
    public abstract void AddColorText(string text);
}

public class RedColorText : IColorText
{
   public void AddColorText(string text)
   {
      //something here
   }
}

public class WhiteColorText : IColorText
{
   public void AddColorText(string text)
   {
      //something here
   }
}

And then define a factory to create instances based on your type

public class ColorTextFactory
{
    public static IColorText Create(enum condition)
    {
        switch(condition)
        {
            case 1:
                return new RedColorText();
            case 2:
                return new WhiteColorText();
            default :
                return null;
        }
    }
}

If you want to create red ColorText class and add some text, following code can be used

var colorText = ColorTextFactory(1);
colorText.AddColorText("Red Color Text");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.