接口模式 - 1.1 适配器模式 --代码实现(C#)

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IDoSome_target idosome = new DoSome_Adapter();
            idosome.request();
            Console.ReadKey();

            IDoSome_target2 idosome2 = new DoSome_Adapter2();
            idosome2.request();
            Console.ReadKey();
            IDoSome_target3 idosome3 = new DoSome_Adapter3();
            idosome3.request();

            Console.ReadKey();
        }

    }
    # region 类适配器模式
    /// <summary>
    /// 当客户(Client)已存在固定接口(IDoSome_target)时,而新开发的类的(DoSome)方法名或者方法不符合客户调用的时候,
    /// 需要创建一个中间类(DoSome_Adapter)来调用已经存在的类,可以使用类适配器模式
    /// </summary>
    interface IDoSome_target
    {
       void request();
    }
    public class DoSome
    {
       public void specialRequest()
       {
           Console.WriteLine("reponse to special request!");
       }
    }
    public class DoSome_Adapter:DoSome,IDoSome_target
    {
        public void request()
        {
            this.specialRequest();
        }
    }
#endregion

    # region 对象适配器模式
    /// <summary>
    /// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
    /// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式
   
    /// </summary>
    public class IDoSome_target2
    {
        public virtual void request() { Console.WriteLine("2--"); }
    }
    public class DoSome2
    {
        public void specialRequest()
        {
            Console.WriteLine("2---reponse to special request!");
        }
    }
    public class DoSome_Adapter2 :IDoSome_target2
    {
        private DoSome2 _DoSome2= new DoSome2();
        public override void request()
        {
            _DoSome2.specialRequest();
        }
    }
    #endregion

    # region 对象适配器模式2
    /// <summary>
    /// 当客户(Client) 未定义接口,而是一个类(Target),新开发的类的(Adaptee)方法名或者方法不符合客户调用的时候,
    /// 需要创建一个中间类(Adapter)来调用已经存在的类,可以使用对象适配器模式

    /// </summary>
    public abstract class IDoSome_target3
    {
        public abstract void request();
    }
    public class DoSome3
    {
        public void specialRequest()
        {
            Console.WriteLine("3---reponse to special request!");
        }
    }
    public class DoSome_Adapter3 : IDoSome_target3
    {
        private DoSome3 _DoSome3 = new DoSome3();
        public override void request()
        {
            _DoSome3.specialRequest();
        }
    }
    #endregion


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值