26.8.11

Reviewing Architecting Applications for the Enterprise

Microsoft® .NET: Architecting Applications for the Enterprise by Dino Esposito
My rating: 4 of 5

Interesting and easy-reading book on software architecture using Microsoft technologies. It also provides some helpful best practices in general regardless of the technology. It covers a wide range of many architecture aspects; from presentation patterns to business and data access layers. However, I felt that the book doesn't have a main steam. It gives you tips on every thing but there is nothing that it talks a lot about. All in all, the book is very beneficial and is a must to read for any senior microsoft software developer.

16.8.11

My First Chrome Theme

Snapshot of the theme in Chrome Web Store

Today, I've published a Google Chrome theme. Creating a theme wasn't difficult at all. I used a website that provides the capability of creating Chrome themes online. You can check the website here: Create Chrome Theme Online

My first Chrome theme features a photo that I took in the Jordanian city of Umm Qais. The city resides in the north-west of the country and I think it's not getting  enough attention by tourists and tourism associations.

You can download the theme from this link:
https://chrome.google.com/webstore/detail/gobjieemlhcnpibeobjfodhankgfhoco?hl=en-US

Please try it and tell me what you think.

24.5.11

Abstract vs. Virtual in C#

There are several keywords that can be used for inheritance in object-oriented programming languages. In C#, you can specify a method as abstract or virtual.

In the concrete class, you must override any method marked as abstract. You can override
virtual
methods. In addition, you can provide new implementation for normal methods that are neither
abstract nor virtual. In this case, it's recommended to use the new keyword.

What's the difference between abstract and virtual methods? How can I use them to serve my design? In this article, I'm trying to answer these questions.

Of course the abstract method doesn't have implementation. It just defines the method. But is there any other differences? Let's take an example of two classes. An abstract class and another one that inherits from it.
Class diagram of an abstract class and a concrete one


// the abstract class
public abstract class BaseClass
{
    public abstract string GetAbstractValue();

    public virtual string GetVirtualValue()
    {
        return "Base GetVirtualValue";
    }

    public string GetValue()
    {
        return "Base GetValue";
    }
}


// the concrete class
public class ConcreteClass : BaseClass
{
    public override string GetAbstractValue()
    {
        return "Concrete GetAbstractValue";
    }

    public override string GetVirtualValue()
    {
        return "Concrete GetVirtualValue";
    }

    public string GetValue()
    {
        return "Concrete GetValue";
    }
}

Let's make a console application that uses the above classes. We want to make 2 instances of the concrete class. The first one should be of type
BaseClass and the other one of type ConcreteClass.

The client code should look like this:

class Program
{
    static void Main(string[] args)
    {
        BaseClass instance1 = new ConcreteClass();
            
        Console.WriteLine(instance1.GetAbstractValue());
        Console.WriteLine(instance1.GetVirtualValue());
        Console.WriteLine(instance1.GetValue());


        ConcreteClass instance2 = new ConcreteClass();

        Console.WriteLine(instance2.GetAbstractValue());
        Console.WriteLine(instance2.GetVirtualValue());
        Console.WriteLine(instance2.GetValue());


        Console.Read();
    }
}

Let's now see and compare the results.

The results of running the application

What do you conclude from the results?

Let's make another experiment before conclusion. Let's remove the following 2 methods from the
ConcreteClass

  • GetVirtualValue
  • GetValue

Try to run the application and notice the results.

The results after removing the 2 methods

From the previous results we can conclude the following:


Abstract Virtual No Keyword
Can have implementation?  No Yes Yes
Can override? Must Can but not a must You can declare a new method with the same name
Which keyword to use to provide new implementation in the concrete class? override override No keyword needed
If an object is created of the base class type, which method will be executed? Concrete implementation The parent implementation will be called only if no implementation is provided in the concrete class Parent implementation
If an object is created of the concrete class type, which method will be executed? Concrete implementation Concrete implementation The parent implementation will be called only if no implementation is provided in the concrete class

Popular Posts