Design Patterns - Abstract Factory Pattern

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern_AbstractFactoryApp
{
   class Program
   {
      static void Main(string[] args)
      {
         /* ABSTRACT FACTORY PATTERN:
          * The abstract factory pattern provides a way to 
          * encapsulate a group of individual factories 
          * that have a common theme without 
          * specifying their concrete classes.
          * Use of this pattern makes it possible to 
          * interchange concrete implementations 
          * without changing the code 
          * that uses them, even at runtime. 
          * However, employment of this pattern, 
          * as with similar design patterns, 
          * may result in unnecessary complexity 
          * and extra work in the initial writing of code
          */

         var samsungSmartPhone = new GenericFactory<SamsungSmartPhone>();
         SmartPhone smartPhone1 = new SamsungSmartPhone();
         samsungSmartPhone.CreateInstance().TestSmartPhone(smartPhone1);

         var appleSmartPhone = new GenericFactory<AppleSmartPhone>();
         SmartPhone smartPhone2 = new SamsungSmartPhone();
         appleSmartPhone.CreateInstance().TestSmartPhone(smartPhone2);

         Console.ReadKey();
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern_AbstractFactoryApp
{
   public class GenericFactory<T>
      where T : new()
   {
      public T CreateInstance()
      {
         return new T();
      }
   }
}


**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern_AbstractFactoryApp
{
   public abstract class PhoneFactory
   {
      public abstract SmartPhone CreateSmartPhone();
      public abstract DummyPhone CreateDummyPhone();
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   public abstract class SmartPhone
   {
      public abstract void TestSmartPhone(SmartPhone phone);
   }
}

**************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   public abstract class DummyPhone
   {
      public abstract void TestDummyPhone(DummyPhone phone);
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern_AbstractFactoryApp
{
   public class AppleFactory : PhoneFactory
   {
      public override SmartPhone CreateSmartPhone()
      {
         return new AppleSmartPhone();
      }

      public override DummyPhone CreateDummyPhone()
      {
         return new AppleDummyPhone();
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   class AppleSmartPhone : SmartPhone
   {
      public override void TestSmartPhone(SmartPhone phone)
      {
         Console.WriteLine("Apple Smart: " + phone.GetType().Name);
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   class AppleDummyPhone : DummyPhone
   {
      public override void TestDummyPhone(DummyPhone phone)
      {
         Console.WriteLine("Apple Dummy: " + phone.GetType().Name);
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPattern_AbstractFactoryApp
{
   class SamsungFactory:PhoneFactory
   {
      public override SmartPhone CreateSmartPhone()
      {
         return new SamsungSmartPhone();
      }

      public override DummyPhone CreateDummyPhone()
      {
         return new SamsungDummyPhone();
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   class SamsungSmartPhone : SmartPhone
   {
      public override void TestSmartPhone(SmartPhone phone)
      {
         Console.WriteLine("Samsung Smart: " + phone.GetType().Name);
      }
   }
}

**************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern_AbstractFactoryApp
{
   class SamsungDummyPhone:DummyPhone
   {
      public override void TestDummyPhone(DummyPhone phone)
      {
         Console.WriteLine("Samsung Dummy: " + phone.GetType().Name);
      }
   }
}


Comments

Popular posts from this blog

Solution for the fault: "System.Diagnostics.Process.Start is not working on IIS, but working on ASP.NET Development Server"

Solution for "Can not obtain Oracle client information from registry"