Monday, September 15, 2014

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);
      }
   }
}


Friday, September 5, 2014

Design Patterns - Strategy Pattern

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

namespace DesignPattern_StrategyPattern
{
   class Program
   {
      static void Main(string[] args)
      {
         /* STRATEGY PATTERN (POLICY PATTERN):
          * Strategy lets the algorithm vary independently 
          * from clients that use it.
          * When the program is running, we can switch between different 
          * type of algorithms
          * For instance, a class that performs validation on incoming data
          * may use a strategy pattern to select a validation 
          * algorithm based on the type of data, 
          * the source of the data, user choice,
          * or other discriminating factors. These factors are not known 
          * for each case until run-time, and may require 
          * radically different validation to be performed.
          */

          ICalculate calculate = null;

          while (true)
          {
             try
             {
                Console.WriteLine("Choose an operation! ( 1 = Plusus, 2 = Minus )");
                StrategyType strategyType = (StrategyType)Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter the first number");
                int value1 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter the second number");
                int value2 = Convert.ToInt32(Console.ReadLine());

                switch (strategyType)
                {
                   case StrategyType.Plussus:
                      calculate = new Plussus();
                      Console.WriteLine("Operation: Plusus, Result: " + calculate.Calculate(value1, value2));
                      break;
                   case StrategyType.Minus:
                      calculate = new Minus();
                      Console.WriteLine("Operation: Minus, Result: " + calculate.Calculate(value1, value2));
                      break;
                }
             }
             catch (Exception)
             {
                Console.WriteLine("Enter a valid number!");
             }
         }
      }
   }
}


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

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

namespace DesignPattern_StrategyPattern
{
   //The interface for the strategies
   public interface ICalculate
   {
      //Method is defined in the interface
      int Calculate(int value1, int value2);
   }
}

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

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

namespace DesignPattern_StrategyPattern
{
   //Strategy: Minus
   public class Minus : ICalculate
   {
      public int Calculate(int value1, int value2)
      {
         //The minus logis is defined
         return value1 - value2;
      }
   }
}

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

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

namespace DesignPattern_StrategyPattern
{
   //Strategy: Plussus
   public class Plussus:ICalculate
   {
      public int Calculate(int value1, int value2)
      {
         //The plussus logic is defined
         return value1 + value2;
      }
   }
}

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

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

namespace DesignPattern_StrategyPattern
{
   enum StrategyType
   {
      Plussus = 1,
      Minus = 2
   }
}

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

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

namespace DesignPattern_StrategyPattern
{
   //The client
   public class CalculateClient
   {
      private ICalculate calculate;

      public CalculateClient(ICalculate strategy)
      {
         this.calculate = strategy;
      }

      public int Calculate(int value1, int value2)
      {
         return calculate.Calculate(value1, value2);
      }
   }
}