I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

How to create 2 generic class that will handle 2 different classes initialisation?

I have 3 types of operation :Add,Multiply and Division

Below are the classes to handle this 3 operation:

public class Add
{
   public int Value {get;set;}
   public Add(int value)
   {
      Value=value;
   }
}

This below class will be called by above main class:

class Source : Add
{
   public Source(int value1)
          :base(value1)
   {
   }
}

class Destination : Add
{
    public Destination(int value2)
          :base(value2)
   {
   }
}

I am calling above class like this:

Add addObj1 = new Source(10);
Add addObj2 = new Destination(20);
int c=addObj1.Value + addObj2.Value;

Now i have another class like below:

public class Multiply
{
   public int Value {get;set;}
   public Multiply(int value)
   {
       Value=value;
   }
}

class Source1 : Multiply
{
   public Source(int value1)
          :base(value)
   {
   }
}

class Destination1 : Multiply
{
    public Destination1 (int value2)
          :base(int value2)
   {
   }
}

Now when i am trying to call this class like this:

Multiply multiplyObj1 = new Source(10); //This is always referring to Source of Add class
 Multiply multiplyObj2 = new Destination(5); //This is always referring to Destination of Add class

Now when i rename Source and Destination with Source1 and Destination1 and call like this then it is working:

Multiply multiplyObj1 = new Source1(10); //Working
Multiply multiplyObj2 = new Destination1(5); // Working
int c= multipleObj1.Value * multiplyObj2.Value;

Note:Right now i have created 4 class in which Source and Destination will handle Add class and Source1 and destination1 will handle Multiply

I am talking about how to reuse 2 class for both Add and Multiply class or for other class too(For Division etc..).

Now if i want to perform Division then i have again create 2 more class to handle division.

So here i dont want to duplicate Source and Destination i.e instead of creating 4 class i.e Source,Destination,Source1 and Destination1 is it possible to create only 2 generic class i.e Source and Destination that will perfectly handle both Add and Multiple???

Upvotes: 0

Views: 131

Answers (2)

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64923

Let's summarize your issue now that you've already edited your question and you've added meaningful names to your classes.

You need to...

  • Perform additions.
  • Perform multiplications.

And you want to define base class to rule them all.

First of all, there's an issue here. You can't say that an addition source and a multiplication source are additions and multiplications altogether.

It's like saying that a cat is both an animal and a mineral. No, it's an animal.

This situation translated to object-oriented programming world means multiple inheritance:

// WRONG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! C# doesn't support multiple inheritance
public class Source : Addition, Multiplication 
{
}

BTW, a source of additions couldn't also a source of multiplication. It's not a technical limitation, but a semantic limitation. It's just not right. Your design is simply wrong.

So, what? Well, I believe that you need to refactor your idea and code and you'll get what you want.

For example, if any math operation source has the same form, I would define a class to value sources:

public class Source
{
    public Source(int value) 
    {
         Value = value;
    }

    public int Value { get; }
}

And then I would define Multiply and Add classes based on an Operation class:

public abstract class Operation
{
      public Operation(Source sourceA, Source sourceB)
      {
           SourceA = sourceA;
           SourceB = sourceB;
      }

      public Source SourceA { get; }
      public Source SourceB { get; }

      public abstract int Do();
}

public class Add : Operation
{
      public Operation(Source sourceA, Source sourceB) : base(sourceA, sourceB)
      {
      }

      public override int Do() => SourceA.Value + SourceB.Value;
}

public class Multiply : Operation
{
      public Operation(Source sourceA, Source sourceB) : base(sourceA, sourceB)
      {
      }

      public override int Do() => SourceA.Value * SourceB.Value;
}

Now you've a single source for your mathematical operations and just a base class to summarize there what's common to any operation, and two operations already implemented.

Note that you can take advantage of polymorphism since any operation inherits Operation:

Source sourceA = new Source(203);
Source sourceB = new Source(134);

Operation op1 = new Multiply(sourceA, sourceB);
Operation op2 = new Add(sourceA, sourceB);

int result1 = op1.Do();
int result2 = op2.Do();

I believe that this could be a possible and acceptable design.

Upvotes: 3

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64923

Probably your actual use case doesn't love inheritance and you need to go with composition. Please double-check this other answer I did some days ago: Define a variable of one class from another class

Let's use a real-world case: a Car.

A Car has...

  • Engine
  • Radio
  • Wheel

It wouldn't be correct to say that a concrete Car should inherit Engine, Radio and Wheel, because a Car has them, but they're not cars, but parts of the whole Car.

When you arrive to this conclusion is because you need composition:

public class Car
{
     public Engine Engine { get; set; }
     public Wheel Wheel { get; set; }
     public Radio Radio { get; set; }
}

That way, a concrete car, let's say a Ferrari, can inherit everything a Car has by default and it can also add more components:

public class FerrariTestarossa : Car
{
     // More properties and methods that make a FerrariTestarossa unique
     // from a generic car
}

Probably you can extrapolate this to your own use case.

Upvotes: 2

Related Questions