Decoupling

Someone asked me what can he do with interfaces and abstract classes. Plenty of fun there as we shall see now. I’ll use C++ and C# in this example, and I’ll present this on simple math operations. Interfaces and abstract classes are different in more than one way. For example, interfaces can’t contain any properties, or implemented methods, and they can’t contain access modifiers, while abstract classes can. One thing in common between interface and abstract class is that they can’t be instantiated, only derived and implemented. Interface defined in C# because it’s the easiest:

    interface IintOperation
    {
        int doIt(int a, int b);
    }

So what can you do with and interface? Consider this example:

    class Plus : IintOperation
    {
        public int doIt(int a, int b)
        {
            return a + b;
        }
    }
    class Minus : IintOperation
    {
        public int doIt(int a, int b)
        {
            return a - b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IintOperation p = new Plus();
            IintOperation m = new Minus();
            Console.WriteLine((p.doIt(3, 2).ToString()));
            Console.WriteLine((m.doIt(3, 2).ToString()));
        }
    }

I’ve successfully and completely used polymorphism. Never need to worry what kind of properties class contains and how to handle them in different sections of your project. Call on the behavior of the object and let it work itself on it’s own. That’s the whole point of OOP. Now, if you want your class to have some properties you can do it like this:

    class Plus : IintOperation
    {
        public int a, b, result;
        public Plus(int a, int b)
        {
            this.a = a;
            this.b = b;
            this.result = 0;
        }
        public int doIt()
        {
            result = a + b;
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IintOperation p = new Plus(3, 5);
            // returns 5
            p.doIt();
        }
    }

Regardless of the public access modifier on properties a and b of the corresponding class, after initialization those properties are not visible. You can only see and call public method doIt(). This is called decoupling and it’s pretty powerful. It’s used when we want to minimize the dependency between multiple building block of an application. If the changes occur on one part it should not affect the other. Here’s an example of it’s usage in event processing. This will be a complicated example for newbies, but when you’re working with multiple classes it makes sense:

    interface IintOperation
    {
        int doIt();
    }
    class Plus : IintOperation
    {
        public delegate void op(IintOperation o);
        public op handle;
        int a, b, result;
        public Plus(int a, int b)
        {
            this.a = a;
            this.b = b;
            this.handle += handler;
        }
        public void handler(IintOperation o)
        {
            result = o.doIt();
        }
        public int add()
        {
            handle(this);
            return result;
        }
        public int doIt()
        {
            result = a + b;
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            plus p = new Plus(3, 2);
            Console.WriteLine((p.add().ToString()));
        }
    }
}

I’ve used delegates here instead of standard events because you can define the parameters as you like. In this case handler accepts one object type operation and we automatically have access to doIt() method. You can notice that a and b are not accessible and we can use this method to handle multiple objects with various implementations if they are of the same subtype.
In C++ things differ a little. It does not support abstract or interface keywords, but you can still define abstract class. In C++, a class is considered abstract if it contains one or more pure virtual functions. Like the following example:

class  IntOperation
{
	public:
		virtual int doIt() = 0;
};

And after that you can inherit that class and implement doIt() method like this:

class Plus: public IntOperation
{
	private:
		int a, b;
	public:
		Plus(int a, int b)
		{
			this->a = a;
			this->b = b;
		}
		int doIt()
		{
			return a + b;
		}
};

Now we can use that class to create object of a subtype IntOperation:

#include "operation.h"
#include <iostream>
int main(void)
{
	IntOperation *p = new *lus(3, 2);
	std::cout << p->doIt() << std::endl;
	return 0;
}

Unlike C++, C# supports abstract keyword and you can define abstract class like this:

class IntOperation
{
    abstract int doIt();
}

And the usage is similar.
Please note the differences between abstract classes and interfaces as I mentioned them on the top of this post and use them wisely.
Thank you for reading.

About palethorn

Geeky GNU/Linux user, computer science student, anime addict, loves programming and optimization
This entry was posted in CSharp, OOP, Posts in English, Programming and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a comment