Adaper(适配器模式)

Adaper(适配器模式)作用就是把不兼容的东西,转化为可用;

使用时机:系统后期维护,功能扩展时使用;

一、类适配器

下面,我们一起看个例子,我底数据经常要更换,目前系统支持sqlserver,mysql;现在我们要增加对oracle的支持。

定义操作数据库的接口:

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

namespace AdaperShow
{
    public interface IHelper
    {
        void Add();
        void Delete();
        void Update();
        void Search();
    }
}

目前系统支持sqlserver,mysql

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

namespace AdaperShow
{
    public  class SQLserverHelper : IHelper
    {
        public void Add()
        {
            Console.WriteLine("这里是Add,{0}",this.GetType());
        }

        public void Delete()
        {
            Console.WriteLine("这里是Delete,{0}", this.GetType());
        }

        public void Search()
        {
            Console.WriteLine("这里是Search,{0}", this.GetType());
        }

        public void Update()
        {
            Console.WriteLine("这里是Update,{0}", this.GetType());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdaperShow
{
    public class MysqlHelper : IHelper
    {
        public void Add()
        {
            Console.WriteLine("这里是Add,{0}", this.GetType());
        }

        public void Delete()
        {
            Console.WriteLine("这里是Delete,{0}", this.GetType());
        }

        public void Search()
        {
            Console.WriteLine("这里是Search,{0}", this.GetType());
        }

        public void Update()
        {
            Console.WriteLine("这里是Update,{0}", this.GetType());
        }
    }
}

下面我们要类适配器实现对oracle的操作

具体的操作类

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

namespace AdaperShow
{
   public class OracleAdaper:Oracle,IHelper
    {
        public void Add()
        {
            base.Add();
        }

        public void Delete()
        {
            base.Delete();
        }

        public void Search()
        {
            base.Search();
        }

        public void Update()
        {
            Console.WriteLine("这里是Update,没有实现,{0}", this.GetType());
            base.Delete();
            base.Add();
        }
    }
}

上端调用

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

namespace AdaperShow
{
    /// <summary>
    /// 1、适配器模式是做什么的
    /// 2、类适配器和对象的适配器
    /// 3、工作中的应用
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            IHelper sqlhelper = new SQLserverHelper();
            sqlhelper.Add();
            sqlhelper.Delete();
            sqlhelper.Update();
            sqlhelper.Search();

            IHelper mysqlhelper = new MysqlHelper();
            mysqlhelper.Add();
            mysqlhelper.Delete();
            mysqlhelper.Update();
            mysqlhelper.Search();


            IHelper OracleHelper = new OracleAdaper();            
            OracleHelper.Add();
            OracleHelper.Delete();
            OracleHelper.Update();
            OracleHelper.Search();
            Console.Read();

        }
    }
}

二、对象适配器

普通 手机没支付功能,我们用对象适配器,对对象的功能进行扩展

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

namespace AdaperShow
{
    public interface Iphone
    {
        void call();
        void message();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdaperShow
{
    public class Dagedaphone : Iphone
    {
        public void call()
        {
            Console.WriteLine("手机打电话");
        }

        public void message()
        {
            Console.WriteLine("手机发信息");
        }
    }
}

创建一个对象适配器

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

namespace AdaperShow
{
    public class PhoneAdaper
    {
        private Iphone _Iphone;
        public PhoneAdaper(Iphone iphone)
        {
            _Iphone = iphone;
        }
        public void zifu()
        {
            Console.WriteLine("这是支付");
        }
    }
}

上端调用

            Iphone iphone = new Dagedaphone();
            PhoneAdaper dagedaAdaper = new PhoneAdaper(iphone);
            dagedaAdaper.zifu();

 

posted on 2016-12-21 23:29  枫叶168  阅读(282)  评论(0)    收藏  举报

导航