Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine which inheriting class is using an abstract class's methods

In my console application have an abstract Factory class "Listener" which contains code for listening and accepting connections, and spawning client classes. This class is inherited by two more classes (WorldListener, and MasterListener) that contain more protocol specific overrides and functions.

I also have a helper class (ConsoleWrapper) which encapsulates and extends System.Console, containing methods for writing to console info on what is happening to instances of the WorldListener and MasterListener.

I need a way to determine in the abstract ListenerClass which Inheriting class is calling its methods.

Any help with this problem would be greatly appreciated! I am stumped :X

Simplified example of what I am trying to do.

abstract class Listener
{
   public void DoSomething()
   {
      if(inheriting class == WorldListener)
      ConsoleWrapper.WorldWrite("Did something!");
      if(inheriting class == MasterListener)
      ConsoleWrapper.MasterWrite("Did something!");
   }
}

public static ConsoleWrapper
{
   public void WorldWrite(string input)
   {
       System.Console.WriteLine("[World] {0}", input);
   }
}

public class WorldListener : Listener
{
   public void DoSomethingSpecific()
   {
       ConsoleWrapper.WorldWrite("I did something specific!");
   }
}

public void Main()
{
   new WorldListener();
   new MasterListener();
}

Expected output

[World] Did something!
[World] I did something specific!
[Master] Did something!
[World] I did something specific!

like image 448
Kin Avatar asked Feb 27 '23 04:02

Kin


1 Answers

If you know each of the types you want to compare against, then use the is operator:

if (this is WorldListener)
{
 // ...
}
else if (this is MasterListener)
{
 // ...
}

Alternatively, you could use GetType if you want a little more flexibility:

var type = GetType();
// Do some logic on the type to determine what to do next.

You should be careful with this approach, however; it's generally indicative of bad design that you need to explicitly check for types (as these lovely people insist). Instead, it's almost always more appropriate to use polymorphism to delegate the desired behaviour to the base class (using a virtual or abstract method in the base class) – this is, after all, what it's designed for!

You might apply polymorphism something like this:

static class Program
{
    static void Main(string[] args)
    {
        Listener listener = new WorldListener();
        listener.DoSomething();
    }
}

abstract class Listener
{
    public void DoSomething()
    {
        var output = Decorate("Did something!");
        ConsoleWrapper.WriteLine(output);
    }

    protected abstract string Decorate(string input);
}

class WorldListener : Listener
{
    protected override string Decorate(string input)
    {
        return string.Format("[World] {0}", input);
    }
}

class MasterListener : Listener
{
    protected override string Decorate(string input)
    {
        return string.Format("[Master] {0}", input);
    }
}

This will produce the output [World] Did something!. The advantage of this approach is that if you ever want to add another type of listener, it's simply a matter of defining a new class for it with the appropriate Decorate method; there's no need to modify Listener itself.

like image 75
Will Vousden Avatar answered Apr 25 '23 11:04

Will Vousden