Strategy Design Pattern

We have a bunch of algorithms/functionality and can be applied to the data set only by
Depending upon the data, at runtime. So we use strategy design pattern
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it



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

// Liskov Substitution Principle

namespace ManualResetEvent_
{


//class which calls the interface and based on which the mehods are called.

    class Filetoberead
    {
        public string fileName { get; set; }
        public string filetype { get; set; }


        public IFileRead fileread_;

        public Filetoberead(IFileRead file_name_new)
        {
            fileread_ = file_name_new;
        }

        public void CallFileReadClass(string Filename, string type)
        {
            fileread_.CallFileReadClass(fileName, filetype);
        }


    }

    class Prog
    {
        public static void Main()
    {

      
        Filetoberead file1 = new Filetoberead(new FlatFileRead());
        file1.filetype = "FLAT";
        file1.fileName = "Test.flat";
        file1.CallFileReadClass(file1.fileName ,file1.filetype);
        Console.ReadLine();


    }
    }


//Interface//
    interface IFileRead
    {
        void CallFileReadClass(string Filename, string type);
    }

// Family of algorithms////


    class XMLFileRead:IFileRead
    {
        public void CallFileReadClass(string Filename, string type)
        {
            Console.WriteLine("I am responsible for reading XML file" + Filename);
        }
    }

    class CSVFileRead :IFileRead
    {
        public void CallFileReadClass(string Filename, string type)
        {
            Console.WriteLine("I am responsible for reading CSV file "+Filename);
        }
    }

    class FlatFileRead:IFileRead
    {
        public void CallFileReadClass(string Filename, string type)
        {
            Console.WriteLine("I am responsible for reading FLAT file "+Filename);
        }
    }

}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6