首页 > 其他 > 详细

适配器模式

时间:2019-07-04 15:55:55      阅读:85      评论:0      收藏:0      [点我收藏+]

适配器模式非常简单。就是建立一个适配器。

把一个现有的类的对象包含过来,利用现有对象的方法,做一些改动。来满足现在的新需求。 这也是组合思想的最简单的实践。

而android 的adapter.完全不是设计模式的adapter的意思。 可见不一定要满足设计模式的东西才能叫适配器。

google 就把list 的填充器,叫做Adapter 。大家用的还不是没有任何意见。

适配器,使用也非常多。没办法。大家都是调包侠。包需要新功能,只能搞搞适配器模式,才能维持得了生活的样子。

public class Adapter
{
    public interface Ichannel
    {
        public int reasePower();
    }

    public static class China220VChannel implements Ichannel
    {
        public int reasePower()
        {
            return 220;
        }
    }

    public static class AdapterAmanican implements Ichannel
    {
        private China220VChannel mChina220VChannel=new China220VChannel();

        @Override
        public int reasePower()
        {
            int v=mChina220VChannel.reasePower();
            v=v-110;
            return v;
        }
    }

    public static class AmanicanVCD
    {
        public String play(Ichannel channel)
        {
            if(channel.reasePower()==110)
            {
                return "the fv is ok.i can play sm";
            }
            else
            {
                return "fv is not ok";
            }
        }
    }

    public static void Run()
    {
        AmanicanVCD myvcd=new AmanicanVCD();
        String result =myvcd.play(new China220VChannel());
        String result2=myvcd.play(new AdapterAmanican());

        LSComponentsHelper.LS_Log.Log_INFO(result+result2);
    }
}

 

适配器模式

原文:https://www.cnblogs.com/lsfv/p/11132803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!