C# Abstract Factory Pattern

C# Abstract Factory Pattern tutorial

Advertisements

Link To source Code

In previous post i introduced you to the concept of Factory Method. Abstract factory is very similar, but this time we will encapsulate all classes, by cremating interfaces for  Product and Factory. This pattern is also called as Factory of factories, because this patterns acts a superFactory which creates other factories. Abstract factory pattern in useful when the client needs to create objects which are somehow related, without specifying the concrete class.  With this pattern objects can interact with each other through common interfaces.

As usual I am using example from game development. In my example I’ll create Abstract Factory that will spawn enemies and give them specific weapon and armor.

UML diagram

l9

 

Factory

First thing to crate is the interface for Factory, that will be used to specify the equipment of each enemy.


 interface IEnemyFactory
    {
       IWeapon GetWeapon();
       IArmor GetArmor();
    }

Concrete classes will be named as the type of enemy that will be created. In my Example i have two enemies Mage and Warrior.


class Mage : IEnemyFactory
{
    public IWeapon GetWeapon()
    {
        return new Wand();
    }

    public IArmor GetArmor()
    {
        return new Cloak();
    }
}

class Warrior : IEnemyFactory
{
    public IWeapon GetWeapon()
    {
        return new Sword();
    }

    public IArmor GetArmor()
    {
        return new BodyArmor();
    }
}

Product

In next step we will create interface for items that will be given to the enemies. Both types share the same types of equipment, weapon and armor.


interface IWeapon
 {
     string Item();
 }
interface IArmor
 {
     string Item();
 }

In Concrete classes methods will return string values that will be printed to the console.


class Sword : IWeapon
 {
     public string Item()
     {
         return "Iron Sword";
     }
 }

class Wand : IWeapon
 {
     public string Item()
     {
          return "Magic Wand";
     }
 }
 class BodyArmor : IArmor
 {
      public string Item()
      {
          return "Iron Body Armor";
      }
 }

class Cloak : IArmor
 {
     public string Item()
     {
          return "Magic Cloak";
     }
 }

Client

Client is the last part is to create. Here we will create instance of the factory and spawn our enemies.


class Client
{

IEnemyFactory factory=null;

//SpawnEnemy method will decide what type of enemy will be created

//Based on string input

public void SpawnEnemy(string enemy)
 {
   if (enemy == "Warrior")
   {
     factory = new Warrior();
     Console.WriteLine("New Warior equipment:");
     Console.WriteLine(factory.GetWeapon().Item());
     Console.WriteLine(factory.GetArmor().Item());
     Console.WriteLine("_____________________");
   }
   else if (enemy == "Mage")
   {
     factory = new Mage();
     Console.WriteLine("New Mage equipment: ");
     Console.WriteLine(factory.GetWeapon().Item());
     Console.WriteLine(factory.GetArmor().Item());
     Console.WriteLine("_____________________");
   }
   else
   {
      Console.WriteLine("Wrong type");
   }
 }

 //Main method
 static void Main(string[] args)
 {
     Client client = new Client();
     client.SpawnEnemy("Mage");
     client.SpawnEnemy("Warrior");
     Console.Read();
 }
}

To conclude, Abstract Factory  provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is very useful but in large projects it may increase the complexity of the code. I hope you learned something new today. In next article You will learn how to implement another pattern. Stay Awesome!

 

Abstraction

This is the fourth principle of  object oriented programming. Abstraction is a concept related to polymorphism. Abstraction in programming means declaring the methods that will be defined in derived class. The idea of abstraction works well with large and complex projects,  where class contains multiple methods that performs different tasks.

Another approach to this concept is saying that Abstraction works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level. The process of abstraction is the process to build a software model that presents solution for some task\s in real world. Abstraction is  a process of building concept model of more complex system where we dont need to know how low level tasks are executed

Abstract Base Class

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events.  Abstract classes are created to be a base class of other classes. User can not instantiate object and call abstract class method. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

Lets take a look at example:

 abstract class car
    {
        public abstract void move();
    }

Here we create abstract class with one abstract method. This method must be public so it’s available for other classes.

 abstract class car
    {
        public abstract void move();
    }
    class audi : car
    {
        public override void move()
        { 
             Console.WriteLine("moving"); 
        }
    }

Method in child class must overwrite the abstract method and contain the body of the function. Noticed that abstract class contains only declaration of the method.

class audi : car
        {
            public override void move() { Console.WriteLine("moving"); }
        }
        static void Main(string[] args)
        {
            audi r8 = new audi();
            car myNewCar = r8;
            myNewCar.move();
            r8.move();
        }
    }

We can now call our method from the instance of the child class. We can also create Object of type car, by coping the instance of subclass. Notice that we don’t instantiate object of abstract class.

This was the last topic of main principles of object oriented programming in C#. In the next article I’ll cover more complex topics. Use this knowledge to write better and more cleaner code. Posts about other principles of oop you can find in C# .NET category. Stay Awesome!