您的位置:首页 > 编程语言 > C#

201802151200->深入浅出设计模式:c#适配器模式

2018-02-15 12:00 441 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace _005适配器模式

{

    #region 模式1

    /*-------------------------------------适配接口-------------------------------------*/

    public interface ITarget

    {

        void GetTargetInterface();

    }

    /*-------------------------------------适配接口-------------------------------------*/

    /*-------------------------------------适配实体-------------------------------------*/

    public class Adaptee

    {

        public void GetTargetData()

        {

            Console.WriteLine("已获取目标数据...");

        }

    }

    /*-------------------------------------适配实体-------------------------------------*/

    /*-------------------------------------适配器-------------------------------------*/

    public class Adapter : Adaptee, ITarget

    {

        public void GetTargetInterface()

        {

            this.GetTargetData();

        }

    }

    /*-------------------------------------适配器-------------------------------------*/

    #endregion 模式1

    #region 模式2

    /*-------------------------------------适配器-------------------------------------*/

    public class Adapter2 : ITarget

    {

        public Adaptee adaptee = new Adaptee();

        public void GetTargetInterface()

        {

            adaptee.GetTargetData();

        }

    }

    /*-------------------------------------适配器-------------------------------------*/

    #endregion 模式2

    internal class Program

    {

        private static void Main(string[] args)

        {

            ITarget target1 = new Adapter();

            target1.GetTargetInterface();

            Console.WriteLine("模式1适配器...");

            ITarget target2 = new Adapter();

            target2.GetTargetInterface();

            Console.WriteLine("模式2适配器...");

            /*

             * 模式1利用调用基类来实现接口以达到适配的目的

             *

             * 模式2利用调用实例方法来达到适配目标

             */

            Console.ReadKey();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  心得 理解 笔记 c#