Extend Factory Design Pattern to implement Base and Derive class

Introduction

In this article we will extend Factory design pattern and use base and derive classes. We will use the object of Base class to return the instance of the derived class.

Background

You should have basic understanding of Factory design pattern.

Using the code

What is Factory Design Pattern?

Factory design pattern depends on Interface and Class to return the object of the Concrete class to the client.

Now let me know simplify this definition

Concrete Class :  A class in the project which has the actual business logic written in its method.

Client Class : This class needs to invoke the method of the Concrete class so that application can perform business logic.

Interface : As its name it is just an interface. Concrete classes have the responsibility to implement this Interface.

Factory Class : This is a static class which connects Client class and Concrete class. Client class calls the method of the Factory class and Factory class returns the instance of the Concrete class to the Client class.

In this pattern Client class never creates the instance of the Concrete class rather it depends on the factory class to do the same. This way we achieve loosely coupled architecture where Client code doesn't need to be changed / compiled if you want to refer to another Concrete class.

Below is a small code snippet of Factory design pattern

 


Public Interface IFace
{
 public string GetString();
}

Class Concrete : IFace 
{
  public string GetString()
    {
    return "Concrete Class";
    }
}

static class Factory()
{
  public static IFace GetConcreteClass()
     {
       return new Concrete();
     }
}

static void main(string[] args) //our client class
{
   IFace iface = Factory.GetConcreteClass();
   Console.writeline(iface.GetString());
}

I hope with the above definition and code snipper you will have got an understanding of Factory Design Patter.

In below section we will extend the Factory Design Pattern and use Base Concrete Class and Derived Concrete classes

Extending Factory Design Pattern

We will create a Concrete class and that will act as a base class. Then Concrete class will implement the Interface. We will also create few derived classes which will inherit the Base Concrete class and override the implementation of the method which was defined in the Interface. Confused!!!!. Below code snippet will give you an understanding

 public Interface IFace
   {
     string GetString();
   }

Class BaseConcrete : IFace
{
 public virtual string GetString()
  {
    return "BaseConcrete class";
  } 
}

Class DeriveConcrete : BaseConcrete
{
   public override string GetString()
     {
       return "DeriveConcrete Class";
     }
}

Class Derive1Concrete : BaseConcrete
{
   public override string GetString()
     {
         return "Derive1Concrete Class";
     }
}

Class Derive2Concrete : BaseConcrete
{
 public override string GetString()
 {
 return "Derive2Concrete Class";
 }
}

static class Factory
{
  public IFace GetBase()
   {
       BaseConcrete baseConcrete = null;
       return baseConcrete; 
   }
  public IFace GetDerive()
   {
       return new DeriveConcrete();
    }
  public IFace GetDerive1()
   {
      return new Derive1Concrete();
    }
}

class program
static void main (string[] args)
{
  IFace iface = Factory.GetBase();
  Console.writeline(iface.GetString());  
}
  

In the above code we created a BaseConcrete class which implemented IFace interface. Then Derived classes which inherited this BaseConcrete class and override the method. Now in our Factory class we are returning the instance of the DerivedClass based on the object of the BaseConcrete class.

But Why?

So why to do all these complex things? 

To understanfd this lets consider a situation where we have bunch of Concrete classes and they all are implementing the Interface. Now if we add a new method in the Interface all Concrete classes will have to implement that method. If some Concrete classes do not want to implement that new method then either they will implement the "throw new NotImplementedException" or we create more interfaces and implement them only to those classes which actually want to implement them.

In above scenario we could do it in a straighforward way. We could either create new method in any derived class or we could include that method in interface and implement it in the base class and ask our Factory class to return the instance of the Base class.

Above code is just a skeleton of how we could do it. I believe this implementation is best fit where we have bunch of Concrete classes which are related to each other. Rather than exploring other Design Patterns we could just extend Factory Design Pattern.

This article is open to suggestion. My main intent is to extend the Factory Design Pattern. Please contribute in the comment section.

One thought on “Extend Factory Design Pattern to implement Base and Derive class

Leave a Reply

Your email address will not be published. Required fields are marked *