AdapterPattern-适配器模式

C#适配器模式(Adapter Pattern)是一种结构型设计模式,用于将一个类的接口转换成客户端所期望的另一个接口。

适配器模式的核心思想是通过一个适配器类,将原本不兼容的接口转换成兼容的接口,使得客户端能够使用这些接口进行交互,而不需要修改原有的类或接口。适配器模式通常涉及以下几个角色:

目标接口(Target Interface):客户端所期望的接口,通过适配器进行调用。

适配器类(Adapter Class):实现目标接口,并持有源接口的实例,通过调用源接口的方法来适配目标接口。

源接口(Adaptee Interface):需要被适配的接口或类。

namespace AdapterPattern_适配器模式
{/// <summary>
/// 目标接口
/// </summary>
    public interface SqlInterface
    {
        void Add<T>();
        void Delete<T>();
        void Update<T>();
        void Query<T>();
    }
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// SqlServer数据库
/// </summary>
    public class SqlServer : SqlInterface
    {
        public void Add<T>()
        {
            Console.WriteLine("This is {0} Add", this.GetType().Name);
        }
        public void Delete<T>()
        {
            Console.WriteLine("This is {0} Delete", this.GetType().Name);
        }
        public void Update<T>()
        {
            Console.WriteLine("This is {0} Update", this.GetType().Name);
        }
        public void Query<T>()
        {
            Console.WriteLine("This is {0} Query", this.GetType().Name);
        }
    }
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 源接口,新增MySql
/// </summary>
    public class MySql
    {
        public void AddRedis<T>()
        {
            Console.WriteLine("This is {0} Add", this.GetType().Name);
        }
        public void DeleteRedis<T>()
        {
            Console.WriteLine("This is {0} Delete", this.GetType().Name);
        }
        public void UpdateRedis<T>()
        {
            Console.WriteLine("This is {0} Update", this.GetType().Name);
        }
        public void QueryRedis<T>()
        {
            Console.WriteLine("This is {0} Query", this.GetType().Name);
        }
    }
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 类适配器模式
/// </summary>
    public class MySqlClass : MySql, SqlInterface
    {
        public void Add<T>()
        {
            base.AddRedis<T>();
        }

        public void Delete<T>()
        {
            base.DeleteRedis<T>();
        }

        public void Update<T>()
        {
            base.UpdateRedis<T>();
        }

        public void Query<T>()
        {
            base.QueryRedis<T>();
        }
    }
}
namespace AdapterPattern_适配器模式
{/// <summary>
/// 对象适配器模式
/// </summary>
    public class MySqlObject : SqlInterface
    {
        private MySql mySql = null;
        public MySqlObject(MySql mySql)
        {
            this.mySql = mySql;
        }
        public MySqlObject()
        {
            this.mySql = new MySql();
        }
        public void Add<T>()
        {
            this.mySql.AddRedis<T>();
        }

        public void Delete<T>()
        {
            this.mySql.DeleteRedis<T>();
        }

        public void Update<T>()
        {
            this.mySql.UpdateRedis<T>();
        }

        public void Query<T>()
        {
            this.mySql.QueryRedis<T>();
        }
    }
}
namespace AdapterPattern_适配器模式
{
    internal class Program
    {
        static void Main(string[] args)
        {
            {
                Console.WriteLine("*****************************");
                SqlInterface sqlserver = new SqlServer();
                sqlserver.Add<Program>();
                sqlserver.Delete<Program>();
                sqlserver.Update<Program>();
                sqlserver.Query<Program>();
            }
            {
                Console.WriteLine("*****************************");
                SqlInterface mysqlclass = new MySqlClass();
                mysqlclass.Add<Program>();
                mysqlclass.Delete<Program>();
                mysqlclass.Update<Program>();
                mysqlclass.Query<Program>();
            }
            {
                Console.WriteLine("*****************************");

                SqlInterface mysqlclass = new MySqlObject();
                mysqlclass.Add<Program>();
                mysqlclass.Delete<Program>();
                mysqlclass.Update<Program>();
                mysqlclass.Query<Program>();
            }
            {
                Console.WriteLine("*****************************");
                MySql mysql = new MySql();
                SqlInterface mysqlclass = new MySqlObject(mysql);
                mysqlclass.Add<Program>();
                mysqlclass.Delete<Program>();
                mysqlclass.Update<Program>();
                mysqlclass.Query<Program>();
            }
            Console.Read();
            //输出结果
            //*****************************
            //This is SqlServer Add
            //This is SqlServer Delete
            //This is SqlServer Update
            //This is SqlServer Query
            //* ****************************
            //This is MySqlClass Add
            //This is MySqlClass Delete
            //This is MySqlClass Update
            //This is MySqlClass Query
            //* ****************************
            //This is MySql Add
            //This is MySql Delete
            //This is MySql Update
            //This is MySql Query
            //* ****************************
            //This is MySql Add
            //This is MySql Delete
            //This is MySql Update
            //This is MySql Query
        }
    }
}

适配器模式的优点是能够将已有的类或接口与需要的接口进行适配,使得客户端能够通过统一的接口来调用不同的实现类。这样可以提高代码的扩展性和复用性。但适配器模式也有一定的局限性,特别是对于已经存在大量实现的系统,而需要新增不兼容接口的情况下,需要谨慎考虑适配器模式的应用。

 

适配器模式(Adapter Pattern)是一种结构型设计模式,用于将一个类的接口转换成客户端所期望的另一个接口。适配器模式具有以下优点和缺点:

优点:

  1. 提供接口转换:适配器模式允许将一个类的接口转换为另一个接口,使得原本不兼容的类能够协同工作。这样可以提供更好的接口一致性,将不同类之间的耦合度降到最低。

  2. 复用现有类:适配器模式可以复用现有的类,不需要修改原有类的代码。适配器作为一个中间层,将原有类的接口包装起来,使其符合客户端的需求,提高代码的复用性。

  3. 编写灵活性:适配器模式可以编写具有灵活性的代码。由于适配器模式将不同类的接口解耦,因此可以更加方便地进行代码修改和扩展。

  4. 支持功能扩展:适配器模式可以支持对现有功能的扩展。通过适配器,可以在不修改原有类的情况下添加新的方法或属性,提供更多的功能。

缺点:

  1. 增加了复杂性:适配器模式会增加代码的复杂性。引入适配器可以增加类和接口的数量,使得代码结构变得更加复杂。

  2. 运行效率下降:适配器模式在对原有类的接口进行转换时,可能需要进行一些额外的转换和处理。这可能会对程序的运行效率产生一定的影响。

  3. 不适用于所有情况:适配器模式并非在所有情况下都适用。对于已经存在多个兼容接口的类或者能够直接修改源代码的情况,适配器模式可能并不是必需的。

需要根据具体的应用场景和需求来评估使用适配器模式的利弊。适配器模式适用于需要不同接口之间进行兼容性转换的情况,能够提供接口一致性和代码复用。通过权衡其优点和缺点,可以选择是否使用适配器模式。

posted @ 2023-08-16 11:12  ZHIZRL  阅读(10)  评论(0编辑  收藏  举报