适配器模式

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

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

而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);
    }
}

 

posted @ 2019-07-04 15:39  琴鸟  阅读(96)  评论(0编辑  收藏  举报