mercredi 28 avril 2021

Is the Target interface really necessary in adapter design pattern?

I saw a code example online that uses adapter pattern as: enter image description here enter image description here enter image description here

if you look at the HR system, the employee information in the form of string array and the ProcessSalary method of the Third Party Billing System wants to data in List. So, the HR System cannot call directly to the Third Party Billing System because List and string array are not compatible. So we use adapter pattern, the full code is:

namespace AdapterDesignPattern
{
    public interface ITarget
    {
        void ProcessCompanySalary(string[,] employeesArray);
    }
}



namespace AdapterDesignPattern
{
    public class EmployeeAdapter : ThirdPartyBillingSystem, ITarget
    {
        public void ProcessCompanySalary(string[,] employeesArray)
        {
            string Id = null;
            string Name = null;
            string Designation = null;
            string Salary = null;

            List<Employee> listEmployee = new List<Employee>();

            for (int i = 0; i < employeesArray.GetLength(0); i++)
            {
                for (int j = 0; j < employeesArray.GetLength(1); j++)
                {
                    if (j == 0)
                    {
                        Id = employeesArray[i, j];
                    }
                    else if (j == 1)
                    {
                        Name = employeesArray[i, j];
                    }
                    else if (j == 1)
                    {
                        Designation = employeesArray[i, j];
                    }
                    else
                    {
                        Salary = employeesArray[i, j];
                    }
                }

                listEmployee.Add(new Employee(Convert.ToInt32(Id), Name, Designation, Convert.ToDecimal(Salary)));
            }

            Console.WriteLine("Adapter converted Array of Employee to List of Employee");
            Console.WriteLine("Then delegate to the ThirdPartyBillingSystem for processing the employee salary\n");
            ProcessSalary(listEmployee);
        }
    }
}

// Cilent
namespace AdapterDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] employeesArray = new string[5, 4] 
            {
                {"101","John","SE","10000"},
                {"102","Smith","SE","20000"},
                {"103","Dev","SSE","30000"},
                {"104","Pam","SE","40000"},
                {"105","Sara","SSE","50000"}
            };
            
            ITarget target = new EmployeeAdapter();  <-------why not just use new EmployeeAdapter() directly?
            Console.WriteLine("HR system passes employee string array to Adapter\n");
            target.ProcessCompanySalary(employeesArray); 
        }
    }
}

I just don't understand one thing, is ITarget interface really necessary? Why not just get rid of it, then the client can do this:

namespace AdapterDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] employeesArray = new string[5, 4] { ... };
            Console.WriteLine("HR system passes employee string array to Adapter\n");
            new EmployeeAdapter().ProcessCompanySalary(employeesArray);
        }
    }
}

so that we can call ProcessCompanySalary on EmployeeAdapter instance directly?

Aucun commentaire:

Enregistrer un commentaire