How to determine which inheriting class uses abstract class methods

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

I also have a helper class (ConsoleWrapper) that encapsulates and extends System.Console, which contains methods for writing to the console information about what happens with WorldListener and MasterListener instances.

I need a way to define in an abstract ListenerClass that the Inheriting class calls its methods.

Any help with this issue would be greatly appreciated! I'm at a standstill: X

A simplified example of what I'm 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 Result

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

+4
source share
3 answers

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

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

Alternatively, you can use GetType if you want a little more flexible:

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

However, you must be careful with this approach; it usually indicates a poor design that you need to explicitly check types (since these nice people insist). Instead, it is almost always more appropriate to use polymorphism to delegate the desired behavior to the base class (using a virtual or abstract method in the base class) - this is, after all, what it is intended for!

You can apply polymorphism 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 exit [World] Did something! . The advantage of this approach is that if you want to add another type of listener, it is just a matter of defining a new class for it using the corresponding Decorate method; no need to modify the Listener itself.

+4
source

Hmm .. Well, in your simplified example, you do not call DoSomething () and DoSomethingSpecific (), and there is no implementation for MasterListener .. Also, if I understand correctly, in your expected output, your masterListener.DoSomethingSpecific () starts ConsoleWrapper.WorldWrite. . Probably you mean masterWrite?

Anyway .. Here is a working example that does what you want (at least the way I understood your request: P)

Printed Result:

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

The code:

  void Main() { var wl = new WorldListener(); wl.DoSomething(); wl.DoSpecific(); var ml = new MasterListener(); ml.DoSomething(); ml.DoSpecific(); } public abstract class Listener { public abstract string Category { get; } public void DoSomething() { ConsoleWrapper.Write(Category, "Did something"); } } public static class ConsoleWrapper { public static void Write(string category, string input) { Console.WriteLine("[{0}] {1}", category, input); } } public class WorldListener : Listener { public override string Category { get { return "World"; } } public void DoSpecific() { ConsoleWrapper.Write(Category, "I did sth specific!"); } } public class MasterListener : Listener { public override string Category { get { return "Master"; } } public void DoSpecific() { ConsoleWrapper.Write(Category, "I did sth specific!"); } } 
+2
source

you can use

 if (this is WorldListener) 

instead of your pseudo code

 if (inheriting class == WorldListener) 

However, this is a bad smell design. You should strongly consider an alternative solution, for example. writing to the console shell in a virtual method instead of adding this strong connection between the base class and its subclasses.

+2
source

Source: https://habr.com/ru/post/1307258/


All Articles