简体   繁体   中英

Share a method between 2 classes C#

I have simplified what I have into this:

class A : SomeClassICantAccess
{
    IVIClass ivi = new IVIClass();

    public A()
    {
        string address = "1111";
        ivi.Initialize(address);
    }

    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

class B : SomeClassICantAccess
{
    IVIClass ivi = new IVIClass();

    public B()
    {
        string address = "2222";
        ivi.Initialize(address);
    }

    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

class Main()
{
    //I want my main to keep this format
    A a = new A();
    B b = new B();

    a.SendCommand("commands");
    b.SendCommand("commands");
}

As you can see, class A and B have identical SendCommand() method, but since their ivi s are initialized with different address, the commands are sent to different instruments.

It seems wrong to have a same method copied-pasted in two different classes. But I really want my Main() to look like how it is right now - so that it will be clear whether the SendCommand() is refered to instrument A or instrument B.

How can I merge them?

If this is your actual scenario, there is no need for two class you can deal with A only:

Class definition for A :

class A()
{
    IVIClass ivi = new IVIClass();
    public A(string address)
    {        
        ivi.Initialize(address);
    }
    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

How To Use:

class Main()
{   
    A a = new A("1111");//initialize  ivi with "1111"
    A b = new A("2222");//initialize  ivi with "2222"
    a.SendCommand("commands");
    b.SendCommand("commands");
}

You need a base interface, let's call it ICommandSender and an extension method that takes an instance of ICommandSender as it's source.

// Base interface
public interface ICommandSender
{
    // Shared definition
    IVIClass ivi;
}

public class A : SomeOtherClass, ICommandSender
{
    // A code here
}

public class B : SomeOtherClass, ICommandSender
{
    // B code here
}

// Extension Methods are held in a static class (think Façade Pattern)
public static class ExtMethods
{
    // Accept the source and cmd 
    // The this keyword indicates the target type.
    public static void SendCommand(this ICommandSender source, string cmd)
    {
        source.ivi.Send(cmd);
    }
}

// Main doesn't change at all.
class Main()
{
    //I want my main to keep this format
    A a = new A();
    B b = new B();

    a.SendCommand("commands");
    b.SendCommand("commands");
}

Introduce a base class?

class BaseClass
{
    IVIClass ivi = new IVIClass();
    public void SendCommand(string cmd)
    {
        ivi.Send(cmd);
    }
}

class A : BaseClass
{   
    public A()
    {
        string address = "1111";
        ivi.Initialize(address);
    }
}

class B : BaseClass
{   
    public B()
    {
        string address = "2222";
        ivi.Initialize(address);
    }
}
abstract class AbstractClass{

   protected String socket;

   public AbstractClass(String socket){
      this.socket = socket;
   }

   public virtual void SendCommand(String cmd){
     //dostuff
   }

}

class A : AbstractClass{
  public A(String socket):base(socket){
  }

  //you can ovverride the virtual method if need be. 
}

Same for Class B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM