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

  • SEARCH
  • Home
  • 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 9026441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:31:12+00:00 2026-06-16T06:31:12+00:00

Consider the following case – I have a class (Fruits), which has some methods

  • 0

Consider the following case –

I have a class (Fruits), which has some methods such as PackTheFruit(), CutTheFruit(), CleanTheFruit(). This class can not be modified.

I also have a set of classes which would contain an object of fruit type. In some of the classes I want to have access to PackTheFruit() method but not in all.

I have thought creating two interfaces, which would be implemented by Fruits class. One would expose PackTheFruit() and one would expose the other methods only and each class would have an object of the these interface type instead depending on if they need to have access to that method or not.

The problem in this solution is that, when ever I add another method to the Fruits class I will have to update the interfaces. That would be a bad design in my eyes at least.

  • 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-06-16T06:31:13+00:00Added an answer on June 16, 2026 at 6:31 am

    I depends what these operations are doing. Let’s assume that packing consists of adding fruits to a basket up to a maximum weight, then you will need to know the weight of a fruit in order to make it packable. If you want to pack different kinds of fruits it would be better to have a separate packer class. It feels strange to have fruits packing themselves.

    public interface IPackable
    {
        public float Weight { get; set; }
    }
    
    public interface IPacker
    {
        // Returns a list of packages represented by lists of fruits.
        List<List<Fruit>> GetPackages(IEnumerable<Fruit> fruits, float maxPackageWeight);
    }
    
    public class Packer : IPacker
    {
        public List<List<Fruit>> GetPackages(IEnumerable<Fruit> fruits,
                                             float maxPackageWeight)
        {
            var currentPackage = new List<Fruit>();
            var packages = new List<List<Fruit>>(currentPackage);
            float currentWeight = 0.0f;
            foreach (Fruit fruit in fruits) {
                var packable = fruit as IPackable;
                if (packable != null && packable.Weight <= maxPackageWeight) {
                    if (currentWeight + packable.Weight <= maxPackageWeight) {
                        currentPackage.Add(fruit);
                        currentWeight += packable.Weight;
                    } else {
                        var currentPackage = new List<Fruit>(fruit);
                        packages.Add(currentPackage);
                        currentWeight = packable.Weight;
                    }
                }
            }
            return packages;
        }
    }
    

    If you add new interfaces for new functionalities, you will not have to change existing interfaces. This is called Interface segregation principle (ISP) and is one of the five SOLID principles of Object-Oriented Design.

    Note: Having an IPacker interface allows you to implement different kinds of packers. An implementation might mix different kinds of fruits within a package, while another one might sort the fruits.

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

Sidebar

Related Questions

Consider following case: I have base class inherited by another two. For instance we
I have a relatively simple use case which is failing. Consider the following code:
Consider the following case: public class A { public A() { b = new
Please consider the following simple use case: public class Foo { public virtual int
Consider the following test case: public class Main { static int a = 0;
Consider the following test case, in which a floated and an inline element are
Consider the following case: I have a single Excel workbook with 4 sheets in
Having the SOLID principles and testability in mind, consider the following case: You have
Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback

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.