locked
Pattern Selection RRS feed

  • Question

  • User-574293449 posted

    hi,

      I am learning design patterns now and started with adapter pattern. So as per my understanding adapeter patterns uses to interface incompatible types.

    1. As an example, i have SQL and Oracle as the backend. So some customer needs oracle and some needs sql. Here for sql we need sql client and the methods and for oracle different. For client i can give common interface and class called adpater so that client don't need to know the specfic implementation. Is my understanding right?

    Kindly clarify me if i am wrong.

    2. If my point is right i have one confusion like thinking of opposite scneario. I have common db as SQL. For data collection i have seperate custom scenarios for each client like for one client i interface hardware with serial port and another with excel import and another direct entry. But all these data will enetred in same table and i have SQL database only. Here which pattern help me to commonize the code? Or i use like layered approach. (Example Raw methods as hosted in wcf and the the client pages as custom for each one?

    Can anyone clarify me this scenarios so that i can have the clarity

    Sunday, December 8, 2013 11:45 PM

All replies

  • User697462465 posted

    Hi akhilrajau,

    According to your description, my understanding is that you would like to design your application to support multiple database using adapter pattern.

    If so, please refer to the following sample code:

    public class ThirdPartyBillingSystem
        {
            private ITarget employeeSource;
            public ThirdPartyBillingSystem(ITarget employeeSource)
            {
                this.employeeSource = employeeSource;
            }
            public void ShowCurrentConnection()
            {
                string connection = employeeSource.PrintCurrentConnection();
                //To DO:Implement you busineee logic
                Console.WriteLine(connection);
            }
        }
        /// <summary>
        /// The 'ITarget' interface
        /// </summary>
        public interface ITarget
        {
            string PrintCurrentConnection();
        }
        public class MSSQLCLASS{
            //there is just sample, you can return the connection object
            public string GetConnection()
            {
                return "MSSQL Connection";
            }
        }
        public class ORACLECLASS
        {
            //there is just sample, you can return the connection object
            public string GetConnection()
            {
                return "ORACLE Connection";
            }
        }
    
        /// <summary>
        /// The 'Adapter' class
        /// </summary>
        public class MSSQLAdapter : MSSQLCLASS, ITarget
        {
            public string PrintCurrentConnection()
            {
                return GetConnection();
            }
        }
        /// <summary>
        /// The 'Adapter' class
        /// </summary>
        public class ORACLEAdapter : ORACLECLASS, ITarget
        {
            public string PrintCurrentConnection()
            {
                return GetConnection();
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                ITarget Itarget = new MSSQLAdapter();
                ThirdPartyBillingSystem client = new ThirdPartyBillingSystem(Itarget);
                client.ShowCurrentConnection();
                Itarget = new ORACLEAdapter();
                client = new ThirdPartyBillingSystem(Itarget);
                client.ShowCurrentConnection();
                Console.ReadKey();
            }
        }

    More information about adapter pattern please refer to:
    http://www.codeproject.com/Articles/42915/Adapter-Design-Pattern

    Hope it helps.

    Best Regards,
    Terry Guo

    Tuesday, December 10, 2013 12:05 AM
  • User-574293449 posted

    hi,

      Thanks for the help. But actually i just told the adpater pattern scenario. Is my scnario is right or wrong? And my question is not the implementation of adapater pattern. My doubt is the pattern for applying the second scenario in the second.  For clarification i will numbering the points in the first post.

    Tuesday, December 10, 2013 1:36 AM