首页 > 其他分享 >06.适配器模式

06.适配器模式

时间:2022-11-16 22:01:29浏览次数:35  
标签:Console void 模式 class public WriteLine 06 适配器

适配器模式

作为两个不兼容的接口之间的桥梁

结合了两个独立接口的功能

例如实际中有个两孔的插座,现在需要有个三孔的插口,如果不直接在两孔的class修改,就需要加一个适配class来处理


namespace 适配器模式
{
    public class TwoHole
    {
        public void TwoPower()
        {
            Console.WriteLine("我是两个孔的插头");
        }
    }

    interface IThreeHole
    {
        void ThreePower();
    }

    class PowerAdapter : TwoHole, IThreeHole
    {
        public void ThreePower()
        {
            Console.WriteLine("我是3个孔的插头");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var powerAdapter = new PowerAdapter();
            powerAdapter.TwoPower();

            powerAdapter.ThreePower();

            Console.WriteLine("Hello World!");
        }
    }
    
}

标签:Console,void,模式,class,public,WriteLine,06,适配器
From: https://www.cnblogs.com/thomerson/p/16897661.html

相关文章