Partager via


Creational - Factory Pattern

In continuation to my previous post, I am presenting you a sample code that implements Factory Pattern.

The overview:
 - We are using the Person, Gift and Letter classes as we did in the Abstract Factory Pattern implementation. This is just to show how you can use the same scenario against different patterns.
 - The abstract Person class is the Creator. Gift and Letter are Products.
 - The concrete creator classes are Employee and Colleague. The concrete Product classes are: Email, Postal for Letter; Flowers, Perfume for Gift.
 - The Creator has 2 abstract functions that create Products. the Concrete Classes override them to create different Products.
 - You can create an Array of Concrete objects for the Creator class.

Takeaways:
 - This pattern is particularly helpful in designing frameworks.
 - The Creator relies on its subclasses to define the factory method to return the appropriate ConcreteProduct.
 - There is no Client class.

The Creator Class:

    1: public abstract class Person
    2:     {
    3:         public abstract Gift CreateGift();
    4:         public abstract Letter CreateLetter();
    5:     }

The Concrete Classes:

    1: public class Employee: Person
    2:     {
    3:         public override Gift CreateGift()
    4:         {
    5:             return new Flowers();
    6:         }
    7:         public override Letter CreateLetter()
    8:         {
    9:             return new Email();
   10:         }
   11:     }
   12:     
   13:     public class Colleague: Person
   14:     {
   15:         public override Gift CreateGift()
   16:         {
   17:             //throw new NotImplementedException();
   18:             return new Perfume();
   19:         }
   20:  
   21:         public override Letter CreateLetter()
   22:         {
   23:             //throw new NotImplementedException();
   24:             return new Postal();
   25:         }
   26:     }

The Products - Letter and Gift; Concrete Products Email, Postal, Flowers, and Perfume

    1: public abstract class Letter
    2:     {
    3:     }
    4:     
    5:     public class Email: Letter
    6:     {
    7:         public Email()
    8:         {
    9:             Console.WriteLine("Email Created");
   10:         }
   11:     }
   12:     
   13:     public class Postal: Letter
   14:     {
   15:         public Postal()
   16:         {
   17:             Console.WriteLine("Postal Created");
   18:         }
   19:     }
   20:     
   21:     public abstract class Gift
   22:     {        
   23:     }
   24:     
   25:     public class Flowers: Gift
   26:     {
   27:         public Flowers()
   28:         {
   29:             Console.WriteLine("Flowers Created");
   30:         }
   31:     }
   32:     
   33:     public class Perfume: Gift
   34:     {
   35:         public Perfume()
   36:         {
   37:             Console.WriteLine("Perfume Created");
   38:         }
   39:     }

The usage from within application:

    1: static void Main(string[] args)
    2:         {
    3:             Person[] people = new Person[2];
    4:             people[0] = new Employee();
    5:             people[1] = new Colleague();
    6:             
    7:             foreach(Person person in people)
    8:             {
    9:                 Console.WriteLine(person.CreateGift());
   10:                 person.CreateLetter();
   11:             }
   12:             Console.Read();
   13:         }

Hope it helps. Do write to me in case of any question/suggestions.

Comments