E1D0L0N
E1D0L0N

Reputation: 31

C# Abstract Class Implementation

I am just starting to use abstract classes and would like to know if I am using them in the correct manner. In my example I have a Windows Form application where Form1 is the main UI for the app. Within Form1 I have a method which writes debug messages to a richtextbox. I currently have a bunch of different classes which all have the same method in them to write message back to the main UI richtextbox. However, I was wondering if it would make sense to make this functionality part of an abstract class, an just have all of my other class inherit this functionality as shown below?

using System;
using System.Windows.Forms;

namespace abstractTest
{
    /// <summary>
    /// Main User Interface
    /// </summary>
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void RTBWriteLine(string message)
        {
            /* Print (append) message to the debug richtextbox */
            this.richTextBox1.AppendText(message + Environment.NewLine);

            /* Select the last entered message */
            this.richTextBox1.Select(richTextBox1.Text.Length, 0);

            /* Scroll to selected message */
            this.richTextBox1.ScrollToCaret();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Class1 c1 = new Class1(this);
            Class2 c2 = new Class2(this);
        }
    }

    public abstract class absTest
    {
        protected Form1 MainUI;

        public virtual void MainRTBWriteLine(string msg)
        {
            if (MainUI != null)
            {
                MainUI.RTBWriteLine(msg);
            }
        }
    }

    public class Class1 : absTest
    {
        public Class1(Form callingForm)
        {
            MainUI = callingForm as Form1;
            MainRTBWriteLine("This is Class1");
        }
    }

    public class Class2 : absTest
    {
        public Class2(Form callingForm)
        {
            MainUI = callingForm as Form1;
            MainRTBWriteLine("This is Class2");
        }
    }
}

Edit: My main goal is that I would to make DTBWriteLine(among many other methods that update the main UI is some way) to be available to all of my classes, but only have the code in one location. Right now, I have the following method (among many others) duplicated in every class I've created:

public virtual void MainRTBWriteLine(string msg)
{
    if (MainUI != null)
    {
        MainUI.RTBWriteLine(msg);
    }
}

If putting this in an abstract class isn't the best approach, what is?

Upvotes: 2

Views: 9289

Answers (6)

Alexander Balte
Alexander Balte

Reputation: 900

Abstract classes are widely used to implement Template Method pattern. Check C# implementation samples.

I would add that in your example is not nessesary to use exactly abstract keyword. absTest may be not abstract without any harm, and it would be fully functional in case of absTest(Form callingForm) constructor implementation.

Upvotes: 0

Joanna Derks
Joanna Derks

Reputation: 4063

To me it seems that what you are after is a separate DebugLogger class whose only concern would be logging debug messages to the textbox.

Then your other classes would just delegate debug logging to this one class rather than inheriting this additional functionality that has not much to do with their primary function.

Upvotes: 1

faester
faester

Reputation: 15076

It makes sense and reflects the idea with abstract classes. In your examples it also provides you with a nice separation of concerns where your absTest implementations enables you to swap logging strategies at a single point if needed.

Upvotes: 0

Stuart Golodetz
Stuart Golodetz

Reputation: 20616

An abstract class is one option in this situation, yes. An alternative (particularly if you want to reuse the same functionality from elsewhere) would be to create a utility method in a separate class. Inheritance is one way of achieving code reuse, but it's not always the best choice - consider the options carefully.

Upvotes: 1

Oded
Oded

Reputation: 499002

Yes, this scenario is a perfect example for using an abstract class.

When you have shared functionality in an inheritance tree.

Upvotes: 3

CC Inc
CC Inc

Reputation: 5918

The point of an abstract class, according to the docs:

Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes

  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

Upvotes: 0

Related Questions