Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6980537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:02:02+00:00 2026-05-27T18:02:02+00:00

I have different fruit class, all implements the same interface IFruit : public interface

  • 0

I have different fruit class, all implements the same interface IFruit :

public interface IApple : IFruit{ }  
public interface IBanana : IFruit{ }  
public interface ICarrot: IFruit{ }  

Each of them have their own drawer :

public class AppleDrawer
{
    public void Draw(IApple apple, Graphics graphics){}
}

public class BananaDrawer
{
    public void Draw(IBanana banana, Graphics graphics){}
}

If I want to draw a list of fruit, I am doing the following

public void DrawFruits(List<IFruit> fruits, Graphics graphics)
{
    foreach(var fruit in fruits)
    {
        if(fruit is IBanana)
        {
            var banana = (IBanana)fruit;
            var drawer = new BananaDrawer();
            drawer.Draw(banana, graphics);
        }
        else if(fruit is IApple)
        {
            var apple = (IApple)fruit;
            var drawer = new AppleDrawer();
            drawer.Draw(banana, graphics);
        }
        etc...

}

I feel extremely dirty when I read my code.
My problem is the multiple if..else statement, because I have 12 differents fruits, and I have to do this statement a lot in my current project.

Is there a way to refactoring my DrawFruits method ?
I am thinking of a kind of factory pattern but I don’t really see how to do it.
Is my fruit class must take a Drawer as a Property ? Or maybe I can call a Drawer Factory method ?

This is a pattern that I find a lot in my current project, and I don’t find a solution that satisfies me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:02:03+00:00Added an answer on May 27, 2026 at 6:02 pm

    One way is to have a GetDrawer on your IFruit

    public interface IFruit 
    {
        BaseDrawer GetDrawer();
    }
    

    and a BaseDrawer interface

    public interface BaseDrawer
    {
        void Draw(IFruit fruit, Graphics graphics);
    }.
    
    public class AppleDrawer : BaseDrawer
    {
        public void Draw(IFruit apple, Graphics graphics) { }
    }
    
    public class BananaDrawer : BaseDrawer
    {
        public void Draw(IFruit banana, Graphics graphics) { }
    }
    

    Now your draw Fruits is simply

       public void DrawFruits(List<IFruit> fruits, Graphics graphics)
        {
            foreach (var fruit in fruits)
            {
                var drawer = fruit.GetDrawer();
                drawer.Draw(fruit, graphics);
            }
        }
    

    Sometimes you need a Drawer , Plotter and Printer so your IFruit might get too heavy like below

    public interface IFruit 
    {
        BaseDrawer GetDrawer();
        BasePrinter GetPrinter();
        BasePlotter GetPlotter();
    }
    

    Visitor pattern is a good solution for this. Basically you will have

     public interface iFruit
       {
          void Accept(FruitVisitor visitor);
       } 
    

    only one class for all possible drawing visits

    public class DrawVisitor : FruitVisitor 
       {
          public override void Visit(Apple apple)
          {
             //draw the apple
          }
    
          public override void Visit(Banana banana)
          { 
             // draw the banana
          }
       }
    

    Here you just have one DrawVisitor instead of AppleDrawer , BananaDrawer etc and all your draw code is neatly in one place. You might end up needing PlotterVisitor , PrinterVisiter etc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have: public class Fruit { public static List<String> Suppliers { get;
Basically I have a custom List class that contains different fruits. Assume that each
I have this class Class Fruits: Class fruits: Are they both same or different
I have two classes defined in different h files and each class has some
I have different urls that points to the same code www.url1.com www.url2.com I need
You can have different naming convention for class members, static objects, global objects, and
I have two functions that have different enough logic but pretty much the same
Suppose I have a bunch of fruit: class Fruit { ... }; class Apple
I have different XML-files in my 'src/main/recources' folder, and I'd like to read them
Normal overriding would work this way: class Fruit { public: string color(); }; string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.